o
    ߥi`$                     @   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edgereflect	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.
        )meanstdN)super__init__coarse_input_sizerefine_input_sizer   	Normalize	normalize)selfr   r   coarse_model_widthcoarse_model_heightrefine_model_widthrefine_model_heightmean_vecstd_vecargskwargs	__class__ e/home/ubuntu/.local/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_metas
input_size	img_metas)keys	Exception
isinstancestrr	   convert_to_ndarrayastypenpuint8cv2cvtColorCOLOR_RGB2BGRcheck_imageprocess_single_imgr   r   )	r   r)   r$   r+   r,   r-   r/   r.   r0   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      	ori_shapepad_directionrefine_shape)shapeget_refine_inputr   image_transformr   	unsqueeze)r   imgr2   rC   r'   r'   r(   r?   |   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 )	NrA   )axis      r      g      ?g     o@)rE   lenr9   stackconcatenate)r   	input_imgwhole_temp_shaper'   r'   r(   r>      s0   z&ImageSkyChangePreprocessor.check_image)__name__
__module____qualname__r   	INFERENCEr6   r   r   r   r   r@   r?   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)matr   rC   r'   r'   r(   rF      s   
rF   c                 C   s   t | \}}}|\}}||ks||kr@t|| }t|| }t||}	t||	 d }
t||	 d }tj| ||
ftjd} | S )N      ?)interpolation)r9   rE   floatminintr;   resizeINTER_LINEAR)imagerefine_model_sizehwcresize_wresize_hh_scalew_scaleresize_scalenew_hnew_wr'   r'   r(   rX      s   
rX   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   rK   )r   r   r   r   rA   r[   )padding_mode)rE   maxr_   pad)rb   	crop_sizepadvaluern   	pad_imagerd   re   pad_hpad_wrC   half_whalf_hr'   r'   r(   rY      s   rY   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 {}rL   zGot inappropriate padding argr   rK   rA   rM   )topbottomleftright
borderTypevalue)is_numpy_image	TypeErrorformattyper5   numbersNumbertuplelistrN   rE   r;   copyMakeBorder_cv2_pad_to_strr9   newaxis)	rI   paddingfillrn   pad_leftpad_top	pad_right
pad_bottom	shape_lenr'   r'   r(   rp      sZ   

	
rp   c                 C   sL   | d d d d d d df } |  d} | tjd } |t|  } | S )N)rA   r   rK      )	transposer8   r9   float32torch
from_numpycopy)rI   r   r'   r'   r(   rG      s
   
rG   c                 C   s   t | tjo
| jdv S )N>   rA   rM   )r5   r9   ndarrayndim)rI   r'   r'   r(   r~      s   r~   )r   )r   r   )'r   pdbtypingr   r   r   r;   jsonnumpyr9   r   torchvisionr   modelscope.metainfor   modelscope.preprocessorsr    modelscope.preprocessors.builderr   modelscope.preprocessors.imager	   modelscope.utils.constantr
   r   BORDER_CONSTANTBORDER_REPLICATEBORDER_REFLECT_101BORDER_REFLECTr   register_modulecvimage_sky_change_preprocessorr   rF   rX   rY   rp   rG   r~   r'   r'   r'   r(   <module>   s:   y

.