
    9id              
          d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlZd dlZd dlmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZm Z   e       rtd dl!Z"d dl#Z" ejH                   ejH                  e"jJ                        jL                         ejH                  d      k\  re"jN                  jP                  Z)ne"jN                  Z)er e       rd dl*Z*ed	ejV                  d
ed	   eejV                     ed
   f   Z, G d de      Z-d Z.d Z/d Z0d Z1dejV                  de2fdZ3d(de4dee,   fdZ5dejV                  fdZ6	 d)dejV                  de	ee4e
e4df   f      de-fdZ7	 d)dejV                  de	ee-e8f      de4fdZ9d)dejV                  de-de
e4e4f   fdZ:dee8eee
f   f   de2fdZ;dee8eee
f   f   de2fd Z<d!eee8eee
f   f      de2fd"Z=d!eee8eee
f   f      de2fd#Z>d)dee8d	f   d$e	e?   dd	fd%Z@ G d& d'      ZAy)*    N)BytesIO)TYPE_CHECKINGDictIterableListOptionalTupleUnion)version   )ExplicitEnumis_jax_tensoris_tf_tensoris_torch_availableis_torch_tensoris_vision_availablerequires_backendsto_numpy)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDIMAGENET_STANDARD_MEANIMAGENET_STANDARD_STDOPENAI_CLIP_MEANOPENAI_CLIP_STDz9.1.0zPIL.Image.Imageztorch.Tensorc                       e Zd ZdZdZy)ChannelDimensionchannels_firstchannels_lastN)__name__
__module____qualname__FIRSTLAST     b/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/transformers/image_utils.pyr   r   @   s    EDr%   r   c                 b    t               xr$ t        | t        j                  j                        S N)r   
isinstancePILImageimgs    r&   is_pil_imager.   E   s     EZSYY__%EEr%   c                     t               xr$ t        | t        j                  j                        xsA t        | t        j
                        xs% t        |       xs t        |       xs t        |       S r(   )	r   r)   r*   r+   npndarrayr   r   r   r,   s    r&   is_valid_imager2   I   s_    			C:c399??#C 	c2::&	3	 	 r%   c                 r    t        | t        t        f      r| D ]  }t        |      r y yt	        |       syy)NFT)r)   listtuplevalid_imagesr2   )imgsr-   s     r&   r6   r6   S   s?    $u& 	C$	  D!r%   c                 L    t        | t        t        f      rt        | d         S y)Nr   F)r)   r4   r5   r2   r,   s    r&   
is_batchedr9   _   s"    #e}%c!f%%r%   imagereturnc                     | j                   t        j                  k(  ryt        j                  |       dk\  xr t        j                  |       dk  S )zV
    Checks to see whether the pixel values have already been rescaled to [0, 1].
    Fr   r   )dtyper0   uint8minmax)r:   s    r&   is_scaled_imagerA   e   s>     {{bhh 66%=A4"&&-1"44r%   expected_ndimsc           	      Z   t        |       r| S t        | t        j                  j                        r| gS t	        |       rU| j
                  |dz   k(  rt        |       } | S | j
                  |k(  r| g} | S t        d|dz    d| d| j
                   d      t        dt        |        d      )a  
    Ensure that the input is a list of images. If the input is a single image, it is converted to a list of length 1.
    If the input is a batch of images, it is converted to a list of images.

    Args:
        images (`ImageInput`):
            Image of images to turn into a list of images.
        expected_ndims (`int`, *optional*, defaults to 3):
            Expected number of dimensions for a single input image. If the input image has a different number of
            dimensions, an error is raised.
    r   z%Invalid image shape. Expected either z or z dimensions, but got z dimensions.ztInvalid image type. Expected either PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray, but got .)	r9   r)   r*   r+   r2   ndimr4   
ValueErrortype)imagesrB   s     r&   make_list_of_imagesrI   p   s     & &#))//*xf;;.1,,&\F  [[N*XF 	 78J7K4P^O_ `KK=. 
 	  $V~Q	0 r%   c                     t        |       st        dt        |              t               r9t	        | t
        j                  j                        rt        j                  |       S t        |       S )NzInvalid image type: )
r2   rF   rG   r   r)   r*   r+   r0   arrayr   r,   s    r&   to_numpy_arrayrL      sP    #/S	{;<<C!Axx}C=r%   num_channels.c                 V   ||nd}t        |t              r|fn|}| j                  dk(  rd\  }}n-| j                  dk(  rd\  }}nt        d| j                         | 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.
    r      rP   )r         z(Unsupported number of image dimensions: z(Unable to infer channel dimension format)r)   intrE   rF   shaper   r"   r#   )r:   rM   	first_dimlast_dims       r&   infer_channel_dimension_formatrW      s     $0#;<L&0s&CL?LzzQ"	8	q"	8CEJJ<PQQ{{9-%%%	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.
    rP   r   Unsupported data format: )rW   r   r"   rE   r#   rF   )r:   rX   s     r&   get_channel_dimension_axisr[      sd      :5A,222zzA~	.33	3zzA~
01B0CD
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.
    rZ   )rW   r   r"   rT   r#   rF   )r:   r\   s     r&   get_image_sizera      s{     4U;&,,,{{2B//	(--	-{{2B//4[MBCCr%   
annotationc                     t        | t              rId| v rEd| v rAt        | d   t        t        f      r(t	        | d         dk(  st        | d   d   t              ryy)Nimage_idannotationsr   TFr)   dictr4   r5   lenrb   s    r&   "is_valid_annotation_coco_detectionrj      s_    :t$*$Z'z-04-@ 
=)*a/*]3A6= r%   c                     t        | t              rMd| v rId| v rEd| v rAt        | d   t        t        f      r(t	        | d         dk(  st        | d   d   t              ryy)Nrd   segments_info	file_namer   TFrf   ri   s    r&   !is_valid_annotation_coco_panopticrn      sg    :t$*$z):%z/2T5MB 
?+,1*_5a8$? r%   re   c                 &    t        d | D              S )Nc              3   2   K   | ]  }t        |        y wr(   )rj   .0anns     r&   	<genexpr>z3valid_coco_detection_annotations.<locals>.<genexpr>  s     N31#6N   allre   s    r&    valid_coco_detection_annotationsry     s    N+NNNr%   c                 &    t        d | D              S )Nc              3   2   K   | ]  }t        |        y wr(   )rn   rq   s     r&   rt   z2valid_coco_panoptic_annotations.<locals>.<genexpr>  s     M#05Mru   rv   rx   s    r&   valid_coco_panoptic_annotationsr|     s    MMMMr%   timeoutc                 `   t        t        dg       t        | t              r| j	                  d      s| j	                  d      r@t
        j                  j                  t        j                  | d|      j                        } nt        j                  j                  |       r t
        j                  j                  |       } n| j	                  d      r| j                  d      d   } 	 t        j                   | d	      }t
        j                  j                  t#        |            } n2t        | t
        j                  j                        r| } nt'        d      t
        j(                  j+                  |       } | j-                  d      } | S # t$        $ r}t'        d
|  d|       d}~ww xY w)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.
    visionzhttp://zhttps://T)streamr}   zdata:image/,r   )validatezIncorrect 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_imager)   str
startswithr*   r+   openrequestsgetrawospathisfilesplitbase64	b64decoder   	ExceptionrF   ImageOpsexif_transposeconvert)r:   r}   b64es       r&   r   r     so    j8*-%I&%*:*::*F IINN8<<dG#T#X#XYEWW^^E"IINN5)E.C(+&&ut<		ws|4
 
E399??	+ D
 	
 LL''.EMM% EL    i  jo  ip  p~  @  ~A  B s   *?F 	F-F((F-c                       e Zd ZdZd ZddZd Zdej                  de	e
ef   dej                  fd	Zdd
Zd ZddZddZd Zd ZddZy)ImageFeatureExtractionMixinzD
    Mixin that contain utilities for preparing image features.
    c                     t        |t        j                  j                  t        j                  f      s$t        |      st        dt        |       d      y y )Nz	Got type zS which is not supported, only `PIL.Image.Image`, `np.array` and `torch.Tensor` are.)r)   r*   r+   r0   r1   r   rF   rG   selfr:   s     r&   _ensure_format_supportedz4ImageFeatureExtractionMixin._ensure_format_supportedK  sQ    %#))//2::!>?X]H^DK= )& &  I_?r%   Nc                    | j                  |       t        |      r|j                         }t        |t        j
                        r|'t        |j                  d   t        j                        }|j                  dk(  r$|j                  d   dv r|j                  ddd      }|r|dz  }|j                  t        j                        }t        j                  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.
        r   rP   rO   r   rQ      )r   r   numpyr)   r0   r1   flatfloatingrE   rT   	transposeastyper>   r*   r+   	fromarray)r   r:   rescales      r&   to_pil_imagez(ImageFeatureExtractionMixin.to_pil_imageR  s     	%%e,5!KKMEeRZZ($UZZ]BKK@zzQ5;;q>V#;1a0LL*E99&&u--r%   c                     | j                  |       t        |t        j                  j                        s|S |j	                  d      S )z
        Converts `PIL.Image.Image` to RGB format.

        Args:
            image (`PIL.Image.Image`):
                The image to convert.
        r   )r   r)   r*   r+   r   r   s     r&   convert_rgbz'ImageFeatureExtractionMixin.convert_rgbp  s8     	%%e,%1L}}U##r%   r:   scaler;   c                 .    | j                  |       ||z  S )z7
        Rescale a numpy image by scale amount
        )r   )r   r:   r   s      r&   r   z#ImageFeatureExtractionMixin.rescale~  s     	%%e,u}r%   c                    | j                  |       t        |t        j                  j                        rt	        j
                  |      }t        |      r|j                         }|'t        |j                  d   t        j                        n|}|r/| j                  |j                  t        j                        d      }|r"|j                  dk(  r|j                  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.
        r   p?rP   rQ   r   )r   r)   r*   r+   r0   rK   r   r   r   integerr   r   float32rE   r   )r   r:   r   channel_firsts       r&   rL   z*ImageFeatureExtractionMixin.to_numpy_array  s     	%%e,eSYY__-HHUOE5!KKME;B?*UZZ]BJJ7PWLLbjj!99EEUZZ1_OOAq!,Er%   c                     | j                  |       t        |t        j                  j                        r|S t	        |      r|j                  d      }|S 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   )axis)r   r)   r*   r+   r   	unsqueezer0   expand_dimsr   s     r&   r   z'ImageFeatureExtractionMixin.expand_dims  s_     	%%e, eSYY__-L5!OOA&E  NN5q1Er%   c                    | j                  |       t        |t        j                  j                        r| j	                  |d      }nw|rut        |t
        j                        r0| j                  |j                  t
        j                        d      }n+t        |      r | j                  |j                         d      }t        |t
        j                        rt        |t
        j                        s.t        j                  |      j                  |j                        }t        |t
        j                        st        j                  |      j                  |j                        }n_t        |      rTddl}t        ||j                        s |j                   |      }t        ||j                        s |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   NrP   rO   )r   r)   r*   r+   rL   r0   r1   r   r   r   r   floatrK   r=   torchTensortensorrE   rT   )r   r:   meanstdr   r   s         r&   	normalizez%ImageFeatureExtractionMixin.normalize  s     	%%e,eSYY__-''t'<E %,U\\"**%=yI 'U[[]I>eRZZ(dBJJ/xx~,,U[[9c2::.hhsm**5;;7U#dELL1#u||D)c5<<0"ell3'::?u{{1~7DD$//3q$}3EEEDLC''r%   c                    ||nt         j                  }| j                  |       t        |t        j
                  j
                        s| j                  |      }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        }}|.||
k  rt        d| d|       ||kD  rt        ||z  |z        |}}||k  r||fn||f}|j                  ||      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`.
        r   r   zmax_size = zN must be strictly greater than the requested size for the smaller edge size = )resample)PILImageResamplingBILINEARr   r)   r*   r+   r   r4   r5   rS   rh   sizerF   resize)r   r:   r   r   default_to_squaremax_sizewidthheightshortlongrequested_new_short	new_shortnew_longs                r&   r   z"ImageFeatureExtractionMixin.resize  sy   <  (389K9T9T%%e,%1%%e,EdD!;DdC CIN '1$'<d|47DQRGBT %

v16&ufovuot.8s.Cda#// L&93?RUY?Y\a?a;b8	'#66()( 4@@DvG   (*.1(Y2F2Q.RT\8	05	8,hPYEZ||D8|44r%   c                    | j                  |       t        |t              s||f}t        |      st        |t        j
                        rP|j                  dk(  r| j                  |      }|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|j                  ||||f      S |j                  d   dv rdnd}|sKt        |t        j
                        r|j                  ddd      }t        |      r|j                  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|j#                  |	      }
|	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).
        rQ   r   rO   r   NTF.r^   )rT   r_   )r   r)   r5   r   r0   r1   rE   r   rT   r   r*   r+   cropr   permuter@   
zeros_like	new_zerosr?   )r   r:   r   image_shapetopbottomleftrightr   	new_shape	new_imagetop_pad
bottom_padleft_pad	right_pads                  r&   center_cropz'ImageFeatureExtractionMixin.center_crop*  s    	%%e,$&$<D 5!Zrzz%BzzQ((/-2[[^v-E%++ab/5;;WYXY?K ::a=%**Q-8K1~Q'A-tAwAa(Q.tAw eSYY__-::tS%899 !&A& 8e %,1a0u%aA. !8+a.0TQY5KXYNCZc&j$u*455 KK$DG[^(Dc$q'S^_`SaFb'cc	eRZZ(e9=IU#	2IR=;q>1a7{1~-
bMKN2q8{1~-	AF	#wz)8I+==>w'Qs9??2#6??QPST]TcTcdfTginPoAoo
	 r%   c                     | j                  |       t        |t        j                  j                        r| j	                  |      }|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   r)   r*   r+   rL   r   s     r&   flip_channel_orderz.ImageFeatureExtractionMixin.flip_channel_orderu  sI     	%%e,eSYY__-''.ETrT1aZ  r%   c                     ||nt         j                  j                  }| j                  |       t	        |t         j                  j                        s| j                  |      }|j                  ||||||      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`.
        )r   expandcenter	translate	fillcolor)r*   r+   NEARESTr   r)   r   rotate)r   r:   angler   r   r   r   r   s           r&   r   z"ImageFeatureExtractionMixin.rotate  sn      (389J9J%%e,%1%%e,E||HVFicl  
 	
r%   r(   )NT)F)NTN)Nr   NNN)r   r    r!   __doc__r   r   r   r0   r1   r
   r   rS   r   rL   r   r   r   r   r   r   r$   r%   r&   r   r   F  sj    <$RZZ eSj0A bjj @(,(\A5FIV!"
r%   r   )rP   r(   )Br   r   ior   typingr   r   r   r   r   r	   r
   r   r0   r   	packagingr   utilsr   r   r   r   r   r   r   r   utils.constantsr   r   r   r   r   r   	PIL.Imager*   PIL.ImageOpsparse__version__base_versionr+   
Resamplingr   r   r1   
ImageInputr   r.   r2   r6   r9   boolrA   rS   rI   rL   rW   r   r[   ra   rj   rn   ry   r|   r   r   r   r$   r%   r&   <module>r      s     	  N N N   	 	 	  w}}]W]]3??3@@A]W]]SZE[[ YY11 YY rzz>48I3JDQSQ[Q[L\^bcq^rr

| 
F	52:: 5$ 5$ $D<L $N2::  NRA::A%-eCsCx4H.I%JAAB TXF::F*259I39N3O*PFF0D"** D3C DuUXZ]U] D0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V
 V
r%   