
    eTh[                     8   S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	J
r
  S SKrS SKrS SKJr  SSKJrJrJrJrJrJrJrJrJrJrJr  SS	KJrJrJrJ r J!r!J"r"  \" 5       (       Ga  S SK#r$S SK%r$\RL                  " \RL                  " \$RN                  5      RP                  5      \RL                  " S
5      :  a  \$RR                  RT                  r+O\$RR                  r+\" 5       (       a  S SK,J-r-  \+R\                  \-R^                  \+R`                  \-R`                  \+Rb                  \-Rb                  \+Rd                  \-Rd                  \+Rf                  \-Rf                  \+Rh                  \-Rh                  0r5O0 r5\" 5       (       a  S SK6r6\Rn                  " \85      r9\
S\Rt                  S\;S   \;\Rt                     \;S   4   r< " S S\5      r= " S S\5      r> " S S\5      r?\@\A\
\B\A\;\@   4   4   rCS rD " S S\5      rES rFS rGS\;4S jrHS rIS rJS rKS\Rt                  S\L4S  jrMSTS!\BS\;\<   4S" jjrNS\
\;\<   \<4   S\<4S# jrOS\
\;\<   \<4   S\<4S$ jrPS\Rt                  4S% jrQ SUS\Rt                  S&\	\
\B\R\BS'4   4      S\=4S( jjrS SUS\Rt                  S)\	\
\=\A4      S\B4S* jjrTSUS\Rt                  S+\=S\R\B\B4   4S, jjrUS-\R\B\B4   S.\BS/\BS\R\B\B4   4S0 jrVS1\@\A\
\;\R4   4   S\L4S2 jrWS1\@\A\
\;\R4   4   S\L4S3 jrXS4\\@\A\
\;\R4   4      S\L4S5 jrYS4\\@\A\
\;\R4   4      S\L4S6 jrZSUS\
\AS4   S7\	\[   SS4S8 jjr\ SUS\
\;\R\AS4   S7\	\[   S\
S\;S   \;\;S      4   4S9 jjr]            SVS:\	\L   S;\	\[   S<\	\L   S=\	\
\[\;\[   4      S>\	\
\[\;\[   4      S?\	\L   S@\	\B   SA\	\L   SB\	\@\A\B4      SC\	\L   SD\	\@\A\B4      SE\	SF   4SG jjr^ " SH SI5      r_SJ\>SK\R\>S'4   S4\;\@   SS4SL jr`SM\;\A   SN\;\A   4SO jra\" SPSQ9 " SR SS5      5       rbg)W    N)Iterable)	dataclass)BytesIO)OptionalUnion)version   )ExplicitEnumis_jax_tensoris_numpy_arrayis_tf_tensor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_STDz9.1.0)InterpolationModezPIL.Image.Imageztorch.Tensorc                       \ rS rSrSrSrSrg)ChannelDimensionU   channels_firstchannels_last N)__name__
__module____qualname____firstlineno__FIRSTLAST__static_attributes__r!       P/var/www/auris/envauris/lib/python3.13/site-packages/transformers/image_utils.pyr   r   U   s    EDr)   r   c                       \ rS rSrSrSrSrg)AnnotationFormatZ   coco_detectioncoco_panopticr!   N)r"   r#   r$   r%   COCO_DETECTIONCOCO_PANOPTICr(   r!   r)   r*   r,   r,   Z   s    %N#Mr)   r,   c                   l    \ rS rSr\R
                  R                  r\R                  R                  rSrg)AnnotionFormat_   r!   N)	r"   r#   r$   r%   r,   r0   valuer1   r(   r!   r)   r*   r3   r3   _   s$    %44::N$2288Mr)   r3   c                 l    [        5       =(       a$    [        U [        R                  R                  5      $ N)r   
isinstancePILImageimgs    r*   is_pil_imager=   g   s     EZSYY__%EEr)   c                   (    \ rS rSrSrSrSrSrSrSr	g)		ImageTypek   pillowtorchnumpy
tensorflowjaxr!   N)
r"   r#   r$   r%   r9   TORCHNUMPY
TENSORFLOWJAXr(   r!   r)   r*   r?   r?   k   s    
CEEJ
Cr)   r?   c                 p   [        U 5      (       a  [        R                  $ [        U 5      (       a  [        R                  $ [        U 5      (       a  [        R                  $ [        U 5      (       a  [        R                  $ [        U 5      (       a  [        R                  $ [        S[        U 5       35      e)NzUnrecognised image type )r=   r?   r9   r   rF   r   rG   r   rH   r   rI   
ValueErrortypeimages    r*   get_image_typerO   s   s    E}}ueE###U}}
/U}=
>>r)   c                     [        U 5      =(       dA    [        U 5      =(       d/    [        U 5      =(       d    [        U 5      =(       d    [	        U 5      $ r7   )r=   r   r   r   r   r;   s    r*   is_valid_imagerQ      s8    vs 3vs7Kv|\_O`vdqrudvvr)   imagesc                 8    U =(       a    [        S U  5       5      $ )Nc              3   8   #    U  H  n[        U5      v   M     g 7fr7   )rQ   ).0rN   s     r*   	<genexpr>*is_valid_list_of_images.<locals>.<genexpr>   s     DVE.//V   all)rR   s    r*   is_valid_list_of_imagesr[      s    DcDVDDDr)   c                 V   [        U S   [        5      (       a  U  VVs/ s H  o  H  o"PM     M     snn$ [        U S   [        R                  5      (       a  [        R                  " U SS9$ [        U S   [
        R                  5      (       a  [
        R                  " U SS9$ g s  snnf )Nr   axis)dim)r8   listnpndarrayconcatenaterB   Tensorcat)
input_listsublistitems      r*   concatenate_listri      s    *Q-&&$.CJ747JCC	JqM2::	.	.~~jq11	JqM5<<	0	0yy++ 
1 Ds   B%c                     [        U [        [        45      (       a  U  H  n[        U5      (       a  M    g   g[	        U 5      (       d  gg)NFT)r8   r`   tuplevalid_imagesrQ   )imgsr<   s     r*   rl   rl      sC    $u&&C$$   D!!r)   c                 V    [        U [        [        45      (       a  [        U S   5      $ g)Nr   F)r8   r`   rk   rQ   r;   s    r*   
is_batchedro      s%    #e}%%c!f%%r)   rN   returnc                     U R                   [        R                  :X  a  g[        R                  " U 5      S:  =(       a    [        R                  " U 5      S:*  $ )zN
Checks to see whether the pixel values have already been rescaled to [0, 1].
Fr   r	   )dtypera   uint8minmaxrM   s    r*   is_scaled_imagerv      s>     {{bhh 66%=A4"&&-1"44r)   expected_ndimsc           	      J   [        U 5      (       a  U $ [        U 5      (       a  U /$ [        U 5      (       aW  U R                  US-   :X  a  [	        U 5      n U $ U R                  U:X  a  U /n U $ [        SUS-    SU SU R                   S35      e[        S[        U 5       S35      e)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.ztInvalid image type. Expected either PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray, but got .)ro   r=   rQ   ndimr`   rK   rL   )rR   rw   s     r*   make_list_of_imagesr{      s     & Fxf;;.1,,&\F  [[N*XF 	 78J7K4P^O_ `KK=. 
 	  $V~Q	0 r)   c                    [        U [        [        45      (       aK  [        S U  5       5      (       a4  [        S U  5       5      (       a  U  VVs/ s H  o  H  o"PM     M     snn$ [        U [        [        45      (       ah  [	        U 5      (       aX  [        U S   5      (       d  U S   R                  S:X  a  U $ U S   R                  S:X  a  U  VVs/ s H  o  H  o"PM     M     snn$ [        U 5      (       a>  [        U 5      (       d  U R                  S:X  a  U /$ U R                  S:X  a  [        U 5      $ [        SU  35      es  snnf s  snnf )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.
Returns:
    list: A list of images or a 4d array of images.
c              3   N   #    U  H  n[        U[        [        45      v   M     g 7fr7   r8   r`   rk   rU   images_is     r*   rV   +make_flat_list_of_images.<locals>.<genexpr>        KF
8dE]33F   #%c              3   8   #    U  H  n[        U5      v   M     g 7fr7   r[   r   s     r*   rV   r           I&h'11&rX   r         z*Could not make a flat list of images from 	r8   r`   rk   rZ   r[   r=   rz   rQ   rK   )rR   img_listr<   s      r*   make_flat_list_of_imagesr      s    	6D%=))KFKKKI&III$*?FhshF??&4-((-DV-L-Lq	""fQinn&9M!9>>Q(.CH(3C(CCCf6;;!#38O;;!<
A&J
KK @ Ds   EEc                 ^   [        U [        [        45      (       a0  [        S U  5       5      (       a  [        S U  5       5      (       a  U $ [        U [        [        45      (       ah  [	        U 5      (       aX  [        U S   5      (       d  U S   R                  S:X  a  U /$ U S   R                  S:X  a  U  Vs/ s H  n[        U5      PM     sn$ [        U 5      (       a@  [        U 5      (       d  U R                  S:X  a  U //$ U R                  S:X  a  [        U 5      /$ [        S5      es  snf )z
Ensure that the output is a nested list of images.
Args:
    images (`Union[List[ImageInput], ImageInput]`):
        The input image.
Returns:
    list: A list of list of images or a list of 4d array of images.
c              3   N   #    U  H  n[        U[        [        45      v   M     g 7fr7   r~   r   s     r*   rV   -make_nested_list_of_images.<locals>.<genexpr>  r   r   c              3   8   #    U  H  n[        U5      v   M     g 7fr7   r   r   s     r*   rV   r     r   rX   r   r   r   z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.r   )rR   rN   s     r*   make_nested_list_of_imagesr      s     	6D%=))KFKKKI&III &4-((-DV-L-Lq	""fQinn&98O!9>>Q-34VEDKV44 f6;;!#3H:;;!L>!
t
uu 5s   7D*c                    [        U 5      (       d  [        S[        U 5       35      e[        5       (       a?  [	        U [
        R                  R                  5      (       a  [        R                  " U 5      $ [        U 5      $ )NzInvalid image type: )
rQ   rK   rL   r   r8   r9   r:   ra   arrayr   r;   s    r*   to_numpy_arrayr     sY    #/S	{;<<C!A!Axx}C=r)   num_channels.c                 F   Ub  UOSn[        U[        5      (       a  U4OUnU R                  S:X  a  Su  p#OBU R                  S:X  a  Su  p#O-U R                  S:X  a  Su  p#O[        SU R                   35      eU R                  U   U;   aF  U R                  U   U;   a3  [
        R                  SU R                   S	35        [        R                  $ U R                  U   U;   a  [        R                  $ U R                  U   U;   a  [        R                  $ [        S
5      e)a7  
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.
r	   r   r   )r      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)
r8   intrz   rK   shapeloggerwarningr   r&   r'   )rN   r   	first_dimlast_dims       r*   infer_channel_dimension_formatr   &  s    $0#;<L&0s&C&CL?LzzQ"	8	q"	8	q"	8CEJJ<PQQ{{9-%++h2G<2WB5;;-  PJ  K	
  %%%	Y	<	/%%%	X	,	.$$$
?
@@r)   input_data_formatc                     Uc  [        U 5      nU[        R                  :X  a  U R                  S-
  $ U[        R                  :X  a  U R                  S-
  $ [        SU 35      e)ar  
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.
r   r	   Unsupported data format: )r   r   r&   rz   r'   rK   )rN   r   s     r*   get_channel_dimension_axisr   M  sd      :5A,222zzA~	.33	3zzA~
01B0CD
EEr)   channel_dimc                    Uc  [        U 5      nU[        R                  :X  a  U R                  S   U R                  S   4$ U[        R                  :X  a  U R                  S   U R                  S   4$ [        SU 35      e)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.
r   )r   r   r&   r   r'   rK   )rN   r   s     r*   get_image_sizer   e  s{     4U;&,,,{{2B//	(--	-{{2B//4[MBCCr)   
image_size
max_height	max_widthc                 j    U u  p4X-  nX$-  n[        XV5      n[        X7-  5      n[        XG-  5      n	X4$ )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.
)rt   r   )
r   r   r   heightwidthheight_scalewidth_scale	min_scale
new_height	new_widths
             r*   #get_image_size_for_max_height_widthr   }  sH    , MF&L#KL.IV'(JE%&I  r)   
annotationc                     [        U [        5      (       aX  SU ;   aR  SU ;   aL  [        U S   [        [        45      (       a.  [	        U S   5      S:X  d  [        U S   S   [        5      (       a  gg)Nimage_idannotationsr   TFr8   dictr`   rk   lenr   s    r*   "is_valid_annotation_coco_detectionr     si    :t$$*$Z'z-04-@@ 
=)*a/:j>WXY>Z\`3a3a r)   c                     [        U [        5      (       a^  SU ;   aX  SU ;   aR  SU ;   aL  [        U S   [        [        45      (       a.  [	        U S   5      S:X  d  [        U S   S   [        5      (       a  gg)Nr   segments_info	file_namer   TFr   r   s    r*   !is_valid_annotation_coco_panopticr     sq    :t$$*$z):%z/2T5MBB 
?+,1Z
?@[\]@^`d5e5e r)   r   c                 &    [        S U  5       5      $ )Nc              3   8   #    U  H  n[        U5      v   M     g 7fr7   )r   rU   anns     r*   rV   3valid_coco_detection_annotations.<locals>.<genexpr>  s     N+31#66+rX   rY   r   s    r*    valid_coco_detection_annotationsr     s    N+NNNr)   c                 &    [        S U  5       5      $ )Nc              3   8   #    U  H  n[        U5      v   M     g 7fr7   )r   r   s     r*   rV   2valid_coco_panoptic_annotations.<locals>.<genexpr>  s     M#055rX   rY   r   s    r*   valid_coco_panoptic_annotationsr     s    MMMMr)   timeoutc           	         [        [        S/5        [        U [        5      (       Ga-  U R	                  S5      (       d  U R	                  S5      (       aE  [
        R                  R                  [        [        R                  " XS9R                  5      5      n O[        R                  R                  U 5      (       a   [
        R                  R                  U 5      n OU R	                  S5      (       a  U R                  S5      S   n  [         R"                  " U R%                  5       5      n[
        R                  R                  [        U5      5      n O7[        U [
        R                  R                  5      (       a  U n O[+        S5      e[
        R,                  R/                  U 5      n U R1                  S5      n U $ ! [&         a  n[)        SU  S	U 35      eS
nAff = f)a  
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.
visionzhttp://zhttps://r   zdata:image/,r	   zIncorrect 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 z. 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_imager8   str
startswithr9   r:   openr   requestsgetcontentospathisfilesplitbase64decodebytesencode	ExceptionrK   	TypeErrorImageOpsexif_transposeconvert)rN   r   b64es       r*   r   r     s    j8*-%I&&%*:*::*F*F IINN78<<+O+W+W#XYEWW^^E""IINN5)E..C(+((8		ws|4
 
E399??	+	+ D
 	
 LL''.EMM% EL    i  jo  ip  p~  @  ~A  B s   AF> >
GGGc                 R   [        U [        [        45      (       at  [        U 5      (       aJ  [        U S   [        [        45      (       a,  U  VVs/ s H  o" Vs/ s H  n[	        X1S9PM     snPM     snn$ U  Vs/ s H  n[	        X1S9PM     sn$ [	        XS9$ s  snf s  snnf s  snf )zLoads 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   r   )r8   r`   rk   r   r   )rR   r   image_grouprN   s       r*   load_imagesr     s     &4-((v;;:fQi$??eklekVa[Q[EZ7[QekllDJKF5Ju6FKK&22	 RlKs   	BB+B:B$B
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padsize_divisibilitydo_center_crop	crop_size	do_resizesizeresamplePILImageResamplingc                     U (       a  Uc  [        S5      eU(       a  Uc  [        S5      eU(       a  Ub  Uc  [        S5      eU(       a  Uc  [        S5      eU	(       a  U
b  Uc  [        S5      egg)ao  
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`.zzDepending on the model, `size_divisibility`, `size_divisor`, `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`.)rK   )r   r   r   r   r   r   r   r   r   r   r   r   s               r*   validate_preprocess_argumentsr     s    , n,XYY#+ I
 	
 +y/@kll)+WXXdlh&6\]] '7yr)   c                       \ rS rSrSrS rSS jrS rS\R                  S\
\\4   S	\R                  4S
 jrSS jrS rSS jrSS jrS rS rSS jrSrg)ImageFeatureExtractionMixini.  z<
Mixin that contain utilities for preparing image features.
c                     [        U[        R                  R                  [        R                  45      (       d)  [        U5      (       d  [        S[        U5       S35      eg g )Nz	Got type zS which is not supported, only `PIL.Image.Image`, `np.array` and `torch.Tensor` are.)r8   r9   r:   ra   rb   r   rK   rL   selfrN   s     r*   _ensure_format_supported4ImageFeatureExtractionMixin._ensure_format_supported3  sW    %#))//2::!>??X]H^H^DK= )& &  I_?r)   Nc                    U R                  U5        [        U5      (       a  UR                  5       n[        U[        R
                  5      (       a  Uc'  [        UR                  S   [        R                  5      nUR                  S:X  a&  UR                  S   S;   a  UR                  SSS5      nU(       a  US-  nUR                  [        R                  5      n[        R                  R                  U5      $ U$ )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.
r   r   r   r	   r      )r   r   rC   r8   ra   rb   flatfloatingrz   r   	transposeastypers   r9   r:   	fromarray)r   rN   rescales      r*   to_pil_image(ImageFeatureExtractionMixin.to_pil_image:  s     	%%e,5!!KKMEeRZZ(($UZZ]BKK@zzQ5;;q>V#;1a0LL*E99&&u--r)   c                     U R                  U5        [        U[        R                  R                  5      (       d  U$ UR	                  S5      $ )zo
Converts `PIL.Image.Image` to RGB format.

Args:
    image (`PIL.Image.Image`):
        The image to convert.
r   )r   r8   r9   r:   r   r   s     r*   convert_rgb'ImageFeatureExtractionMixin.convert_rgbX  s;     	%%e,%11L}}U##r)   rN   scalerp   c                 ,    U R                  U5        X-  $ )z'
Rescale a numpy image by scale amount
)r   )r   rN   r  s      r*   r	  #ImageFeatureExtractionMixin.rescalef  s     	%%e,}r)   c                    U R                  U5        [        U[        R                  R                  5      (       a  [        R
                  " U5      n[        U5      (       a  UR                  5       nUc'  [        UR                  S   [        R                  5      OUnU(       a/  U R                  UR                  [        R                  5      S5      nU(       a#  UR                  S:X  a  UR                  SSS5      nU$ )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.
r   p?r   r   r	   )r   r8   r9   r:   ra   r   r   rC   r  integerr	  r  float32rz   r  )r   rN   r	  channel_firsts       r*   r   *ImageFeatureExtractionMixin.to_numpy_arraym  s     	%%e,eSYY__--HHUOE5!!KKME;B?*UZZ]BJJ7PWLLbjj!99EEUZZ1_OOAq!,Er)   c                     U R                  U5        [        U[        R                  R                  5      (       a  U$ [	        U5      (       a  UR                  S5      nU$ [        R                  " USS9nU$ )z
Expands 2-dimensional `image` to 3 dimensions.

Args:
    image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`):
        The image to expand.
r   r]   )r   r8   r9   r:   r   	unsqueezera   expand_dimsr   s     r*   r  'ImageFeatureExtractionMixin.expand_dims  se     	%%e, eSYY__--L5!!OOA&E  NN5q1Er)   c                 >   U R                  U5        [        U[        R                  R                  5      (       a  U R	                  USS9nOU(       a  [        U[
        R                  5      (       a0  U R                  UR                  [
        R                  5      S5      nO0[        U5      (       a   U R                  UR                  5       S5      n[        U[
        R                  5      (       a  [        U[
        R                  5      (       d/  [
        R                  " U5      R                  UR                  5      n[        U[
        R                  5      (       d/  [
        R                  " U5      R                  UR                  5      nO[        U5      (       a  SSKn[        X%R                  5      (       dD  [        U[
        R                  5      (       a  UR                   " U5      nOUR"                  " U5      n[        X5R                  5      (       dD  [        U[
        R                  5      (       a  UR                   " U5      nOUR"                  " U5      nUR$                  S:X  a*  UR&                  S   S;   a  XSS2SS4   -
  USS2SS4   -  $ X-
  U-  $ )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   Nr   r   )r   r8   r9   r:   r   ra   rb   r	  r  r  r   floatr   rr   rB   rd   
from_numpytensorrz   r   )r   rN   meanstdr	  rB   s         r*   	normalize%ImageFeatureExtractionMixin.normalize  s     	%%e,eSYY__--''t'<E %,,U\\"**%=yI ''U[[]I>eRZZ((dBJJ//xx~,,U[[9c2::..hhsm**5;;7U##dLL11dBJJ// ++D1D <<-Dc<<00c2::..**3/C,,s+C::?u{{1~7D$//3q$}3EEELC''r)   c                    Ub  UO[         R                  nU R                  U5        [        U[        R
                  R
                  5      (       d  U R                  U5      n[        U[        5      (       a  [        U5      n[        U[        5      (       d  [        U5      S:X  a  U(       a#  [        U[        5      (       a  X"4O	US   US   4nOUR                  u  pgXg::  a  Xg4OXv4u  p[        U[        5      (       a  UOUS   n
X:X  a  U$ U
[        X-  U-  5      pUb,  XZ::  a  [        SU SU 35      eX:  a  [        X[-  U-  5      UpXg::  a  X4OX4nUR                  X#S9$ )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`.
r	   r   zmax_size = zN must be strictly greater than the requested size for the smaller edge size = )r   )r   BILINEARr   r8   r9   r:   r
  r`   rk   r   r   r   rK   resize)r   rN   r   r   default_to_squaremax_sizer   r   shortlongrequested_new_short	new_shortnew_longs                r*   r&  "ImageFeatureExtractionMixin.resize  sb   <  (389K9T9T%%e,%11%%e,EdD!!;DdC  CIN '1$'<'<|47DQRGBT %

16uovo.8s.C.Cda#/ L&93?R?Y\a?a;b8'6()( 4@@DvG   *.1(2F2Q.RT\805	,hEZ||D|44r)   c                    U R                  U5        [        U[        5      (       d  X"4n[        U5      (       d  [        U[        R
                  5      (       aS  UR                  S:X  a  U R                  U5      nUR                  S   S;   a  UR                  SS OUR                  SS nOUR                  S   UR                  S   4nUS   US   -
  S-  nXBS   -   nUS   US   -
  S-  nXbS   -   n[        U[        R                  R                  5      (       a  UR                  XdXu45      $ UR                  S   S;   a  SOSnU(       dU  [        U[        R
                  5      (       a  UR                  SSS5      n[        U5      (       a  UR                  SSS5      nUS:  a   XSS   ::  a  US:  a  XsS   ::  a
  USXE2Xg24   $ UR                  SS	 [        US   US   5      [        US   US   5      4-   n	[        U[        R
                  5      (       a  [        R                   " XS
9n
O![        U5      (       a  UR#                  U	5      n
U	S	   US   -
  S-  nXS   -   nU	S   US   -
  S-  nXS   -   nUW
SX2X24'   XK-  nX[-  nXm-  nX}-  nU
S[        SU5      [%        U
R                  S	   U5      2[        SU5      [%        U
R                  S   U5      24   n
U
$ )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	   NTF.r   )r   r   )r   r8   rk   r   ra   rb   rz   r  r   r   r9   r:   cropr  permuteru   
zeros_like	new_zerosrt   )r   rN   r   image_shapetopbottomleftrightr  	new_shape	new_imagetop_pad
bottom_padleft_pad	right_pads                  r*   center_crop'ImageFeatureExtractionMixin.center_crop  s    	%%e,$&&<D 5!!Zrzz%B%BzzQ((/-2[[^v-E%++ab/5;;WYXY?K ::a=%**Q-8K1~Q'A-AwAa(Q.Aw eSYY__--::t%899 !&A& 8e %,,1a0u%%aA. !8a.0TQY5XYNCZcj$*455 KK$DG[^(Dc$q'S^_`SaFb'cc	eRZZ((e=IU##	2IR=;q>1a71~-
bMKN2q81~-	AF	#w)8+==>Qs9??2#6??QPST]TcTcdfTginPoAoo
	 r)   c                     U R                  U5        [        U[        R                  R                  5      (       a  U R	                  U5      nUSSS2SS2SS24   $ )ah  
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   r8   r9   r:   r   r   s     r*   flip_channel_order.ImageFeatureExtractionMixin.flip_channel_orderc  sL     	%%e,eSYY__--''.ETrT1aZ  r)   c           	          Ub  UO[         R                  R                  nU R                  U5        [	        U[         R                  R                  5      (       d  U R                  U5      nUR                  X#XEXgS9$ )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`.
)r   expandcenter	translate	fillcolor)r9   r:   NEARESTr   r8   r
  rotate)r   rN   angler   rE  rF  rG  rH  s           r*   rJ  "ImageFeatureExtractionMixin.rotatet  sj      (389J9J%%e,%11%%e,E||Vi  
 	
r)   r!   r7   )NT)F)NTN)Nr   NNN)r"   r#   r$   r%   __doc__r   r
  r  ra   rb   r   r  r   r	  r   r  r"  r&  r?  rB  rJ  r(   r!   r)   r*   r   r   .  sj    <$RZZ eSj0A bjj @(2(hA5FIV!"
r)   r   annotation_formatsupported_annotation_formatsc                     X;  a  [        S[         SU 35      eU [        R                  L a  [	        U5      (       d  [        S5      eU [        R
                  L a  [        U5      (       d  [        S5      eg g )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.)rK   formatr,   r0   r   r1   r   )rN  rO  r   s      r*   validate_annotationsrR    s    
 <:6(BRSoRpqrr,;;;/<<B  ,:::.{;;M  < ;r)   valid_processor_keyscaptured_kwargsc                     [        U5      R                  [        U 5      5      nU(       a+  SR                  U5      n[        R	                  SU S35        g g )Nz, zUnused or unrecognized kwargs: ry   )set
differencejoinr   r   )rS  rT  unused_keysunused_key_strs       r*   validate_kwargsr[    sJ    o&11#6J2KLK;/88HJK r)   T)frozenc                       \ rS rSr% SrSr\\   \S'   Sr	\\   \S'   Sr
\\   \S'   Sr\\   \S'   Sr\\   \S'   Sr\\   \S	'   S
 rSrg)SizeDicti  z6
Hashable dictionary to store image size information.
Nr   r   longest_edgeshortest_edger   r   c                 V    [        X5      (       a  [        X5      $ [        SU S35      e)NzKey z not found in SizeDict.)hasattrgetattrKeyError)r   keys     r*   __getitem__SizeDict.__getitem__  s-    44%%cU"9:;;r)   r!   )r"   r#   r$   r%   rM  r   r   r   __annotations__r   r_  r`  r   r   rf  r(   r!   r)   r*   r^  r^    sb     !FHSM E8C="&L(3-&#'M8C=' $J$#Ix}#<r)   r^  )r   r7   )NNNNNNNNNNNN)cr   r   collections.abcr   dataclassesr   ior   typingr   r   rC   ra   r   	packagingr   utilsr
   r   r   r   r   r   r   r   r   r   r   utils.constantsr   r   r   r   r   r   	PIL.Imager9   PIL.ImageOpsparse__version__base_versionr:   
Resamplingr   torchvision.transformsr   rI  NEAREST_EXACTBOXr%  HAMMINGBICUBICLANCZOSpil_torch_interpolation_mappingrB   
get_loggerr"   r   rb   r`   
ImageInputr   r,   r3   r   r   r   AnnotationTyper=   r?   rO   rQ   r[   ri   rl   ro   boolrv   r{   r   r   r   rk   r   r   r   r   r   r   r   r   r  r   r   r   r   rR  r[  r^  r!   r)   r*   <module>r     s0    	 $ !  "        }}W]]3??3@@AW]]SZE[[ YY11 YY!!< &&(9(G(G""$5$9$9''):)C)C&&(9(A(A&&(9(A(A&&(9(A(A+
' +-'  
		H	% rzz>48I3JDQSQ[Q[L\^bcq^rr

| 
$| $
9\ 9
 c5c4:!5667F ?wED E,	52:: 5$ 5$ $D<L $N L$z"J./ L LF!v$z"J./!v!vH2::  NR$A::$A%-eCsCx4H.I%J$A$AP TXF::F*259I39N3O*PFF0D"** D3C DuUXZ]U] D0!c3h!! ! 38_	!>4U4;=O8O3P UY $sE$+<N7N2O TX  O(4U4QV;EW@W;X2Y O^b ON$sE$PU+DV?V:W1X N]a N)eC!223 )huo )Yj )Z TX3$s$5563AI%3
d#45tDAR<S7TTU3, "&&*#'6:59!'+%)*. $%)/3&^&^UO&^ 4.&^ ud5k123	&^
 eT%[012&^ TN&^  }&^ TN&^ S#X'&^ ~&^ 4S>
"&^ +,&^T\
 \
~
'"'(8#(="> d 
	2L$s) Ld3i L $< < <r)   