o
    *j`$                     @   s  d dl Z d dlZd dlmZmZmZ d dlZd dlZd dlZ	d dl
Z
d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ ejejejejd	Zejejejd
G dd deZ dd Z!dd Z"	dddZ#dddZ$dd Z%dd Z&dS )    N)AnyDictUnion)
transforms)Preprocessors)Preprocessor)PREPROCESSORS)	LoadImage)FieldsModeKeys)constantedgeZreflectZ	symmetric)module_namec                	       st   e Zd Zdejddddg dg dfdedef fdd	Zd
eeef deee	f fddZ
dd Zdd Z  ZS )ImageSkyChangePreprocessorNi  i   )g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?	model_dirmodec	                    s4   t  | ||g| _||g| _tj||d| _dS )a  
        Args:
            model_dir (str): model directory to initialize some resource.
            mode: The mode for the preprocessor.
            coarse_model_width: required width of input tensor of coarse model.
            coarse_model_height: required height of input tensor of coarse model.
            refine_model_width: required width of input tensor of refine model.
            refine_model_height: required height of input tensor of refine model.
            mean_vec: mean of dataset(for transforms.Normalize), default is mean of Imagenet dataset.
            std_vec: standard deviation of dataset(for transforms.Normalize), default is std of Imagenet dataset.
        )meanZstdN)super__init__coarse_input_sizerefine_input_sizer   Z	Normalize	normalize)selfr   r   Zcoarse_model_widthZcoarse_model_heightZrefine_model_widthZrefine_model_heightZmean_vecZstd_vecargskwargs	__class__ r/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/image_skychange/preprocessor.pyr      s   

z#ImageSkyChangePreprocessor.__init__datareturnc           	      K   s:  d|  vr
tdd|  vrtdt|d tr=t|d }|tj}t	
|t	j}|dur9| |}ntdtdt|d trjt|d }|tj}t	
|t	j}|durf| |}ntdtd	i }| |\}}| |\}}||d< ||d
< ||d< ||d< ||| j| jdd|d< |S )aH  process the raw input data
        Args:
            data (dict): data dict containing following info:
                sky_image, scene_image

        Example:
            >>> {
            >>>     "sky_image": "xxx.jpg" # sky_image path(str)
            >>>     "scene_image": "xxx.jpg", # scene_image path(str)
            >>> }

        Returns:
            Dict[str, Any]: the preprocessed data
            {
                "sky_image": the preprocessed sky image(origin size)
                "sky_image_refine": the preprocessed resized sky image
                "scene_image": the preprocessed scene image(origin size)
                "scene_image_refine": the preprocessed resized scene image
                "img_metas": informations of preprocessed images, e.g. origin shape, pad information, resized shape.
            }
        	sky_imagezsky_image not in input datascene_imagezscene_image not in input dataNzsky_image is Nonez)sky_image(path of sky image) is not validzscene_image is Nonez-scene_image(path of scene image) is not validsky_image_refinescene_image_refine)r   r   )sky_img_metasscene_img_metasZ
input_size	img_metas)keys	Exception
isinstancestrr	   Zconvert_to_ndarrayastypenpZuint8cv2ZcvtColorZCOLOR_RGB2BGRcheck_imageprocess_single_imgr   r   )	r   r   r   r!   r"   r#   r%   r$   r&   r   r   r   __call__<   sH   
z#ImageSkyChangePreprocessor.__call__c                 C   s^   i }|j dd |d< t|| j\}}t|| j}|d}||d< |j dd  |d< ||fS )Nr      Z	ori_shapepad_directionZrefine_shape)shapeget_refine_inputr   image_transformr   Z	unsqueeze)r   imgr'   r3   r   r   r   r0   |   s   
z-ImageSkyChangePreprocessor.process_single_imgc                 C   s   |j }t|dkrtj|||gdd}|S |d dkr'tj|||gdd}|S |d dkrM|d d d d ddf d |d d d d ddf  d d }|S )	Nr2   )Zaxis      r      g      ?g     o@)r4   lenr-   stackZconcatenate)r   Z	input_imgZwhole_temp_shaper   r   r   r/      s0   z&ImageSkyChangePreprocessor.check_image)__name__
__module____qualname__r   Z	INFERENCEr+   r   r   r   r   r1   r0   r/   __classcell__r   r   r   r   r      s"    "@r   c                 C   s"   t | |} t| |d\} }| |fS )Nr   )max_dim_matchcenter_pad_image_withwh)Zmatr   r3   r   r   r   r5      s   
r5   c                 C   s   t | \}}}|\}}||ks||kr@t|| }t|| }t||}	t||	 d }
t||	 d }tj| ||
ftjd} | S )N      ?)interpolation)r-   r4   floatminintr.   resizeZINTER_LINEAR)imageZrefine_model_sizehwcZresize_wZresize_hZh_scaleZw_scaleZresize_scaleZnew_hZnew_wr   r   r   rA      s   
rA   r   c                 C   s   | }| j d | j d }}t|d | d}t|d | d}d}	|dks)|dkrKt|d d }
t|d d }|
|||
 || f}	t| |	||d}||	fS )Nr   r8   )r   r   r   r   r2   rC   )padding_mode)r4   maxrG   pad)rI   Z	crop_sizeZpadvaluerM   Z	pad_imagerJ   rK   Zpad_hZpad_wr3   Zhalf_wZhalf_hr   r   r   rB      s   rB   c           	   	   C   s   t | stdt| t|tjttfrt	|dkr td|d }|d }|d }|d }t	| j
}|dkrGtj| ||||t| |dS |dkrj| j
d dkrjtj| ||||t| |dd d d d tjf S tj| ||||t| |dS )	Nz#img should be numpy ndarray. Got {}r9   zGot inappropriate padding argr   r8   r2   r:   )topbottomleftrightZ
borderTypevalue)is_numpy_image	TypeErrorformattyper*   numbersNumbertuplelistr;   r4   r.   ZcopyMakeBorder_cv2_pad_to_strr-   Znewaxis)	r7   paddingfillrM   pad_leftpad_top	pad_rightZ
pad_bottomZ	shape_lenr   r   r   rO      sZ   

	
rO   c                 C   sL   | d d d d d d df } |  d} | tjd } |t|  } | S )N)r2   r   r8      )Z	transposer,   r-   Zfloat32torchZ
from_numpycopy)r7   r   r   r   r   r6      s
   
r6   c                 C   s   t | tjo
| jdv S )N>   r2   r:   )r*   r-   Zndarrayndim)r7   r   r   r   rU      s   rU   )r   )r   r   )'rY   Zpdbtypingr   r   r   r.   jsonnumpyr-   re   Ztorchvisionr   Zmodelscope.metainfor   Zmodelscope.preprocessorsr   Z modelscope.preprocessors.builderr   Zmodelscope.preprocessors.imager	   Zmodelscope.utils.constantr
   r   ZBORDER_CONSTANTZBORDER_REPLICATEZBORDER_REFLECT_101ZBORDER_REFLECTr]   Zregister_moduleZcvZimage_sky_change_preprocessorr   r5   rA   rB   rO   r6   rU   r   r   r   r   <module>   s:   y

.