o
    ॵi5                     @   s  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m	Z	 d dlm
Z
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 d
dlmZ d
dlmZ eejejG dd dZdede
j
fddZejejejdG dd deZ ejejej!dG dd deZ"ejejej#dG dd deZ$ejejej%dG dd deZ&ejejej'dG dd deZ(ejejej)dG dd deZ*ejejej+dG d d! d!eZ,ejejej-dG d"d# d#eZ.dS )$    N)AnyDictUnion)ndarray)ImageImageOps)File)Preprocessors)	InputKeys)Fields)type_assert   )Preprocessor)PREPROCESSORSc                   @   sd   e Zd ZdZdddZdeeeeef f fddZd	d
 Z	e
defddZe
defddZdS )	LoadImagea  Load an image from file or url.
    Added or updated keys are "filename", "img", "img_shape",
    "ori_shape" (same as `img_shape`), "pad_shape" (same as `img_shape`),
    "scale_factor" (1.0) and "img_norm_cfg" (means=0 and stds=1).
    Args:
        mode (str): See :ref:`PIL.Mode<https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes>`.
        backend (str): Type of loading image. Should be: cv2 or pillow. Default is pillow.
    rgbpillowc                 C   s   |  | _|| _d S )N)uppermodebackend)selfr   r    r   R/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/preprocessors/image.py__init__   s   

zLoadImage.__init__inputc                 C   sd  t |tr
|d }n|}| jdkrVt|}||2}t|tj}| j	dkr0t
|tj| |jd |jd |jd }}}|||f}	W d   n1 sPw   Y  nC| jdkrt|}
t|
}t|}t|}|| j	}W d   n1 sw   Y  |jd |jd d	f}	ntd
| j |||	dd}t |tr| }|| |}|S )zCall functions to load image and get image meta information.
        Args:
            input (str or dict): input image path or input dict with
                a key `filename`.
        Returns:
            dict: The dict contains loaded image.
        filenamecv2RGBr   r      Nr      z/backend should be either cv2 or pillow,but got img)r   r    	img_shape	img_field)
isinstancedictr   r   _get_storageas_local_pathr   imreadIMREAD_COLORr   cvtColorCOLOR_BGR2RGBshapereadioBytesIOr   openr   exif_transposeconvertsize	TypeErrorcopyupdate)r   r   image_path_or_urlstorageimg_pathr    img_himg_wimg_cr!   bytesinfileresults	input_retr   r   r   __call__#   sD   




"





zLoadImage.__call__c                 C   s   | j j d| j d}|S )Nz(mode=))	__class____name__r   )r   repr_strr   r   r   __repr__R   s   zLoadImage.__repr__returnc                 C   s   t | trtt| }|S t | tjjrt| d}|S t | tjrCt	| j
dkr3t| tj} | d d d d d d df }|S t | trZ| tjd }|rXtt|}|S tdt|  )Nr   r   9input should be either str, PIL.Image, np.array, but got )r#   strnparray
load_imagePILr   r1   r   lenr+   r   r)   COLOR_GRAY2BGRr   getr
   IMAGEr3   typer   r    r   r   r   convert_to_ndarrayV   s&   

zLoadImage.convert_to_ndarrayc                 C   s   t | trt| }|S t | tjjr| d}|S t | tjrHt| j	dkr-t
| t
j}| d d d d d d df }t|dd}|S t | tr\| tjd }|rZt|}|S tdt|  )Nr   r   rG   uint8rH   )r#   rI   rL   rM   r   r1   rJ   r   rN   r+   r   r)   rO   	fromarrayastyper   rP   r
   rQ   r3   rR   rS   r   r   r   convert_to_imgi   s(   


zLoadImage.convert_to_imgN)r   r   )rC   
__module____qualname____doc__r   r   rI   r   r@   rE   staticmethodr   rT   rX   r   r   r   r   r      s    
	/r   r6   rF   c                 C   s   t  }|| d S )z simple interface to load an image from file or url

    Args:
        image_path_or_url (str): image file path or http url
    r    )r   )r6   loaderr   r   r   rL   ~   s   rL   )module_namec                       sD   e Zd Zd fdd	Zeeedejdee	ef fddZ
  ZS )	"ObjectDetectionTinynasPreprocessor    c                    s   t  jdi | || _dS )a  Preprocess the image.

        What this preprocessor will do:
        1. Transpose the image matrix to make the channel the first dim.
        2. If the size_divisible is gt than 0, it will be used to pad the image.
        3. Expand an extra image dim as dim 0.

        Args:
            size_divisible (int): The number will be used as a length unit to pad the image.
                Formula: int(math.ceil(shape / size_divisible) * size_divisible)
                Default 32.
        Nr   )superr   size_divisible)r   rb   kwargsrB   r   r   r      s   
z+ObjectDetectionTinynasPreprocessor.__init__datarF   c                 C   s   | tj}|d}|j}| jdkr@ddl}| j}t|}t|	|d | | |d< t|	|d | | |d< t
|}t| tj}||ddd|jd d|jd f< t|d}d|iS )zPreprocess the image.

        Args:
            data: The input image with 3 dimensions.

        Returns:
            The processed data in dict.
            {'img': np.ndarray}

        )r   r   r   r   Nr   r   r    )rW   rJ   float32	transposer+   rb   mathlistintceiltuplezerosexpand_dims)r   re   imager+   rh   stridepad_imgr   r   r   r@      s   

&z+ObjectDetectionTinynasPreprocessor.__call__)r`   )rC   rY   rZ   r   r   objectrJ   r   r   rI   r@   __classcell__r   r   rd   r   r_      s    &r_   c                       sN   e Zd Zdef fddZeeedeeef deeef fddZ	  Z
S )%ImageColorEnhanceFinetunePreprocessor	model_dirc                       t  j|i | || _dS )zmpreprocess the data from the `model_dir` path

        Args:
            model_dir (str): model path
        Nra   r   ru   r   ru   argsrc   rd   r   r   r      s   
z.ImageColorEnhanceFinetunePreprocessor.__init__re   rF   c                 C      |S )a  process the raw input data

        Args:
            data (tuple): [sentence1, sentence2]
                sentence1 (str): a sentence
                    Example:
                        'you are so handsome.'
                sentence2 (str): a sentence
                    Example:
                        'you are so beautiful.'
        Returns:
            Dict[str, Any]: the preprocessed data
        r   r   re   r   r   r   r@      s   z.ImageColorEnhanceFinetunePreprocessor.__call__)rC   rY   rZ   rI   r   r   rr   r   r   r@   rs   r   r   rd   r   rt      s    
,rt   c                       D   e Zd Zdef fddZdeeef deeef fddZ  ZS )ImageDenoisePreprocessorru   c                    :   t  j|i | || _ddlm} |ddgdg| _dS @

        Args:
            model_dir (str): model path
        r   )Filterr   target)reserved_keysNra   r   ru   commonr   _transformsr   ru   ry   rc   r   rd   r   r   r         z!ImageDenoisePreprocessor.__init__re   rF   c                 C      | j D ]}||}q|S zprocess the raw input data

        Args:
            data Dict[str, Any]

        Returns:
            Dict[str, Any]: the preprocessed data
        r   r   re   tr   r   r   r@         
	
z!ImageDenoisePreprocessor.__call__	rC   rY   rZ   rI   r   r   r   r@   rs   r   r   rd   r   r}          *r}   c                       r|   )ImageDeblurPreprocessorru   c                    r~   r   r   r   rd   r   r   r      r   z ImageDeblurPreprocessor.__init__re   rF   c                 C   r   r   r   r   r   r   r   r@     r   z ImageDeblurPreprocessor.__call__r   r   r   rd   r   r      r   r   c                       r|   )$ImagePortraitEnhancementPreprocessorru   c                    rv   r   Nrw   rx   rd   r   r   r   !     
z-ImagePortraitEnhancementPreprocessor.__init__re   rF   c                 C   rz   r   r   r{   r   r   r   r@   *     	z-ImagePortraitEnhancementPreprocessor.__call__r   r   r   rd   r   r     s    *	r   c                       sL   e Zd Z fddZdd Zdd Zeeedee	e
f fdd	Z  ZS )
%ImageInstanceSegmentationPreprocessorc                    s   t  j|i | |dd| _|dd| _|dd| _g | _g | _ddlm	} | jdurJt
| jtr:| jg| _| jD ]}||}| j| q=| jdurjt
| jtrZ| jg| _| jD ]}||}| j| q]dS dS )zKimage instance segmentation preprocessor in the fine-tune scenario
        trainingTtrainNvalr   )build_preprocess_transform)ra   r   popr   preprocessor_train_cfgpreprocessor_test_cfgtrain_transformstest_transforms9modelscope.models.cv.image_instance_segmentation.datasetsr   r#   r$   append)r   ry   rc   r   cfg	transformrd   r   r   r   ;  s*   





z.ImageInstanceSegmentationPreprocessor.__init__c                 C   
   d| _ d S NTr   r   r   r   r   r   Y     z+ImageInstanceSegmentationPreprocessor.trainc                 C   r   NFr   r   r   r   r   eval]  r   z*ImageInstanceSegmentationPreprocessor.evalr>   c                 C   s8   | j r| j}n| j}|D ]}||}|du r dS q|S zprocess the raw input data

        Args:
            results (dict): Result dict from loading pipeline.

        Returns:
            Dict[str, Any] | None: the preprocessed data
        N)r   r   r   )r   r>   
transformsr   r   r   r   r@   a  s   z.ImageInstanceSegmentationPreprocessor.__call__)rC   rY   rZ   r   r   r   r   rr   r   rI   r   r@   rs   r   r   rd   r   r   6  s     r   c                       r|   )VideoSummarizationPreprocessorru   c                    rv   r   rw   rx   rd   r   r   r     r   z'VideoSummarizationPreprocessor.__init__re   rF   c                 C   rz   r   r   r{   r   r   r   r@     r   z'VideoSummarizationPreprocessor.__call__r   r   r   rd   r   r   {  s    *	r   c                       sB   e Zd Z fddZdd Zdd Zdeeef fdd	Z	  Z
S )
%ImageClassificationBypassPreprocessorc                    s@   t  j|i | |dd| _|dd| _|dd| _dS )zKimage classification bypass preprocessor in the fine-tune scenario
        r   Tr   Nr   )ra   r   r   r   r   preprocessor_val_cfg)r   ry   rc   rd   r   r   r     s   z.ImageClassificationBypassPreprocessor.__init__c                 C   r   r   r   r   r   r   r   r     r   z+ImageClassificationBypassPreprocessor.trainc                 C   r   r   r   r   r   r   r   r     r   z*ImageClassificationBypassPreprocessor.evalr>   c                 C   s   dS r   r   )r   r>   r   r   r   r@     r   z.ImageClassificationBypassPreprocessor.__call__)rC   rY   rZ   r   r   r   r   rI   r   r@   rs   r   r   rd   r   r     s
    	r   )/r-   typingr   r   r   r   numpyrJ   rM   r   r   r   modelscope.fileior   modelscope.metainfor	   modelscope.pipeline_inputsr
   modelscope.utils.constantr   modelscope.utils.type_assertr   baser   builderr   register_modulecvrL   r   rI   %object_detection_tinynas_preprocessorr_    image_color_enhance_preprocessorrt   image_denoise_preprocessorr}   image_deblur_preprocessorr   'image_portrait_enhancement_preprocessorr   (image_instance_segmentation_preprocessorr    video_summarization_preprocessorr   (image_classification_bypass_preprocessorr   r   r   r   r   <module>   sh   i
/B