o
    ॵiQ                     @   sL  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZejej	ej
ejdZejejejejejdZdd e D Zdd Zdd	 Zd
d Zdd ZejfddZd)ddZdd Zdd ZejfddZdd ZG dd deZ G dd deZ!G dd  d eZ"G d!d" d"eZ#G d#d$ d$eZ$G d%d& d&eZ%G d'd( d(eZ&dS )*    Nconstantedgereflect	symmetric)nearestbilinearareabicubiclanczosc                 C   s   i | ]\}}||qS  r   ).0kvr   r   ^/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/preprocessors/cv/cv2_transforms.py
<dictcomp>   s    r   c                 C   s   t | o
|  dkS )N   )torch	is_tensor
ndimensionimgr   r   r   _is_tensor_image"      r   c                 C   s   t | tjo
| jdv S )N>      r   )
isinstancenpndarrayndimr   r   r   r   _is_numpy_image&   r   r   c                 C   sT   t | stdt| t| d}t|tjs!|j	tj
kr(| dS |S )zConvert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
    See ``ToTensor`` for more details.
    Args:
        pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
    Returns:
        Tensor: Converted image.
    zpic should be ndarray. Got {})r   r         )r   	TypeErrorformattyper   
from_numpy	transposer   
ByteTensordtypeuint8floatdiv)picr   r   r   r   	to_tensor*   s   r-   c                 C   s<   t | stdt| ||D ]\}}}||| q| S )a  Normalize a tensor image with mean and standard deviation.
    .. note::
        This transform acts in-place, i.e., it mutates the input tensor.
    See :class:`~torchvision.transforms.Normalize` for more details.
    Args:
        tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
        mean (sequence): Sequence of means for each channel.
        std (sequence): Sequence of standard deviations for each channely.
    Returns:
        Tensor: Normalized Tensor image.
    ztensor is not a torch image.)r   r"   zipsub_div_)tensormeanstdtmsr   r   r   	normalize>   s
   r7   c                 C   s  t | stdt| t|ts&t|tjjrt	|dks&td|| j
d | j
d }}t|trb||kr>||ksF||krH||krH| S ||k rW|}t|| | }n|}t|| | }n	|d |d }}tj| ||f|d}| j
d dkr|ddddtjf S |S )a  Resize the input numpy ndarray to the given size.
    Args:
        img (numpy ndarray): Image to be resized.
        size (sequence or int): Desired output size. If size is a sequence like
            (h, w), the output size will be matched to this. If size is an int,
            the smaller edge of the image will be matched to this number maintaing
            the aspect ratio. i.e, if height > width, then image will be rescaled to
            :math:`\left(\text{size} \times \frac{\text{height}}{\text{width}}, \text{size}\right)`
        interpolation (int, optional): Desired interpolation. Default is
            ``cv2.INTER_LINEAR``
    Returns:
        PIL Image: Resized image.
    !img should be numpy image. Got {}r   zGot inappropriate size arg: {}r   r    )dsizeinterpolationN)r   r"   r#   r$   r   intcollectionsabcIterablelenshapecv2resizer   newaxis)r   sizer:   hwowohoutputr   r   r   rB   S   s*   

 rB   r   c              	   C   sz  t | stdt| t|tjttfstdt|tjt	tfs'tdt|t	s0tdt|t
jrGt|dvrGtddt| |dv sOJ d	t|tr\| } } }}t|t
jrtt|d
krt|d  }}|d  }}t|t
jrt|dkr|d }|d }|d
 }|d }| jd
 dkrtj| ||||t| |dddddtjf S tj| ||||t| |dS )a  Pad the given numpy ndarray on all sides with specified padding mode and fill value.
    Args:
        img (numpy ndarray): image to be padded.
        padding (int or tuple): Padding on each border. If a single int is provided this
            is used to pad all borders. If tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a tuple of length 4 is provided
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
            - constant: pads with a constant value, this value is specified with fill
            - edge: pads with the last value on the edge of the image
            - reflect: pads with reflection of image (without repeating the last value on the edge)
                       padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                       will result in [3, 2, 1, 2, 3, 4, 3, 2]
            - symmetric: pads with reflection of image (repeating the last value on the edge)
                         padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                         will result in [2, 1, 1, 2, 3, 4, 4, 3]
    Returns:
        Numpy image: padded image.
    z#img should be numpy ndarray. Got {}zGot inappropriate padding argzGot inappropriate fill argz"Got inappropriate padding_mode arg)r      z9Padding must be an int or a 2, or 4 element tuple, not a z{} element tupler   zBPadding mode should be either constant, edge, reflect or symmetricr   r   r    rJ   r   )topbottomleftright
borderTypevalueN)r   r"   r#   r$   r   numbersNumbertupleliststrr<   Sequencer?   
ValueErrorr;   r@   rA   copyMakeBorder_cv2_pad_to_strr   rC   )r   paddingfillpadding_modepad_left	pad_rightpad_top
pad_bottomr   r   r   padz   sj   


	ra   c                 C   s<   t | stdt| | ||| ||| ddf S )a!  Crop the given PIL Image.
    Args:
        img (numpy ndarray): Image to be cropped.
        i: Upper pixel coordinate.
        j: Left pixel coordinate.
        h: Height of the cropped image.
        w: Width of the cropped image.
    Returns:
        numpy ndarray: Cropped image.
    r8   N)r   r"   r#   r$   )r   ijrE   rF   r   r   r   crop   s   "rd   c                 C   sn   t |tjrt|t|f}| jdd \}}|\}}tt|| d }tt|| d }t| ||||S )Nr   r   g       @)r   rQ   rR   r;   r@   roundrd   r   output_sizerE   rF   thtwrb   rc   r   r   r   center_crop   s   rj   c                 C   s2   t | sJ dt| ||||} t| ||d} | S )aC  Crop the given numpy ndarray and resize it to desired size.
    Notably used in :class:`~torchvision.transforms.RandomResizedCrop`.
    Args:
        img (numpy ndarray): Image to be cropped.
        i: Upper pixel coordinate.
        j: Left pixel coordinate.
        h: Height of the cropped image.
        w: Width of the cropped image.
        size (sequence or int): Desired output size. Same semantics as ``scale``.
        interpolation (int, optional): Desired interpolation. Default is
            ``cv2.INTER_CUBIC``.
    Returns:
        PIL Image: Cropped image.
    zimg should be numpy image)r:   )r   rd   rB   )r   rb   rc   rE   rF   rD   r:   r   r   r   resized_crop   s   rk   c                 C   sT   t | stdt| | jd dkr$t| dddddtjf S t| dS )zHorizontally flip the given numpy ndarray.
    Args:
        img (numpy ndarray): image to be flipped.
    Returns:
        numpy ndarray:  Horizontally flipped image.
    r8   r   r    N)	r   r"   r#   r$   r@   rA   flipr   rC   r   r   r   r   hflip   s
    rm   c                   @   s    e Zd ZdZdd Zdd ZdS )ToTensorzConvert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
    Converts a PIL Image or numpy.ndarray (H x W x C) in the range
    [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
    c                 C   s   t |S )z
        Args:
            pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
        Returns:
            Tensor: Converted image.
        )r-   )selfr,   r   r   r   __call__  s   zToTensor.__call__c                 C   s   | j jd S )Nz())	__class____name__ro   r   r   r   __repr__  s   zToTensor.__repr__N)rr   
__module____qualname____doc__rp   rt   r   r   r   r   rn     s    	rn   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )		Normalizea  Normalize a tensor image with mean and standard deviation.
    Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
    will normalize each channel of the input ``torch.*Tensor`` i.e.
    ``input[channel] = (input[channel] - mean[channel]) / std[channel]``
    .. note::
        This transform acts in-place, i.e., it mutates the input tensor.
    Args:
        mean (sequence): Sequence of means for each channel.
        std (sequence): Sequence of standard deviations for each channel.
    c                 C   s   || _ || _d S N)r2   r3   )ro   r2   r3   r   r   r   __init__!  s   
zNormalize.__init__c                 C      t || j| jS )z
        Args:
            tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
        Returns:
            Tensor: Normalized Tensor image.
        )r7   r2   r3   )ro   r1   r   r   r   rp   %     zNormalize.__call__c                 C      | j jd| j| j S )Nz(mean={0}, std={1}))rq   rr   r#   r2   r3   rs   r   r   r   rt   .     
zNormalize.__repr__Nrr   ru   rv   rw   r{   rp   rt   r   r   r   r   ry     s
    	ry   c                   @   s.   e Zd ZdZejfddZdd Zdd ZdS )	Resizea  Resize the input numpy ndarray to the given size.
    Args:
        size (sequence or int): Desired output size. If size is a sequence like
            (h, w), output size will be matched to this. If size is an int,
            smaller edge of the image will be matched to this number.
            i.e, if height > width, then image will be rescaled to
            (size * height / width, size)
        interpolation (int, optional): Desired interpolation. Default is
            ``cv2.INTER_CUBIC``, bicubic interpolation
    c                 C   s`   t |tr	|| _n"t |tjjr$t|dkr$t|tkr t	|}|| _nt
d||| _d S )Nr   zUnknown inputs for size: {})r   r;   rD   r<   r=   r>   r?   r$   rT   rS   rW   r#   r:   )ro   rD   r:   r   r   r   r{   ?  s   

zResize.__init__c                 C   r|   )z
        Args:
            img (numpy ndarray): Image to be scaled.
        Returns:
            numpy ndarray: Rescaled image.
        )rB   rD   r:   ro   r   r   r   r   rp   K  r}   zResize.__call__c                 C   s    t | j }| jjd| j| S )Nz(size={0}, interpolation={1}))_cv2_interpolation_from_strr:   rq   rr   r#   rD   )ro   interpolate_strr   r   r   rt   T  s   

zResize.__repr__N)	rr   ru   rv   rw   rA   INTER_LINEARr{   rp   rt   r   r   r   r   r   3  s
    	r   c                   @   rx   )	
CenterCropzCrops the given numpy ndarray at the center.
    Args:
        size (sequence or int): Desired output size of the crop. If size is an
            int instead of sequence like (h, w), a square crop (size, size) is
            made.
    c                 C   s,   t |tjrt|t|f| _d S || _d S rz   )r   rQ   rR   r;   rD   )ro   rD   r   r   r   r{   b  s   
zCenterCrop.__init__c                 C   s   t || jS )
        Args:
            img (numpy ndarray): Image to be cropped.
        Returns:
            numpy ndarray: Cropped image.
        )rj   rD   r   r   r   r   rp   h  s   zCenterCrop.__call__c                 C      | j jd| j S )Nz
(size={0}))rq   rr   r#   rD   rs   r   r   r   rt   q     zCenterCrop.__repr__Nr   r   r   r   r   r   Z  s
    	r   c                   @   s>   e Zd ZdZ				dddZedd	 Zd
d Zdd ZdS )
RandomCropa  Crop the given numpy ndarray at a random location.
    Args:
        size (sequence or int): Desired output size of the crop. If size is an
            int instead of sequence like (h, w), a square crop (size, size) is
            made.
        padding (int or sequence, optional): Optional padding on each border
            of the image. Default is None, i.e no padding. If a sequence of length
            4 is provided, it is used to pad left, top, right, bottom borders
            respectively. If a sequence of length 2 is provided, it is used to
            pad left/right, top/bottom borders, respectively.
        pad_if_needed (boolean): It will pad the image if smaller than the
            desired size to avoid raising an exception.
        fill: Pixel fill value for constant fill. Default is 0. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
            This value is only used when the padding_mode is constant
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
             - constant: pads with a constant value, this value is specified with fill
             - edge: pads with the last value on the edge of the image
             - reflect: pads with reflection of image (without repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                will result in [3, 2, 1, 2, 3, 4, 3, 2]
             - symmetric: pads with reflection of image (repeating the last value on the edge)
                padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                will result in [2, 1, 1, 2, 3, 4, 4, 3]
    NFr   r   c                 C   sB   t |tjrt|t|f| _n|| _|| _|| _|| _|| _d S rz   )	r   rQ   rR   r;   rD   rZ   pad_if_neededr[   r\   )ro   rD   rZ   r   r[   r\   r   r   r   r{     s   
zRandomCrop.__init__c                 C   sb   | j dd \}}|\}}||kr||krdd||fS td|| }td|| }||||fS )a   Get parameters for ``crop`` for a random crop.
        Args:
            img (numpy ndarray): Image to be cropped.
            output_size (tuple): Expected output size of the crop.
        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
        r   r   )r@   randomrandintrf   r   r   r   
get_params  s   	zRandomCrop.get_paramsc                 C   s   | j durt|| j | j| j}| jr/|jd | jd k r/t|| jd |jd  df| j| j}| jrO|jd | jd k rOt|d| jd |jd  f| j| j}| || j\}}}}t|||||S )r   Nr    r   )	rZ   ra   r[   r\   r   r@   rD   r   rd   ro   r   rb   rc   rE   rF   r   r   r   rp     s   
zRandomCrop.__call__c                 C   r~   )Nz(size={0}, padding={1}))rq   rr   r#   rD   rZ   rs   r   r   r   rt     r   zRandomCrop.__repr__)NFr   r   )	rr   ru   rv   rw   r{   staticmethodr   rp   rt   r   r   r   r   r   u  s    

r   c                   @   s>   e Zd ZdZddejfddZedd Zdd	 Z	d
d Z
dS )RandomResizedCropa>  Crop the given numpy ndarray to random size and aspect ratio.
    A crop of random size (default: of 0.08 to 1.0) of the original size and a random
    aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
    is finally resized to given size.
    This is popularly used to train the Inception networks.
    Args:
        size: expected output size of each edge
        scale: range of size of the origin size cropped
        ratio: range of aspect ratio of the origin aspect ratio cropped
        interpolation: Default: cv2.INTER_CUBIC
    )g{Gz?g      ?)g      ?gUUUUUU?c                 C   s    ||f| _ || _|| _|| _d S rz   )rD   r:   scaleratio)ro   rD   r   r   r:   r   r   r   r{     s   

zRandomResizedCrop.__init__c                 C   s  t dD ]e}| jd | jd  }tj| | }tj| }ttt|| }ttt|| }t dk r=||}}|| jd kri|| jd kritd| jd | }	td| jd | }
|	|
||f  S qt	| jd | jd }| jd | d }	| jd | d }
|	|
||fS )a  Get parameters for ``crop`` for a random sized crop.
        Args:
            img (numpy ndarray): Image to be cropped.
            scale (tuple): range of size of the origin size cropped
            ratio (tuple): range of aspect ratio of the origin aspect ratio cropped
        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for a random
                sized crop.
        
   r   r          ?r   )
ranger@   r   uniformr;   re   mathsqrtr   min)r   r   r   attemptr	   target_areaaspect_ratiorF   rE   rb   rc   r   r   r   r     s"   

zRandomResizedCrop.get_paramsc                 C   s2   |  || j| j\}}}}t|||||| j| jS )z
        Args:
            img (numpy ndarray): Image to be cropped and resized.
        Returns:
            numpy ndarray: Randomly cropped and resized image.
        )r   r   r   rk   rD   r:   r   r   r   r   rp     s   zRandomResizedCrop.__call__c                 C   sl   t | j }| jjd| j }|dtdd | jD 7 }|dtdd | jD 7 }|d|7 }|S )Nz	(size={0}z, scale={0}c                 s       | ]}t |d V  qdS rJ   Nre   )r   r6   r   r   r   	<genexpr>      z-RandomResizedCrop.__repr__.<locals>.<genexpr>z, ratio={0}c                 s   r   r   r   )r   rr   r   r   r     r   z, interpolation={0}))	r   r:   rq   rr   r#   rD   rS   r   r   )ro   r   format_stringr   r   r   rt     s   
zRandomResizedCrop.__repr__N)rr   ru   rv   rw   rA   r   r{   r   r   rp   rt   r   r   r   r   r     s    


 
r   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )RandomHorizontalFlipzHorizontally flip the given PIL Image randomly with a given probability.
    Args:
        p (float): probability of the image being flipped. Default value is 0.5
    r   c                 C   s
   || _ d S rz   )p)ro   r   r   r   r   r{      s   
zRandomHorizontalFlip.__init__c                 C   s   t   | jk rt|S |S )zrandom
        Args:
            img (numpy ndarray): Image to be flipped.
        Returns:
            numpy ndarray: Randomly flipped image.
        )r   r   rm   r   r   r   r   rp   #  s   zRandomHorizontalFlip.__call__c                 C   r   )Nz(p={}))rq   rr   r#   r   rs   r   r   r   rt   .  r   zRandomHorizontalFlip.__repr__N)r   r   r   r   r   r   r     s
    
r   )r   r   )'r<   r   rQ   r   rA   numpyr   r   BORDER_CONSTANTBORDER_REPLICATEBORDER_REFLECT_101BORDER_REFLECTrY   INTER_NEARESTr   
INTER_AREAINTER_CUBICINTER_LANCZOS4_cv2_interpolation_to_stritemsr   r   r   r-   r7   rB   ra   rd   rj   rk   rm   objectrn   ry   r   r   r   r   r   r   r   r   r   <module>   sL   
'H
'XM