
    #j                        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
mZ d dlZd dlZddlmZ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 dd	l m!Z!  e            rd dl"Z#d dl$Z#e#j%        j&        Z' e            r}d d
l(m)Z)m*Z* d dl+m,Z, d dl-m.Z. e'j/        e,j0        e'j1        e,j1        e'j2        e,j2        e'j3        e,j3        e'j4        e,j4        e'j5        e,j5        iZ6d e67                                D             Z8ni Z6i Z8 e            rd dl9Z9 ej:        e;          Z<edej=        de>d         e>ej=                 e>d         f         Z? G d de          Z@ G d de          ZAeBeCeDeCz  e>eB         z  f         ZEd ZF G d de          ZGd ZHd ZIde>fdZJd ZKd ZLd ZMdej=        deNfd ZOdZd"eDde>e?         fd#ZP	 dZde>e?         e?z  d"eDde?fd$ZQ	 dZde>e?         e?z  d"eDde>e?         fd%ZRdej=        fd&ZS	 d[dej=        d'eDeTeDd(f         z  dz  de@fd)ZUd[dej=        d*e@eCz  dz  deDfd+ZVd[dej=        d,e@dz  deTeDeDf         fd-ZWd.eTeDeDf         d/eDd0eDdeTeDeDf         fd1ZXd2ee
         de>e
         fd3ZYe@jZ        fde>edej=        f                  d*eCe@z  de>eD         fd4Z[d5eBeCe>eTz  f         deNfd6Z\d5eBeCe>eTz  f         deNfd7Z]d8eeBeCe>eTz  f                  deNfd9Z^d8eeBeCe>eTz  f                  deNfd:Z_	 d[deeCdf         d;e`dz  ddfd<Za e!d=>          	 d[deeCdf         d;e`dz  ddfd?            Zb	 d[dee>eTeCdf         d;e`dz  dede>d         e>e>d                  f         fd@Zc	 	 	 	 	 	 	 	 	 	 	 	 d\dAeNdz  dBe`dz  dCeNdz  dDe`e>e`         z  dz  dEe`e>e`         z  dz  dFeNdz  dGeBeCeDf         eDz  dz  dHeNdz  dIeBeCeDf         dz  dJeNdz  dKeBeCeDf         dz  dLedMdNeDf         dz  fdOZd G dP dQ          ZedReAdSeTeAd(f         d8e>eB         ddfdTZfdUe>eC         dVe>eC         fdWZg e             G dX dY                      ZhdS )]    N)Iterable)	dataclassfields)BytesIO)AnyUnion   )	ExplicitEnumis_numpy_arrayis_torch_availableis_torch_tensoris_torchvision_availableis_vision_availableloggingrequires_backendsto_numpy)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDIMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDOPENAI_CLIP_MEANOPENAI_CLIP_STD)requires)ImageReadModedecode_image)InterpolationMode)pil_to_tensorc                     i | ]\  }}||	S  r   ).0kvs      b/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/transformers/image_utils.py
<dictcomp>r$   C   s    &`&`&`1q!&`&`&`    zPIL.Image.Imageztorch.Tensorc                       e Zd ZdZdZdS )ChannelDimensionchannels_firstchannels_lastN)__name__
__module____qualname__FIRSTLASTr   r%   r#   r'   r'   U   s        EDDDr%   r'   c                       e Zd ZdZdZdS )AnnotationFormatcoco_detectioncoco_panopticN)r*   r+   r,   COCO_DETECTIONCOCO_PANOPTICr   r%   r#   r0   r0   Z   s        %N#MMMr%   r0   c                 \    t                      ot          | t          j        j                  S N)r   
isinstancePILImageimgs    r#   is_pil_imager<   b   s!      EZSY_%E%EEr%   c                       e Zd ZdZdZdZdS )	ImageTypepillowtorchnumpyN)r*   r+   r,   r8   TORCHNUMPYr   r%   r#   r>   r>   f   s        
CEEEEr%   r>   c                     t          |           rt          j        S t          |           rt          j        S t          |           rt          j        S t          dt          |                      )NzUnrecognized image type )	r<   r>   r8   r   rB   r   rC   
ValueErrortypeimages    r#   get_image_typerI   l   sg    E }u e 
=U==
>
>>r%   c                 \    t          |           pt          |           pt          |           S r6   )r<   r   r   r:   s    r#   is_valid_imagerK   v   s*    Ks 3 3Ks7K7KKr%   imagesc                 8    | ot          d | D                       S )Nc              3   4   K   | ]}t          |          V  d S r6   )rK   r    rH   s     r#   	<genexpr>z*is_valid_list_of_images.<locals>.<genexpr>{   s*      DDE.//DDDDDDr%   all)rL   s    r#   is_valid_list_of_imagesrS   z   s$    DcDDVDDDDDDr%   c                 ,   t          | d         t                    rd | D             S t          | d         t          j                  rt          j        | d          S t          | d         t
          j                  rt          j        | d          S d S )Nr   c                     g | ]	}|D ]}|
S r   r   )r    sublistitems      r#   
<listcomp>z$concatenate_list.<locals>.<listcomp>   s%    CCC7CC4CCCCr%   axis)dim)r7   listnpndarrayconcatenater@   Tensorcat)
input_lists    r#   concatenate_listrc   ~   s    *Q-&& ,CCJCCCC	JqM2:	.	. ,~jq1111	JqM5<	0	0 ,y++++, ,r%   c                     t          | t          t          f          r| D ]}t          |          s dS nt	          |           sdS dS )NFT)r7   r\   tuplevalid_imagesrK   )imgsr;   s     r#   rf   rf      sd    $u&&  	 	C$$ uu	 D!! u4r%   c                 h    t          | t          t          f          rt          | d                   S dS )Nr   F)r7   r\   re   rK   r:   s    r#   
is_batchedri      s/    #e}%% &c!f%%%5r%   rH   returnc                     | j         t          j        k    rdS t          j        |           dk    ot          j        |           dk    S )zV
    Checks to see whether the pixel values have already been rescaled to [0, 1].
    Fr   r	   )dtyper]   uint8minmaxrG   s    r#   is_scaled_imagerp      s?     {bhu 6%==A4"&--1"44r%      expected_ndimsc           	      F   t          |           r| S t          |           r| gS t          |           rP| j        |dz   k    rt	          |           } n0| j        |k    r| g} n!t          d|dz    d| d| j         d          | S t          dt          |            d          )a  
    Ensure that the output 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.z]Invalid image type. Expected either PIL.Image.Image, numpy.ndarray, or torch.Tensor, but got .)ri   r<   rK   ndimr\   rE   rF   rL   rr   s     r#   make_list_of_imagesrw      s     &  F xf ;.1,,,&\\FF[N**XFF.8J . .P^ . .K. . .   
whlmshthtwww  r%   c                 R   t          | t          t          f          r>t          d | D                       r%t          d | D                       rd | D             S t          | t          t          f          rWt	          |           rHt          | d                   s| d         j        |k    r| S | d         j        |dz   k    rd | D             S t          |           r:t          |           s| j        |k    r| gS | j        |dz   k    rt          |           S t          d|            )a  
    Ensure that the output is a flat list of images. If the input is a single image, it is converted to a list of length 1.
    If the input is a nested list of images, it is converted to a flat list of images.
    Args:
        images (`Union[list[ImageInput], ImageInput]`):
            The input image.
        expected_ndims (`int`, *optional*, defaults to 3):
            The expected number of dimensions for a single input image.
    Returns:
        list: A list of images or a 4d array of images.
    c              3   N   K   | ] }t          |t          t          f          V  !d S r6   r7   r\   re   r    images_is     r#   rP   z+make_flat_list_of_images.<locals>.<genexpr>   0      KK
8dE]33KKKKKKr%   c              3   :   K   | ]}t          |          p| V  d S r6   rS   r{   s     r#   rP   z+make_flat_list_of_images.<locals>.<genexpr>   2      YYh'11A\YYYYYYr%   c                     g | ]	}|D ]}|
S r   r   r    img_listr;   s      r#   rX   z,make_flat_list_of_images.<locals>.<listcomp>   s%    ???h??s????r%   r   r	   c                     g | ]	}|D ]}|
S r   r   r   s      r#   rX   z,make_flat_list_of_images.<locals>.<listcomp>   s%    CCCH(CC3CCCCCr%   z*Could not make a flat list of images from 	r7   r\   re   rR   rS   r<   ru   rK   rE   rv   s     r#   make_flat_list_of_imagesr      sX   " 	6D%=))@KKFKKKKK@ YYRXYYYYY@
 @?F????&4-(( D-DV-L-L Dq	"" 	fQin&F&FM!9>^a///CCCCCCf   	6;.#@#@8O;.1,,,<<
J&JJ
K
KKr%   c                 >   t          | t          t          f          r4t          d | D                       rt          d | D                       r| S t          | t          t          f          rXt	          |           rIt          | d                   s| d         j        |k    r| gS | d         j        |dz   k    rd | D             S t          |           r<t          |           s| j        |k    r| ggS | j        |dz   k    rt          |           gS t          d          )as  
    Ensure that the output is a nested list of images.
    Args:
        images (`Union[list[ImageInput], ImageInput]`):
            The input image.
        expected_ndims (`int`, *optional*, defaults to 3):
            The expected number of dimensions for a single input image.
    Returns:
        list: A list of list of images or a list of 4d array of images.
    c              3   N   K   | ] }t          |t          t          f          V  !d S r6   rz   r{   s     r#   rP   z-make_nested_list_of_images.<locals>.<genexpr>  r}   r%   c              3   :   K   | ]}t          |          p| V  d S r6   r   r{   s     r#   rP   z-make_nested_list_of_images.<locals>.<genexpr>  r   r%   r   r	   c                 ,    g | ]}t          |          S r   )r\   rO   s     r#   rX   z.make_nested_list_of_images.<locals>.<listcomp>  s    444EDKK444r%   z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.r   rv   s     r#   make_nested_list_of_imagesr      sF     	6D%=))KKFKKKKK YYRXYYYYY
  &4-(( 5-DV-L-L 5q	"" 	fQin&F&F8O!9>^a///44V4444 f " 	6;.#@#@H:;.1,,,LL>!
t
u
uur%   c                     t          |           st          dt          |                      t                      r3t	          | t
          j        j                  rt          j        |           S t          |           S )NzInvalid image type: )
rK   rE   rF   r   r7   r8   r9   r]   arrayr   r:   s    r#   to_numpy_arrayr     sl    # =;S		;;<<< C!A!A x}}C==r%   num_channels.c                    ||nd}t          |t                    r|fn|}| j        dk    rd\  }}n9| j        dk    rd\  }}n(| j        dk    rd\  }}nt          d| j                   | j        |         |v r>| j        |         |v r/t
                              d	| j         d
           t          j        S | j        |         |v rt          j        S | j        |         |v rt          j	        S t          d          )a[  
    Infers the channel dimension format of `image`.

    Args:
        image (`np.ndarray`):
            The image to infer the channel dimension of.
        num_channels (`int` or `tuple[int, ...]`, *optional*, defaults to `(1, 3)`):
            The number of channels of the image.

    Returns:
        The channel dimension of the image.
    Nr	   rq   rq   )r            )r   r   z(Unsupported number of image dimensions: z4The channel dimension is ambiguous. Got image shape z. Assuming channels are the first dimension. Use the [input_data_format](https://huggingface.co/docs/transformers/main/internal/image_processing_utils#transformers.image_transforms.rescale.input_data_format) parameter to assign the channel dimension.z(Unable to infer channel dimension format)
r7   intru   rE   shapeloggerwarningr'   r-   r.   )rH   r   	first_dimlast_dims       r#   infer_channel_dimension_formatr      s/    $0#;<<L&0s&C&CUL??LzQ"	88	q"	88	q"	88PEJPPQQQ{9--%+h2G<2W2W K5;  K  K  K	
 	
 	
  %%	Y	<	/	/%%	X	,	.	.$$
?
@
@@r%   input_data_formatc                     |t          |           }|t          j        k    r
| j        dz
  S |t          j        k    r
| j        dz
  S t          d|           )a  
    Returns the channel dimension axis of the image.

    Args:
        image (`np.ndarray`):
            The image to get the channel dimension axis of.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format of the image. If `None`, will infer the channel dimension from the image.

    Returns:
        The channel dimension axis of the image.
    Nrq   r	   Unsupported data format: )r   r'   r-   ru   r.   rE   )rH   r   s     r#   get_channel_dimension_axisr   G  sf      :5AA,222zA~	.3	3	3zA~
D1BDD
E
EEr%   channel_dimc                     |t          |           }|t          j        k    r| j        d         | j        d         fS |t          j        k    r| j        d         | j        d         fS t          d|           )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.   rE   )rH   r   s     r#   get_image_sizer   ]  sz     4U;;&,,,{2B//	(-	-	-{2B//B[BBCCCr%   
image_size
max_height	max_widthc                     | \  }}||z  }||z  }t          ||          }t          ||z            }t          ||z            }	||	fS )a  
    Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio.
    Important, even if image_height < max_height and image_width < max_width, the image will be resized
    to at least one of the edges be equal to max_height or max_width.

    For example:
        - input_size: (100, 200), max_height: 50, max_width: 50 -> output_size: (25, 50)
        - input_size: (100, 200), max_height: 200, max_width: 500 -> output_size: (200, 400)

    Args:
        image_size (`tuple[int, int]`):
            The image to resize.
        max_height (`int`):
            The maximum allowed height.
        max_width (`int`):
            The maximum allowed width.
    )rn   r   )
r   r   r   heightwidthheight_scalewidth_scale	min_scale
new_height	new_widths
             r#   #get_image_size_for_max_height_widthr   u  s_    , MFE&Le#KL+..IVi'((JEI%&&Iy  r%   valuesc                 (    d t          |  D             S )zO
    Return the maximum value across all indices of an iterable of values.
    c                 ,    g | ]}t          |          S r   )ro   )r    values_is     r#   rX   z&max_across_indices.<locals>.<listcomp>  s    777hCMM777r%   )zip)r   s    r#   max_across_indicesr     s     87#v,7777r%   c                     |t           j        k    rt          d | D                       \  }}}n@|t           j        k    rt          d | D                       \  }}}nt	          d|           ||fS )zH
    Get the maximum height and width across all images in a batch.
    c                     g | ]	}|j         
S r   r   r    r;   s     r#   rX   z(get_max_height_width.<locals>.<listcomp>      6S6S6SSsy6S6S6Sr%   c                     g | ]	}|j         
S r   r   r   s     r#   rX   z(get_max_height_width.<locals>.<listcomp>  r   r%   z"Invalid channel dimension format: )r'   r-   r   r.   rE   )rL   r   _r   r   s        r#   get_max_height_widthr     s     ,222#56S6SF6S6S6S#T#T :yy	.3	3	3#56S6SF6S6S6S#T#T 
IqqQ>OQQRRR	""r%   
annotationc                     t          | t                    rfd| v rbd| v r^t          | d         t          t          f          r<t	          | d                   dk    s!t          | d         d         t                    rdS dS )Nimage_idannotationsr   TFr7   dictr\   re   lenr   s    r#   "is_valid_annotation_coco_detectionr     s    :t$$
*$$Z''z-04-@@ ( 
=)**a//:j>WXY>Z\`3a3a/ t5r%   c                    t          | t                    rjd| v rfd| v rbd| v r^t          | d         t          t          f          r<t	          | d                   dk    s!t          | d         d         t                    rdS dS )Nr   segments_info	file_namer   TFr   r   s    r#   !is_valid_annotation_coco_panopticr     s    :t$$*$$z)):%%z/2T5MBB & 
?+,,11Z
?@[\]@^`d5e5e1 t5r%   r   c                 4    t          d | D                       S )Nc              3   4   K   | ]}t          |          V  d S r6   )r   r    anns     r#   rP   z3valid_coco_detection_annotations.<locals>.<genexpr>  s+      NN31#66NNNNNNr%   rQ   r   s    r#    valid_coco_detection_annotationsr     s    NN+NNNNNNr%   c                 4    t          d | D                       S )Nc              3   4   K   | ]}t          |          V  d S r6   )r   r   s     r#   rP   z2valid_coco_panoptic_annotations.<locals>.<genexpr>  s+      MM#055MMMMMMr%   rQ   r   s    r#   valid_coco_panoptic_annotationsr     s    MMMMMMMMr%   timeoutc           	         t          t          dg           t          | t                    r\|                     d          s|                     d          rHt
          j                            t          t          j
        | |d          j                            } nt          j                            |           r t
          j                            |           } n|                     d          r|                     d          d         } 	 t!          j        |                                           }t
          j                            t          |                    } nU# t&          $ r}t)          d	|  d
|           d}~ww xY wt          | t
          j        j                  st+          d          t
          j                            |           } |                     d          } | S )a3  
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
        timeout (`float`, *optional*):
            The timeout value in seconds for the URL request.

    Returns:
        `PIL.Image.Image`: A PIL Image.
    visionhttp://https://Tr   follow_redirectsdata:image/,r	   Incorrect image source. Must be a valid URL starting with `http://` or `https://`, a valid path to an image file, or a base64 encoded string. Got . Failed with NzuIncorrect format used for image. Should be an url linking to an image, a base64 string, a local path, or a PIL image.RGB)r   
load_imager7   str
startswithr8   r9   openr   httpxgetcontentospathisfilesplitbase64decodebytesencode	ExceptionrE   	TypeErrorImageOpsexif_transposeconvert)rH   r   b64es       r#   r   r     s     j8*---% 
I&& 	%*:*::*F*F 	 INN759UG^b+c+c+c+k#l#lmmEEW^^E"" 	INN5))EE.. ,C((+(88	ws||44     B  jo  B  B  @  B  B   sy// 
 D
 
 	
 L''..EMM%  ELs   AE" "
F,FF)torchvision)backendsc                    ddl }t          | t                    r}|                     d          s|                     d          r[t	          j        | |d          j        } |j        t          |          |j	                  }t          |t          j                  S t          j                            |           rt          | t          j                  S |                     d	          r|                     d
          d         } 	 t#          j        |                                           }n'# t(          $ r}t+          d|  d|           d}~ww xY w |j        t          |          |j	                  }t          |t          j                  S t          | t,          j        j                  rAt,          j                            |           } t5          |                     d                    S t9          d          )at  
    Loads `image` directly to a `torch.Tensor` using torchvision.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
        timeout (`float`, *optional*):
            The timeout value in seconds for the URL request.

    Returns:
        `torch.Tensor`: A `[C, H, W]` uint8 tensor in RGB channel order.
    r   Nr   r   Tr   )rl   )moder   r   r	   r   r   r   z`Incorrect format used for image. Should be a URL, a local path, a base64 string, or a PIL image.)r@   r7   r   r   r   r   r   
frombuffer	bytearrayrm   r   r   r   r   r   r   r   r   r   r   r   rE   r8   r9   r   r   r   r   r   )rH   r   r@   rawbufr   s         r#   load_image_as_tensorr     s   " LLL% 
I&& 	=%*:*::*F*F 	=)E7TJJJRC"%"9S>>EEEC-*;<<<<W^^E"" 	=M,=>>>>.. ,C((+(88     B  jo  B  B  @  B  B   #%"9S>>EEEC-*;<<<<	E39?	+	+ 
++E22U]]511222n
 
 	
s   &D2 2
E<EEc                     t          | t          t          f          rMt          |           r0t          | d         t          t          f          rfd| D             S fd| D             S t	          |           S )a  Loads images, handling different levels of nesting.

    Args:
      images: A single image, a list of images, or a list of lists of images to load.
      timeout: Timeout for loading images.

    Returns:
      A single image, a list of images, a list of lists of images.
    r   c                 ,    g | ]}fd |D             S )c                 2    g | ]}t          |           S r   r   r    rH   r   s     r#   rX   z*load_images.<locals>.<listcomp>.<listcomp>:  s&    QQQEZw777QQQr%   r   )r    image_groupr   s     r#   rX   zload_images.<locals>.<listcomp>:  s/    lllVaQQQQ[QQQlllr%   c                 2    g | ]}t          |           S r  r  r  s     r#   rX   zload_images.<locals>.<listcomp><  s&    KKK5Jug666KKKr%   r  )r7   r\   re   r   r   )rL   r   s    `r#   load_imagesr	  ,  s     &4-(( 3v;; 	L:fQi$?? 	LllllekllllKKKKFKKKK&'2222r%   
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padpad_sizedo_center_crop	crop_size	do_resizesizeresamplePILImageResamplingr   c                     | r|t          d          |r|t          d          |r||t          d          |r|t          d          |	r|
|t          d          dS dS )a  
    Checks validity of typically used arguments in an `ImageProcessor` `preprocess` method.
    Raises `ValueError` if arguments incompatibility is caught.
    Many incompatibilities are model-specific. `do_pad` sometimes needs `size_divisor`,
    sometimes `size_divisibility`, and sometimes `size`. New models and processors added should follow
    existing arguments when possible.

    Nz=`rescale_factor` must be specified if `do_rescale` is `True`.zgDepending on the model, `size_divisor` or `pad_size` or `size` must be specified if `do_pad` is `True`.zP`image_mean` and `image_std` must both be specified if `do_normalize` is `True`.z<`crop_size` must be specified if `do_center_crop` is `True`.zA`size` and `resample` must be specified if `do_resize` is `True`.)rE   )r
  r  r  r  r  r  r  r  r  r  r  r  s               r#   validate_preprocess_argumentsr  A  s    ,  Zn,XYYY 	
(" u
 
 	
  m+y/@klll Y)+WXXX ^$*x/C\]]]^ ^/C/Cr%   c                       e Zd ZdZd ZddZd Zdej        de	e
z  dej        fd	ZddZd ZddZddZd Zd ZddZdS )ImageFeatureExtractionMixinzD
    Mixin that contain utilities for preparing image features.
    c                     t          |t          j        j        t          j        f          s/t          |          s"t          dt          |           d          d S d S )Nz	Got type zU which is not supported, only `PIL.Image.Image`, `np.ndarray` and `torch.Tensor` are.)r7   r8   r9   r]   r^   r   rE   rF   selfrH   s     r#   _ensure_format_supportedz4ImageFeatureExtractionMixin._ensure_format_supportedt  sq    %#)/2:!>?? 	X]H^H^ 	&DKK & & &  	 	 	 	r%   Nc                    |                      |           t          |          r|                                }t          |t          j                  r|%t          |j        d         t          j                  }|j        dk    r&|j	        d         dv r|
                    ddd          }|r|dz  }|                    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   rq   r   r	   r      )r  r   rA   r7   r]   r^   flatfloatingru   r   	transposeastyperm   r8   r9   	fromarray)r  rH   rescales      r#   to_pil_imagez(ImageFeatureExtractionMixin.to_pil_image{  s     	%%e,,,5!! 	"KKMMEeRZ(( 
	.$UZ]BK@@zQ5;q>V#;#;1a00 $LL**E9&&u---r%   c                     |                      |           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   )r  r7   r8   r9   r   r  s     r#   convert_rgbz'ImageFeatureExtractionMixin.convert_rgb  sE     	%%e,,,%11 	L}}U###r%   rH   scalerj   c                 6    |                      |           ||z  S )z7
        Rescale a numpy image by scale amount
        )r  )r  rH   r*  s      r#   r&  z#ImageFeatureExtractionMixin.rescale  s"     	%%e,,,u}r%   Tc                    |                      |           t          |t          j        j                  rt	          j        |          }t          |          r|                                }|%t          |j        d         t          j	                  n|}|r3| 
                    |                    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?rq   r   r	   )r  r7   r8   r9   r]   r   r   rA   r!  integerr&  r$  float32ru   r#  )r  rH   r&  channel_firsts       r#   r   z*ImageFeatureExtractionMixin.to_numpy_array  s     	%%e,,,eSY_-- 	$HUOOE5!! 	"KKMME;B?*UZ]BJ777PW 	FLLbj!9!99EEE 	-UZ1__OOAq!,,Er%   c                     |                      |           t          |t          j        j                  r|S t	          |          r|                    d          }nt          j        |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   rY   )r  r7   r8   r9   r   	unsqueezer]   expand_dimsr  s     r#   r3  z'ImageFeatureExtractionMixin.expand_dims  sq     	%%e,,, eSY_-- 	L5!! 	2OOA&&EEN5q111Er%   Fc                    |                      |           t          |t          j        j                  r|                     |d          }n|rt          |t
          j                  r4|                     |                    t
          j	                  d          }n7t          |          r(|                     |                                d          }t          |t
          j                  rt          |t
          j                  s,t          j        |                              |j                  }t          |t
          j                  s,t          j        |                              |j                  }nt          |          rddl}t          ||j                  s;t          |t
          j                  r |j        |          }n |j        |          }t          ||j                  s;t          |t
          j                  r |j        |          }n |j        |          }|j        dk    r-|j        d         dv r||ddddf         z
  |ddddf         z  S ||z
  |z  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)r&  r-  r   Nrq   r   )r  r7   r8   r9   r   r]   r^   r&  r$  r/  r   floatr   rl   r@   r`   
from_numpytensorru   r   )r  rH   meanstdr&  r@   s         r#   	normalizez%ImageFeatureExtractionMixin.normalize  s6     	%%e,,,eSY_-- 	?''t'<<EE  	?%,, ?U\\"*%=%=yII '' ?U[[]]I>>eRZ(( 	,dBJ// :x~~,,U[99c2:.. 8hsmm**5;77U## 	,LLLdEL11 .dBJ// .+5+D11DD'5<--Dc5<00 ,c2:.. ,*%*3//CC&%,s++C:??u{1~77DD$//3qqq$}3EEEDLC''r%   c                    ||nt           j        }|                     |           t          |t          j        j                  s|                     |          }t          |t                    rt          |          }t          |t                    st          |          dk    r|r*t          |t                    r||fn|d         |d         f}n|j        \  }}||k    r||fn||f\  }}	t          |t                    r|n|d         }
||
k    r|S |
t          |
|	z  |z            }}|8||
k    rt          d| d|           ||k    rt          ||z  |z            |}}||k    r||fn||f}|                    ||          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 = )r  )r  BILINEARr  r7   r8   r9   r'  r\   re   r   r   r  rE   resize)r  rH   r  r  default_to_squaremax_sizer   r   shortlongrequested_new_short	new_shortnew_longs                r#   r=  z"ImageFeatureExtractionMixin.resize  s   <  (3889K9T%%e,,,%11 	-%%e,,EdD!! 	;;DdC   	[CIINN  ['1$'<'<Td||47DQRGBT %
v16&ufoovuot.8s.C.C&Pdda#/// L&93?RUY?Y\a?a;b;b8	'#666(G( G G@DG G    (**.1(Y2F2Q.R.RT\8	05	8,,hPYEZ||D8|444r%   c                 V   |                      |           t          |t                    s||f}t          |          st          |t          j                  rN|j        dk    r|                     |          }|j        d         dv r|j        dd         n|j        dd         }n|j	        d         |j	        d         f}|d         |d         z
  dz  }||d         z   }|d         |d         z
  dz  }||d         z   }t          |t          j        j                  r|                    ||||f          S |j        d         dv }|sWt          |t          j                  r|                    ddd          }t          |          r|                    ddd          }|dk    r-||d         k    r!|dk    r||d         k    r|d||||f         S |j        dd         t          |d         |d                   t          |d         |d                   fz   }	t          |t          j                  rt	          j        ||	          }
n$t          |          r|                    |	          }
|	d         |d         z
  dz  }||d         z   }|	d	         |d         z
  dz  }||d         z   }||
d||||f<   ||z  }||z  }||z  }||z  }|
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).
        r   r   r   r	   N.r   r   r   )r  r7   re   r   r]   r^   ru   r3  r   r  r8   r9   cropr#  permutero   
zeros_like	new_zerosrn   )r  rH   r  image_shapetopbottomleftrightr0  	new_shape	new_imagetop_pad
bottom_padleft_pad	right_pads                  r#   center_cropz'ImageFeatureExtractionMixin.center_cropY  sd    	%%e,,,$&& 	 $<D 5!! 	9Zrz%B%B 	9zQ((//-2[^v-E-E%+abb//5;WYXYWY?KK :a=%*Q-8K1~Q'A-tAwAa(Q.tAw eSY_-- 	:::tS%8999 A&0  	/%,, 11a00u%% /aA.. !88+a.00TQYY5KXYNCZCZc&j$u*455 K$DG[^(D(Dc$q'S^_`SaFbFb'cc	eRZ(( 	3e9===IIU## 	3	22IR=;q>1a7{1~-
bMKN2q8{1~-	AF	#wz)8I+==>w'Qs9?2#6???QPST]TcdfTginPoPoAoo
	 r%   c                     |                      |           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.
        Nr   )r  r7   r8   r9   r   r  s     r#   flip_channel_orderz.ImageFeatureExtractionMixin.flip_channel_order  s`     	%%e,,,eSY_-- 	/''..ETTrT111aaaZ  r%   r   c                     ||nt           j        j        }|                     |           t	          |t           j        j                  s|                     |          }|                    ||||||          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)r8   r9   NEARESTr  r7   r'  rotate)r  rH   angler  rY  rZ  r[  r\  s           r#   r^  z"ImageFeatureExtractionMixin.rotate  s}      (3889J%%e,,,%11 	-%%e,,E||HVFicl  
 
 	
r%   r6   )NT)F)NTN)Nr   NNN)r*   r+   r,   __doc__r  r'  r)  r]   r^   r5  r   r&  r   r3  r:  r=  rU  rW  r^  r   r%   r#   r  r  o  s	             <$ $ $RZ  
       @  (2( 2( 2( 2(hA5 A5 A5 A5FI I IV! ! !"
 
 
 
 
 
r%   r  annotation_formatsupported_annotation_formatsc                     | |vrt          dt           d|           | t          j        u rt	          |          st          d          | t          j        u rt          |          st          d          d S d S )NzUnsupported annotation format: z must be one of zInvalid COCO detection annotations. Annotations must a dict (single image) or list of dicts (batch of images) with the following keys: `image_id` and `annotations`, with the latter being a list of annotations in the COCO format.zInvalid COCO panoptic annotations. Annotations must a dict (single image) or list of dicts (batch of images) with the following keys: `image_id`, `file_name` and `segments_info`, with the latter being a list of annotations in the COCO format.)rE   formatr0   r3   r   r4   r   )ra  rb  r   s      r#   validate_annotationsre    s    
  <<<q6qqSoqqrrr,;;;/<< 	B   ,:::.{;; 	M   ;:	 	r%   valid_processor_keyscaptured_kwargsc                     t          |                              t          |                     }|r5d                    |          }t                              d| d           d S d S )Nz, zUnused or unrecognized kwargs: rt   )set
differencejoinr   r   )rf  rg  unused_keysunused_key_strs       r#   validate_kwargsrn    sp    o&&11#6J2K2KLLK L;//JJJJKKKKKL Lr%   c                       e Zd ZU dZdZedz  ed<   dZedz  ed<   dZedz  ed<   dZ	edz  ed<   dZ
edz  ed<   dZedz  ed<   d	 Zdd
Zd Zd Zd Zd Zd ZddZdefdZdS )SizeDictz>
    Hashable dictionary to store image size information.
    Nr   r   longest_edgeshortest_edger   r   c                 h    t          | |          rt          | |          S t          d| d          )NKey z not found in SizeDict.)hasattrgetattrKeyErrorr  keys     r#   __getitem__zSizeDict.__getitem__  s>    4 	&4%%%:c:::;;;r%   c                 f    t          | |          r t          | |          t          | |          S |S r6   ru  rv  )r  ry  defaults      r#   r   zSizeDict.get  s8    4 	&'$"4"4"@4%%%r%   c              #   r   K   t          |           D ]$}t          | |j                  }||j        |fV  %d S r6   )r   rv  name)r  fvals      r#   __iter__zSizeDict.__iter__  sN       	" 	"A$''Cfck!!!	" 	"r%   c                 h    t          | j        | j        | j        | j        | j        | j        f          S r6   )hashr   r   rq  rr  r   r   )r  s    r#   __hash__zSizeDict.__hash__  s/    T[$*d.?ASUYUdfjftuvvvr%   c                 F    t          | |          ot          | |          d uS r6   r|  rx  s     r#   __contains__zSizeDict.__contains__  s&    tS!!DgdC&8&8&DDr%   c                     t          | |          st          d| d          t                              | ||           d S )Nrt  z" is not a valid field of SizeDict.)ru  rw  object__setattr__)r  ry  values      r#   __setitem__zSizeDict.__setitem__  sM    tS!! 	KI#IIIJJJ4e,,,,,r%   c                 2    t          t                    rt                     k    S t          t                    rRt           fdt	                     D                       t          fdt	                     D                       k    S t
          S )Nc              3   B   K   | ]}t          |j                  V  d S r6   rv  r  )r    r  r  s     r#   rP   z"SizeDict.__eq__.<locals>.<genexpr>  s/      EE1qv..EEEEEEr%   c              3   B   K   | ]}t          |j                  V  d S r6   r  )r    r  others     r#   rP   z"SizeDict.__eq__.<locals>.<genexpr>  sH       O O+,qv&&O O O O O Or%   )r7   r   rp  re   r   NotImplemented)r  r  s   ``r#   __eq__zSizeDict.__eq__  s    eT"" 	'::&&eX&& 	EEEEtEEEEE O O O O06tO O O J J   r%   rj   c                     t          |t          t          z            r=t          |           }|                    t          |                     t          di |S t          S )Nr   )r7   r   rp  updater  r  r  mergeds      r#   __or__zSizeDict.__or__!  sU    eTH_-- 	&$ZZFMM$u++&&&%%f%%%r%   c                     t          |t                    r3t          |          }|                    t          |                      |S t          S r6   )r7   r   r  r  r  s      r#   __ror__zSizeDict.__ror__(  sB    eT"" 	%[[FMM$t**%%%Mr%   r6   )rj   rp  )r*   r+   r,   r`  r   r   __annotations__r   rq  rr  r   r   rz  r   r  r  r  r  r  r  r   r  r   r%   r#   rp  rp    s=          FC$JE3:#L#*### $M3:$$$!Jd
!!! IsTz   < < <
   
" " "w w wE E E- - -
           r%   rp  )rq   r6   )NNNNNNNNNNNN)ir   r   collections.abcr   dataclassesr   r   ior   typingr   r   r   rA   r]   utilsr
   r   r   r   r   r   r   r   r   utils.constantsr   r   r   r   r   r   utils.import_utilsr   	PIL.Imager8   PIL.ImageOpsr9   
Resamplingr  torchvision.ior   r   torchvision.transformsr   !torchvision.transforms.functionalr   r]  NEAREST_EXACTBOXr<  HAMMINGBICUBICLANCZOSpil_torch_interpolation_mappingitemstorch_pil_interpolation_mappingr@   
get_loggerr*   r   r^   r\   
ImageInputr'   r0   r   r   r   AnnotationTyper<   r>   rI   rK   rS   rc   rf   ri   boolrp   rw   r   r   r   re   r   r   r   r   r   r-   r   r   r   r   r   r5  r   r   r	  r  r  re  rn  rp  r   r%   r#   <module>r     s
    				 $ $ $ $ $ $ ) ) ) ) ) ) ) )                   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                ) ( ( ( ( (  .- )::::::::888888?????? 	"$5$C 1 5#%6%?"$5$="$5$="$5$='# 'a&`8W8]8]8_8_&`&`&`##&(#&(#  LLL 
	H	%	% rz>48I3JDQSQ[L\^bcq^rr

    |   
$ $ $ $ $| $ $ $
 c39tDz112F F F       ? ? ?L L LED E E E E, , ,	 	 	  52: 5$ 5 5 5 5# # #D<L # # # #P #L #Lz)#L#L #L #L #L #LP $v $vz)$v$v 
*$v $v $v $vN2:     EI$A $A:$A%(5c?%:T%A$A$A $A $A $ANF Fbj FEUX[E[^bEb Fnq F F F F,D D"* D3Cd3J DV[\_ad\dVe D D D D0!c3h!! ! 38_	! ! ! !>8x} 8c 8 8 8 8 braw# #~rz123#HKN^H^#	#Y# # # #4TE\8I3J t    $sD5L7H2I d     O(4TE\@Q;R2S OX\ O O O ON$sD5L?P:Q1R NW[ N N N N !* *''(*T\* * * * *Z 
#$$$ !*
 *
''(*
T\*
 *
 *
 *
 %$*
\ QU3 3$s$5563AF3
d#45tDAR<S7TTU3 3 3 3, ##' $-1,0,0"&'+!"&MQ+^ +^t+^DL+^ ++^ U#d*	+^
 tE{"T)+^ 4K+^ 38ns"T)+^ 4K+^ CH~$+^ d{+^ sCx.4
+^ (*=sBCdJ+^ +^ +^ +^\\
 \
 \
 \
 \
 \
 \
 \
~
'"'(8#(="> d 
	   2L$s) Ld3i L L L L = = = = = = = = = =r%   