
    Z j                        S SK r S SKrS SKJr  S SKJr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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  SS	K J!r!  \" 5       (       a  S SK"r#S SK$r#\#RJ                  RL                  r'\" 5       (       a  S S
K(J)r)J*r*  S SK+J,r,  S SK-J.r.  \'R^                  \,R`                  \'Rb                  \,Rb                  \'Rd                  \,Rd                  \'Rf                  \,Rf                  \'Rh                  \,Rh                  \'Rj                  \,Rj                  0r6\6Ro                  5        V Vs0 s H  u  pX_M	     snn r8O0 r60 r8\" 5       (       a  S SK9r9\Rt                  " \;5      r<\S\Rz                  S\>S   \>\Rz                     \>S   4   r? " S S\5      r@ " S S\5      rA\B\C\D\C-  \>\B   -  4   rES rF " S S\5      rGS rHS rIS\>4S jrJS rKS rLS rMS\Rz                  S\N4S jrOSXS \DS\>\?   4S! jjrP SXS\>\?   \?-  S \DS\?4S" jjrQ SXS\>\?   \?-  S \DS\>\?   4S# jjrRS\Rz                  4S$ jrS SYS\Rz                  S%\D\T\DS&4   -  S-  S\@4S' jjrUSYS\Rz                  S(\@\C-  S-  S\D4S) jjrVSYS\Rz                  S*\@S-  S\T\D\D4   4S+ jjrWS,\T\D\D4   S-\DS.\DS\T\D\D4   4S/ jrXS0\\
   S\>\
   4S1 jrY\@R                  4S\>\S\Rz                  4      S(\C\@-  S\>\D   4S2 jjr[S3\B\C\>\T-  4   S\N4S4 jr\S3\B\C\>\T-  4   S\N4S5 jr]S6\\B\C\>\T-  4      S\N4S7 jr^S6\\B\C\>\T-  4      S\N4S8 jr_ SYS\\CS4   S9\`S-  SS4S: jjra\!" S;S<9 SYS\\CS4   S9\`S-  SS4S= jj5       rb SYS\\>\T\CS4   S9\`S-  S\S\>S   \>\>S      4   4S> jjrc            SZS?\NS-  S@\`S-  SA\NS-  SB\`\>\`   -  S-  SC\`\>\`   -  S-  SD\NS-  SE\B\C\D4   \D-  S-  SF\NS-  SG\B\C\D4   S-  SH\NS-  SI\B\C\D4   S-  SJ\SKSL\D4   S-  4SM jjrd " SN SO5      reSP\ASQ\T\AS&4   S6\>\B   SS4SR jrfSS\>\C   ST\>\C   4SU jrg\" 5        " SV SW5      5       rhgs  snn f )[    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_tensorz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#       i/root/GenerationalWealth/GenerationalWealth/venv/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    [        5       =(       a$    [        U [        R                  R                  5      $ N)r   
isinstancePILImageimgs    r,   is_pil_imager;   b   s     EZSYY__%EEr+   c                        \ rS rSrSrSrSrSrg)	ImageTypef   pillowtorchnumpyr#   N)r$   r%   r&   r'   r7   TORCHNUMPYr*   r#   r+   r,   r=   r=   f   s    
CEEr+   r=   c                     [        U 5      (       a  [        R                  $ [        U 5      (       a  [        R                  $ [        U 5      (       a  [        R                  $ [        S[        U 5       35      e)NzUnrecognized image type )	r;   r=   r7   r   rB   r   rC   
ValueErrortypeimages    r,   get_image_typerI   l   sX    E}}ue
/U}=
>>r+   c                 `    [        U 5      =(       d    [        U 5      =(       d    [        U 5      $ r5   )r;   r   r   r9   s    r,   is_valid_imagerK   v   s!    Ks 3Ks7KKr+   imagesc                 8    U =(       a    [        S U  5       5      $ )Nc              3   8   #    U  H  n[        U5      v   M     g 7fr5   )rK   ).0rH   s     r,   	<genexpr>*is_valid_list_of_images.<locals>.<genexpr>{   s     DVE.//V   all)rL   s    r,   is_valid_list_of_imagesrU   z   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)r6   listnpndarrayconcatenater@   Tensorcat)
input_listsublistitems      r,   concatenate_listrc   ~   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)r6   rZ   tuplevalid_imagesrK   )imgsr:   s     r,   rf   rf      sC    $u&&C$$   D!!r+   c                 V    [        U [        [        45      (       a  [        U S   5      $ g)Nr   F)r6   rZ   re   rK   r9   s    r,   
is_batchedri      s%    #e}%%c!f%%r+   rH   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	   )dtyper[   uint8minmaxrG   s    r,   is_scaled_imagerp      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.z]Invalid image type. Expected either PIL.Image.Image, numpy.ndarray, or torch.Tensor, but got .)ri   r;   rK   ndimrZ   rE   rF   )rL   rq   s     r,   make_list_of_imagesru      s     & Fxf;;.1,,&\F  [[N*XF 	 78J7K4P^O_ `KK=. 
 
ghlmshtguuvw r+   c                    [        U [        [        45      (       aK  [        S U  5       5      (       a4  [        S U  5       5      (       a  U  VVs/ s H  o"  H  o3PM     M     snn$ [        U [        [        45      (       ak  [	        U 5      (       a[  [        U S   5      (       d  U S   R                  U:X  a  U $ U S   R                  US-   :X  a  U  VVs/ s H  o"  H  o3PM     M     snn$ [        U 5      (       aA  [        U 5      (       d  U R                  U:X  a  U /$ U R                  U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.
    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   #    U  H  n[        U[        [        45      v   M     g 7fr5   r6   rZ   re   rO   images_is     r,   rP   +make_flat_list_of_images.<locals>.<genexpr>        KF
8dE]33F   #%c              3   T   #    U  H  n[        U5      =(       d    U(       + v   M      g 7fr5   rU   ry   s     r,   rP   r{      "     YRXh'1A\ARX   &(r   r	   z*Could not make a flat list of images from 	r6   rZ   re   rT   rU   r;   rt   rK   rE   )rL   rq   img_listr:   s       r,   make_flat_list_of_imagesr      s(   " 	6D%=))KFKKKYRXYYY$*?FhshF??&4-((-DV-L-Lq	""fQinn&FM!9>>^a//(.CH(3C(CCCf6;;.#@8O;;.1,,<
A&J
KK @ Ds   EEc                 j   [        U [        [        45      (       a0  [        S U  5       5      (       a  [        S U  5       5      (       a  U $ [        U [        [        45      (       ak  [	        U 5      (       a[  [        U S   5      (       d  U S   R                  U:X  a  U /$ U S   R                  US-   :X  a  U  Vs/ s H  n[        U5      PM     sn$ [        U 5      (       aC  [        U 5      (       d  U R                  U:X  a  U //$ U R                  US-   :X  a  [        U 5      /$ [        S5      es  snf )aO  
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   #    U  H  n[        U[        [        45      v   M     g 7fr5   rx   ry   s     r,   rP   -make_nested_list_of_images.<locals>.<genexpr>  r|   r}   c              3   T   #    U  H  n[        U5      =(       d    U(       + v   M      g 7fr5   r   ry   s     r,   rP   r     r   r   r   r	   z]Invalid input type. Must be a single image, a list of images, or a list of batches of images.r   )rL   rq   rH   s      r,   make_nested_list_of_imagesr      s	     	6D%=))KFKKKYRXYYY &4-((-DV-L-Lq	""fQinn&F8O!9>>^a//-34VEDKV44 f6;;.#@H:;;.1,,L>!
t
uu 5s   :D0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: )
rK   rE   rF   r   r6   r7   r8   r[   arrayr   r9   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   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)
r6   intrt   rE   shapeloggerwarningr   r(   r)   )rH   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(   rt   r)   rE   )rH   r   s     r,   get_channel_dimension_axisr   G  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)   rE   )rH   r   s     r,   get_image_sizer   ]  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.
)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  sH    , MF&L#KL.IV'(JE%&I  r+   valuesc                 P    [        U 6  Vs/ s H  n[        U5      PM     sn$ s  snf )zG
Return the maximum value across all indices of an iterable of values.
)zipro   )r   values_is     r,   max_across_indicesr     s$     +.v,7,hCM,777s   #c                 .   U[         R                  :X  a+  [        U  Vs/ s H  o"R                  PM     sn5      u  p4nXE4$ U[         R                  :X  a+  [        U  Vs/ s H  o"R                  PM     sn5      u  pEnXE4$ [        SU 35      es  snf s  snf )z@
Get the maximum height and width across all images in a batch.
z"Invalid channel dimension format: )r   r(   r   r   r)   rE   )rL   r   r:   _r   r   s         r,   get_max_height_widthr     s     ,222#5F6SFSyyF6S#T y
 ""	 
.33	3#5F6SFSyyF6S#T 
q "" =>O=PQRR	 7T6Ss   BB
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r6   dictrZ   re   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r5   )r   rO   anns     r,   rP   3valid_coco_detection_annotations.<locals>.<genexpr>  s     N+31#66+rR   rS   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r5   )r   r   s     r,   rP   2valid_coco_panoptic_annotations.<locals>.<genexpr>  s     M#055rR   rS   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      (       aF  [
        R                  R                  [        [        R                  " XS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 O4[        U [
        R                  R                  5      (       d  [+        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.
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_imager6   str
startswithr7   r8   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 IINN7599U^b+c+k+k#lmEWW^^E""IINN5)E..C(+((8		ws|4
 syy// D
 	
 LL''.EMM% EL    i  jo  ip  p~  @  ~A  B s   	AF< <
GGG)torchvision)backendsc                    SSK n[        U [        5      (       GaN  U R                  S5      (       d  U R                  S5      (       a[  [        R
                  " XSS9R                  nUR                  " [        U5      UR                  S9n[        U[        R                  S9$ [        R                  R                  U 5      (       a  [        U [        R                  S9$ U R                  S	5      (       a  U R!                  S
5      S   n  ["        R$                  " U R'                  5       5      nUR                  " [        U5      UR                  S9n[        U[        R                  S9$ [        U [,        R.                  R.                  5      (       a9  [,        R0                  R3                  U 5      n [5        U R7                  S5      5      $ [9        S5      e! [(         a  n[+        SU  SU 35      eSnAff = f)aP  
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@   r6   r   r   r   r   r   
frombuffer	bytearrayrm   r   r   r   r   r   r   r   r   r   r   r   rE   r7   r8   r   r   r   r   r   )rH   r   r@   rawbufr   s         r,   load_image_as_tensorr     s   " %I&&%*:*::*F*F))ETJRRC""9S>EC-*;*;<<WW^^E""M,=,=>>..C(+((8
 ""9S>EC-*;*;<<	E399??	+	+++E2U]]5122n
 	
    i  jo  ip  p~  @  ~A  B s   	$G 
G6 G11G6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   )r6   rZ   re   r   r   )rL   r   image_grouprH   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pad_sizedo_center_crop	crop_size	do_resizesizeresamplePILImageResamplingr   c                     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`.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    , n,XYY(" u
 	
 +y/@kll)+WXX$*x/C\]] 0Dyr+   c                       \ rS rSrSrS rSS jrS rS\R                  S\
\-  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)ImageFeatureExtractionMixinio  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 zU which is not supported, only `PIL.Image.Image`, `np.ndarray` and `torch.Tensor` are.)r6   r7   r8   r[   r\   r   rE   rF   selfrH   s     r,   _ensure_format_supported4ImageFeatureExtractionMixin._ensure_format_supportedt  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   rA   r6   r[   r\   flatfloatingrt   r   	transposeastyperm   r7   r8   	fromarray)r  rH   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  r6   r7   r8   r   r  s     r,   convert_rgb'ImageFeatureExtractionMixin.convert_rgb  s;     	%%e,%11L}}U##r+   rH   scalerj   c                 ,    U R                  U5        X-  $ )z'
Rescale a numpy image by scale amount
)r  )r  rH   r!  s      r,   r  #ImageFeatureExtractionMixin.rescale  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  r6   r7   r8   r[   r   r   rA   r  integerr  r  float32rt   r  )r  rH   r  channel_firsts       r,   r   *ImageFeatureExtractionMixin.to_numpy_array  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   rW   )r  r6   r7   r8   r   	unsqueezer[   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  r6   r7   r8   r   r[   r\   r  r  r'  r   floatr   rl   r@   r^   
from_numpytensorrt   r   )r  rH   meanstdr  r@   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  r6   r7   r8   r  rZ   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,   r8  "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;   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	   N.r   )r   r   )r  r6   re   r   r[   r\   rt   r,  r   r  r7   r8   cropr  permutero   
zeros_like	new_zerosrn   )r  rH   r  image_shapetopbottomleftrightr(  	new_shape	new_imagetop_pad
bottom_padleft_pad	right_pads                  r,   center_crop'ImageFeatureExtractionMixin.center_cropY  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&0 %,,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  r6   r7   r8   r   r  s     r,   flip_channel_order.ImageFeatureExtractionMixin.flip_channel_order  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)r7   r8   NEARESTr  r6   r  rotate)r  rH   angler	  rW  rX  rY  rZ  s           r,   r\  "ImageFeatureExtractionMixin.rotate  sj      (389J9J%%e,%11%%e,E||Vi  
 	
r+   r#   r5   )NT)F)NTN)Nr   NNN)r$   r%   r&   r'   __doc__r  r  r  r[   r\   r/  r   r  r   r,  r4  r8  rQ  rT  r\  r*   r#   r+   r,   r  r  o  se    <$RZZ  

 @(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.)rE   formatr.   r2   r   r3   r   )r`  ra  r   s      r,   validate_annotationsrd    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: rs   )set
differencejoinr   r   )re  rf  unused_keysunused_key_strs       r,   validate_kwargsrm    sJ    o&11#6J2KLK;/88HJK r+   c                       \ rS rSr% SrSr\S-  \S'   Sr\S-  \S'   Sr	\S-  \S'   Sr
\S-  \S'   Sr\S-  \S'   Sr\S-  \S	'   S
 rSS jrS rS rS rS rS rSS jrS\4S j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)NKey z not found in SizeDict.)hasattrgetattrKeyErrorr  keys     r,   __getitem__SizeDict.__getitem__  s-    44%%cU"9:;;r+   c                 T    [        X5      (       a  [        X5      b  [        X5      $ U$ r5   rt  ru  )r  rx  defaults      r,   r   SizeDict.get  s'    4'$"4"@4%%r+   c              #      #    [        U 5       H-  n[        XR                  5      nUc  M  UR                  U4v   M/     g 7fr5   )r   ru  name)r  fvals      r,   __iter__SizeDict.__iter__  s4     A$'Cffck! s
   %A A c                     [        U R                  U R                  U R                  U R                  U R
                  U R                  45      $ r5   )hashr   r   rp  rq  r   r   )r  s    r,   __hash__SizeDict.__hash__  s<    T[[$**d.?.?ASASUYUdUdfjftftuvvr+   c                 @    [        X5      =(       a    [        X5      S L$ r5   r|  rw  s     r,   __contains__SizeDict.__contains__  s    t!Dgd&8&DDr+   c                 n    [        X5      (       d  [        SU S35      e[        R                  XU5        g )Nrs  z" is not a valid field of SizeDict.)rt  rv  object__setattr__)r  rx  values      r,   __setitem__SizeDict.__setitem__  s2    t!!T#&HIJJ4e,r+   c                    ^ ^ [        T[        5      (       a  [        T 5      T:H  $ [        T[        5      (       a=  [        U 4S j[	        T 5       5       5      [        U4S j[	        T 5       5       5      :H  $ [
        $ )Nc              3   P   >#    U  H  n[        TUR                  5      v   M     g 7fr5   ru  r  )rO   r  r  s     r,   rP   "SizeDict.__eq__.<locals>.<genexpr>  s     E1qvv..   #&c              3   P   >#    U  H  n[        TUR                  5      v   M     g 7fr5   r  )rO   r  others     r,   rP   r    s#      O0<1qvv&&r  )r6   r   ro  re   r   NotImplemented)r  r  s   ``r,   __eq__SizeDict.__eq__  sk    eT"":&&eX&&EtEE O06tO J   r+   rj   c                     [        U[        [        -  5      (       a0  [        U 5      nUR                  [        U5      5        [        S0 UD6$ [        $ )Nr#   )r6   r   ro  updater  r  r  mergeds      r,   __or__SizeDict.__or__!  s@    eTH_--$ZFMM$u+&%f%%r+   c                     [        U[        5      (       a'  [        U5      nUR                  [        U 5      5        U$ [        $ r5   )r6   r   r  r  r  s      r,   __ror__SizeDict.__ror__(  s3    eT""%[FMM$t*%Mr+   r#   r5   )rj   ro  )r$   r%   r&   r'   r_  r   r   __annotations__r   rp  rq  r   r   ry  r   r  r  r  r  r  r  r   r  r*   r#   r+   r,   ro  ro    s     FC$JE3:#L#*# $M3:$!Jd
! IsTz <

"wE-
 r+   ro  )r   r5   )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.Imager7   PIL.ImageOpsr8   
Resamplingr
  torchvision.ior   r   torchvision.transformsr   !torchvision.transforms.functionalr   r[  NEAREST_EXACTBOXr7  HAMMINGBICUBICLANCZOSpil_torch_interpolation_mappingitemstorch_pil_interpolation_mappingr@   
get_loggerr$   r   r\   rZ   
ImageInputr   r.   r   r   r   AnnotationTyper;   r=   rI   rK   rU   rc   rf   ri   boolrp   ru   r   r   r   re   r   r   r   r   r   r(   r   r   r   r   r   r/  r   r   r   r  r  rd  rm  ro  )kvs   00r,   <module>r     s    	 $ )    
 
 
  ) --:8? 	""$5$C$C 1 5 5##%6%?%?""$5$=$=""$5$=$=""$5$=$='# 9X8]8]8_&`8_qt8_&`#&(#&(#  
		H	% rzz>48I3JDQSQ[Q[L\^bcq^rr

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