a
    d]                     @   sP  d dl Z d dlmZmZmZmZmZmZ d dlZ	d dl
Z
d dlmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZ e rd dlZd dlZe e ej!j"e dkrej#j$Z%nej#Z%ere rd dl&Z&ede	j'd	ed ee	j' ed	 f Z(G d
d deZ)dd Z*dd Z+dd Z,dd Z-d1e.ee( dddZ/e	j'dddZ0e	j'e)dddZ1e	j'e.dddZ2d2e	j'e)ee.e.f d d!d"Z3ee4eeef f e5d#d$d%Z6ee4eeef f e5d#d&d'Z7eee4eeef f  e5d(d)d*Z8eee4eeef f  e5d(d+d,Z9ee4df ddd-d.Z:G d/d0 d0Z;dS )3    N)TYPE_CHECKINGDictIterableListTupleUnion)version   )ExplicitEnumis_jax_tensoris_tf_tensoris_torch_availableis_torch_tensoris_vision_availablerequires_backendsto_numpy)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDIMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDOPENAI_CLIP_MEANOPENAI_CLIP_STDz9.1.0zPIL.Image.Imageztorch.Tensorc                   @   s   e Zd ZdZdZdS )ChannelDimensionZchannels_firstZchannels_lastN)__name__
__module____qualname__FIRSTLAST r   r   a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/image_utils.pyr   >   s   r   c                 C   s   t  ot| tjjS N)r   
isinstancePILImageimgr   r   r   is_pil_imageC   s    r&   c                 C   s8   t  rt| tjjp6t| tjp6t| p6t| p6t| S r    )	r   r!   r"   r#   npndarrayr   r   r   r$   r   r   r   is_valid_imageG   s    
r)   c                 C   s8   t | ttfr(| D ]}t|s dS qnt| s4dS dS )NFT)r!   listtuplevalid_imagesr)   )Zimgsr%   r   r   r   r,   Q   s    
r,   c                 C   s   t | ttfrt| d S dS )Nr   F)r!   r*   r+   r)   r$   r   r   r   
is_batched]   s    r-      )expected_ndimsreturnc                 C   s   t | r| S t| tjjr | gS t| rx| j|d kr@t| } n4| j|krR| g} n"td|d  d| d| j d| S tdt|  ddS )	a  
    Ensure that the input is a list of images. If the input is a single image, it is converted to a list of length 1.
    If the input is a batch of images, it is converted to a list of images.

    Args:
        images (`ImageInput`):
            Image of images to turn into a list of images.
        expected_ndims (`int`, *optional*, defaults to 3):
            Expected number of dimensions for a single input image. If the input image has a different number of
            dimensions, an error is raised.
    r	   z%Invalid image shape. Expected either z or z dimensions, but got z dimensions.ztInvalid image type. Expected either PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray, but got .N)	r-   r!   r"   r#   r)   ndimr*   
ValueErrortype)Zimagesr/   r   r   r   make_list_of_imagesc   s(    

r5   )r0   c                 C   s@   t | stdt|  t r8t| tjjr8t| S t	| S )NzInvalid image type: )
r)   r3   r4   r   r!   r"   r#   r'   arrayr   r$   r   r   r   to_numpy_array   s
    
r7   )imager0   c                 C   sl   | j dkrd\}}n$| j dkr(d\}}ntd| j  | j| dv rLtjS | j| dv r`tjS tddS )z
    Infers the channel dimension format of `image`.

    Args:
        image (`np.ndarray`):
            The image to infer the channel dimension of.

    Returns:
        The channel dimension of the image.
    r.   )r         r	   r.   z(Unsupported number of image dimensions: z(Unable to infer channel dimension formatN)r2   r3   shaper   r   r   )r8   Z	first_dimZlast_dimr   r   r   infer_channel_dimension_format   s    



r=   c                 C   sB   t | }|tjkr| jd S |tjkr0| jd S td| dS )z
    Returns the channel dimension axis of the image.

    Args:
        image (`np.ndarray`):
            The image to get the channel dimension axis of.

    Returns:
        The channel dimension axis of the image.
    r.   r	   Unsupported data format: N)r=   r   r   r2   r   r3   r8   channel_dimr   r   r   get_channel_dimension_axis   s    



rA   )r8   r@   r0   c                 C   s^   |du rt | }|tjkr.| jd | jd fS |tjkrL| jd | jd fS td| dS )a  
    Returns the (height, width) dimensions of the image.

    Args:
        image (`np.ndarray`):
            The image to get the dimensions of.
        channel_dim (`ChannelDimension`, *optional*):
            Which dimension the channel dimension is in. If `None`, will infer the channel dimension from the image.

    Returns:
        A tuple of the image's height and width.
    Nr>   )r=   r   r   r<   r   r3   r?   r   r   r   get_image_size   s    

rE   )
annotationr0   c                 C   sV   t | trRd| v rRd| v rRt | d ttfrRt| d dksNt | d d trRdS dS )Nimage_idannotationsr   TFr!   dictr*   r+   lenrF   r   r   r   "is_valid_annotation_coco_detection   s    rM   c                 C   s^   t | trZd| v rZd| v rZd| v rZt | d ttfrZt| d dksVt | d d trZdS dS )NrG   Zsegments_info	file_namer   TFrI   rL   r   r   r   !is_valid_annotation_coco_panoptic   s     	rO   )rH   r0   c                 C   s   t dd | D S )Nc                 s   s   | ]}t |V  qd S r    )rM   .0annr   r   r   	<genexpr>       z3valid_coco_detection_annotations.<locals>.<genexpr>allrH   r   r   r    valid_coco_detection_annotations   s    rX   c                 C   s   t dd | D S )Nc                 s   s   | ]}t |V  qd S r    )rO   rP   r   r   r   rS      rT   z2valid_coco_panoptic_annotations.<locals>.<genexpr>rU   rW   r   r   r   valid_coco_panoptic_annotations   s    rY   c                 C   s   t tdg t| trp| ds*| drDtjtj	| ddj
} qtj| r^tj| } qtd|  dnt| tjjr| } ntdtj| } | d	} | S )
z
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.

    Returns:
        `PIL.Image.Image`: A PIL Image.
    Zvisionzhttp://zhttps://T)streamzIIncorrect path or url, URLs must start with `http://` or `https://`, and z is not a valid pathzdIncorrect format used for image. Should be an url linking to an image, a local path, or a PIL image.RGB)r   
load_imager!   str
startswithr"   r#   openrequestsgetrawospathisfiler3   ZImageOpsZexif_transposeconvert)r8   r   r   r   r\      s"    


r\   c                   @   s   e Zd ZdZdd ZdddZdd Zeje	e
ef ejd	d
dZdddZdd ZdddZd ddZdd Zdd Zd!ddZdS )"ImageFeatureExtractionMixinzD
    Mixin that contain utilities for preparing image features.
    c                 C   s4   t |tjjtjfs0t|s0tdt| dd S )Nz	Got type zS which is not supported, only `PIL.Image.Image`, `np.array` and `torch.Tensor` are.)r!   r"   r#   r'   r(   r   r3   r4   selfr8   r   r   r   _ensure_format_supported(  s    z4ImageFeatureExtractionMixin._ensure_format_supportedNc                 C   s   |  | t|r| }t|tjr|du r@t|jd tj}|jdkrf|j	d dv rf|
ddd}|rr|d }|tj}tj|S |S )a"  
        Converts `image` to a PIL Image. Optionally rescales it and puts the channel dimension back as the last axis if
        needed.

        Args:
            image (`PIL.Image.Image` or `numpy.ndarray` or `torch.Tensor`):
                The image to convert to the PIL Image format.
            rescale (`bool`, *optional*):
                Whether or not to apply the scaling factor (to make pixel values integers between 0 and 255). Will
                default to `True` if the image type is a floating type, `False` otherwise.
        Nr   r.   r;   r	   r9      )rj   r   numpyr!   r'   r(   flatZfloatingr2   r<   	transposeastypeZuint8r"   r#   Z	fromarray)ri   r8   rescaler   r   r   to_pil_image/  s    
z(ImageFeatureExtractionMixin.to_pil_imagec                 C   s&   |  | t|tjjs|S |dS )z
        Converts `PIL.Image.Image` to RGB format.

        Args:
            image (`PIL.Image.Image`):
                The image to convert.
        r[   )rj   r!   r"   r#   rf   rh   r   r   r   convert_rgbM  s    
z'ImageFeatureExtractionMixin.convert_rgb)r8   scaler0   c                 C   s   |  | || S )z7
        Rescale a numpy image by scale amount
        )rj   )ri   r8   rs   r   r   r   rp   [  s    
z#ImageFeatureExtractionMixin.rescaleTc                 C   s   |  | t|tjjr"t|}t|r2| }|du rLt|jd tj	n|}|rh| 
|tjd}|r|jdkr|ddd}|S )a  
        Converts `image` to a numpy array. Optionally rescales it and puts the channel dimension as the first
        dimension.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to convert to a NumPy array.
            rescale (`bool`, *optional*):
                Whether or not to apply the scaling factor (to make pixel values floats between 0. and 1.). Will
                default to `True` if the image is a PIL Image or an array/tensor of integers, `False` otherwise.
            channel_first (`bool`, *optional*, defaults to `True`):
                Whether or not to permute the dimensions of the image to put the channel dimension first.
        Nr   p?r.   r9   r	   )rj   r!   r"   r#   r'   r6   r   rl   rm   integerrp   ro   float32r2   rn   )ri   r8   rp   channel_firstr   r   r   r7   b  s    

z*ImageFeatureExtractionMixin.to_numpy_arrayc                 C   sB   |  | t|tjjr|S t|r0|d}ntj|dd}|S )z
        Expands 2-dimensional `image` to 3 dimensions.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to expand.
        r   )Zaxis)rj   r!   r"   r#   r   Z	unsqueezer'   expand_dimsrh   r   r   r   rx     s    
z'ImageFeatureExtractionMixin.expand_dimsFc                 C   s@  |  | t|tjjr(| j|dd}n>|rft|tjrN| |tj	d}nt
|rf| | d}t|tjrt|tjst||j}t|tjst||j}n<t
|rddl}t||js||}t||js||}|jdkr0|jd dv r0||ddddf  |ddddf  S || | S dS )a  
        Normalizes `image` with `mean` and `std`. Note that this will trigger a conversion of `image` to a NumPy array
        if it's a PIL Image.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to normalize.
            mean (`List[float]` or `np.ndarray` or `torch.Tensor`):
                The mean (per channel) to use for normalization.
            std (`List[float]` or `np.ndarray` or `torch.Tensor`):
                The standard deviation (per channel) to use for normalization.
            rescale (`bool`, *optional*, defaults to `False`):
                Whether or not to rescale the image to be between 0 and 1. If a PIL image is provided, scaling will
                happen automatically.
        T)rp   rt   r   Nr.   r;   )rj   r!   r"   r#   r7   r'   r(   rp   ro   rv   r   floatr6   ZdtypetorchZTensorZtensorr2   r<   )ri   r8   meanZstdrp   rz   r   r   r   	normalize  s,    


(z%ImageFeatureExtractionMixin.normalizec                 C   sT  |dur|nt j}| | t|tjjs4| |}t|trFt|}t|t	s^t
|dkrF|rt|t	rt||fn|d |d f}n|j\}}||kr||fn||f\}}	t|t	r|n|d }
||
kr|S |
t	|
|	 |  }}|dur,||
krtd| d| ||kr,t	|| | | }}||kr>||fn||f}|j||dS )a  
        Resizes `image`. Enforces conversion of input to PIL.Image.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to resize.
            size (`int` or `Tuple[int, int]`):
                The size to use for resizing the image. If `size` is a sequence like (h, w), output size will be
                matched to this.

                If `size` is an int and `default_to_square` is `True`, then image will be resized to (size, size). If
                `size` is an int and `default_to_square` is `False`, then 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).
            resample (`int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                The filter to user for resampling.
            default_to_square (`bool`, *optional*, defaults to `True`):
                How to convert `size` when it is a single int. If set to `True`, the `size` will be converted to a
                square (`size`,`size`). If set to `False`, will replicate
                [`torchvision.transforms.Resize`](https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.Resize)
                with support for resizing only the smallest edge and providing an optional `max_size`.
            max_size (`int`, *optional*, defaults to `None`):
                The maximum allowed for the longer edge of the resized image: if the longer edge of the image is
                greater than `max_size` after being resized according to `size`, then the image is resized again so
                that the longer edge is equal to `max_size`. As a result, `size` might be overruled, i.e the smaller
                edge may be shorter than `size`. Only used if `default_to_square` is `False`.

        Returns:
            image: A resized `PIL.Image.Image`.
        Nr	   r   zmax_size = zN must be strictly greater than the requested size for the smaller edge size = )resample)PILImageResamplingZBILINEARrj   r!   r"   r#   rq   r*   r+   intrK   sizer3   resize)ri   r8   r   r}   Zdefault_to_squaremax_sizewidthheightshortlongZrequested_new_shortZ	new_shortZnew_longr   r   r   r     s4    


$



z"ImageFeatureExtractionMixin.resizec                 C   s  |  | t|ts||f}t|s0t|tjrp|jdkrD| |}|jd dv r`|jdd n|jdd }n|j	d |j	d f}|d |d  d }||d  }|d |d  d }||d  }t|t
jjr|||||fS |jd dv rdnd}|s4t|tjr|ddd}t|r4|ddd}|dkrz||d krz|dkrz||d krz|d||||f S |jdd	 t|d |d t|d |d f }	t|tjrtj||	d
}
nt|r||	}
|	d	 |d  d }||d  }|	d |d  d }||d  }||
d||||f< ||7 }||7 }||7 }||7 }|
dtd|t|
jd	 |td|t|
jd |f }
|
S )a  
        Crops `image` to the given size using a center crop. Note that if the image is too small to be cropped to the
        size given, it will be padded (so the returned result has the size asked).

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor` of shape (n_channels, height, width) or (height, width, n_channels)):
                The image to resize.
            size (`int` or `Tuple[int, int]`):
                The size to which crop the image.

        Returns:
            new_image: A center cropped `PIL.Image.Image` or `np.ndarray` or `torch.Tensor` of shape: (n_channels,
            height, width).
        r9   r   r;   r	   NTF.rB   )r<   rC   )rj   r!   r+   r   r'   r(   r2   rx   r<   r   r"   r#   Zcroprn   ZpermutemaxZ
zeros_likeZ	new_zerosmin)ri   r8   r   Zimage_shapetopZbottomleftrightrw   Z	new_shapeZ	new_imageZtop_padZ
bottom_padZleft_padZ	right_padr   r   r   center_crop  sP    



,
02

4z'ImageFeatureExtractionMixin.center_cropc                 C   s>   |  | t|tjjr"| |}|dddddddf S )a  
        Flips the channel order of `image` from RGB to BGR, or vice versa. Note that this will trigger a conversion of
        `image` to a NumPy array if it's a PIL Image.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image whose color channels to flip. If `np.ndarray` or `torch.Tensor`, the channel dimension should
                be first.
        NrC   )rj   r!   r"   r#   r7   rh   r   r   r   flip_channel_orderR  s    


z.ImageFeatureExtractionMixin.flip_channel_orderr   c                 C   sL   |dur|nt jj}| | t|t jjs6| |}|j||||||dS )a  
        Returns a rotated copy of `image`. This method returns a copy of `image`, rotated the given number of degrees
        counter clockwise around its centre.

        Args:
            image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
                The image to rotate. If `np.ndarray` or `torch.Tensor`, will be converted to `PIL.Image.Image` before
                rotating.

        Returns:
            image: A rotated `PIL.Image.Image`.
        N)r}   expandcenter	translate	fillcolor)r"   r#   ZNEARESTrj   r!   rq   rotate)ri   r8   Zangler}   r   r   r   r   r   r   r   r   c  s    

z"ImageFeatureExtractionMixin.rotate)N)NT)F)NTN)Nr   NNN)r   r   r   __doc__rj   rq   rr   r'   r(   r   ry   r   rp   r7   rx   r|   r   r   r   r   r   r   r   r   rg   #  s   

 
.
CKrg   )r.   )N)<rc   typingr   r   r   r   r   r   rl   r'   r`   	packagingr   utilsr
   r   r   r   r   r   r   r   Zutils.constantsr   r   r   r   r   r   Z	PIL.Imager"   ZPIL.ImageOpsparse__version__base_versionr#   Z
Resamplingr~   rz   r(   Z
ImageInputr   r&   r)   r,   r-   r   r5   r7   r=   rA   rE   r]   boolrM   rO   rX   rY   r\   rg   r   r   r   r   <module>   sD    (
 


'	  $$#