
    rh'                     ~   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	 d dl
ZddlmZmZmZmZmZ ddlmZmZmZmZmZ dd	lmZmZmZmZmZ  e       r
d dlZdd
lmZ  e       rd dl Z  e       rd dl!Z" e       rd dl#m
Z$ 	 dMdejJ                  de	ee&f   dee	ee&f      dejJ                  fdZ'dejP                  dfdejJ                  de)dee   dejT                  dee	e&ef      dejJ                  fdZ+d Z,	 	 	 dNde	ejJ                  ddddf   dee-   dee&   dee	e&ef      ddf
dZ.dMde/e0e0f   fdZ1	 	 	 dOdejJ                  de	e0e/e0e0f   e2e0   e/e0   f   d e-d!ee0   dee	e&ef      de/fd"Z3	 	 	 	 	 dPdejJ                  de/e0e0f   d#d$d%ee0   dee   d&e-dee	e&ef      dejJ                  fd'Z4	 	 dQdejJ                  d(e	e)ee)   f   d)e	e)ee)   f   dee   dee	e&ef      dejJ                  fd*Z5	 	 dQdejJ                  de/e0e0f   dee	e&ef      dee	e&ef      dejJ                  f
d+Z6dRd-Z7d,ejJ                  dejJ                  fd.Z8dSd/Z9d,edefd0Z:dTd2Z;d1ejJ                  dejJ                  fd3Z<dUd4Z=d1edefd5Z>d6 Z?d7 Z@ G d8 d9e      ZAeAj                  d:ddfdejJ                  d;e	e0e/e0e0f   ee/e0e0f      f   d<eAd=e	e)ee)   f   dee	e&ef      dee	e&ef      dejJ                  fd>ZCdedefd?ZD	 	 dQdejJ                  dee   dee	e&ef      dejJ                  fd@ZEdA ZFdVdBe-fdCZGdD ZH	 dVdEe	e2d   df   dFe-dBe-de/eIe/e0e0f   e2d   f   eIe	e0e/e0e0f   f   e/e/e0e0f   e0f   f   f   fdGZJ	 dVdHeIe/e0e0f   df   dIeIe	e0e/e0e0f   f   e/e/e0e0f   e0f   f   dBe-de	e2d   df   fdJZK G dK dL      ZLy)W    )defaultdict)
CollectionIterable)ceil)OptionalUnionN   )ChannelDimension
ImageInputget_channel_dimension_axisget_image_sizeinfer_channel_dimension_format)ExplicitEnum
TensorTypeis_jax_tensoris_tf_tensoris_torch_tensor)is_flax_availableis_tf_availableis_torch_availableis_vision_availablerequires_backends)PILImageResamplingimagechannel_diminput_channel_dimreturnc                    t        | t        j                        st        dt	        |              |t        |       }t        |      }||k(  r| S |t        j                  k(  rat        t        | j                  dz
              | j                  dz
  | j                  dz
  | j                  dz
  gz   }| j                  |      } | S |t        j                  k(  rat        t        | j                  dz
              | j                  dz
  | j                  dz
  | j                  dz
  gz   }| j                  |      } | S t        d|       )a  
    Converts `image` to the channel dimension format specified by `channel_dim`. The input
    can have arbitrary number of leading dimensions. Only last three dimension will be permuted
    to format the `image`.

    Args:
        image (`numpy.ndarray`):
            The image to have its channel dimension set.
        channel_dim (`ChannelDimension`):
            The channel dimension format to use.
        input_channel_dim (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If not provided, it will be inferred from the input image.

    Returns:
        `np.ndarray`: The image with the channel dimension set to `channel_dim`.
    ,Input image must be of type np.ndarray, got    r	      z&Unsupported channel dimension format: )
isinstancenpndarray	TypeErrortyper   r
   FIRSTlistrangendim	transposeLAST
ValueError)r   r   r   target_channel_dimaxess        p/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/image_transforms.pyto_channel_dimension_formatr1   6   s*   * eRZZ(FtE{mTUU :5A)+6..-333E%**q.)*ejj1nejj1nejj[\n-]]% L 
/44	4E%**q.)*ejj1nejj1nejj[\n-]]% L A+OPP    scaledata_formatdtypeinput_data_formatc                     t        | t        j                        st        dt	        |              | j                  t        j                        |z  }|t        |||      }|j                  |      }|S )a  
    Rescales `image` by `scale`.

    Args:
        image (`np.ndarray`):
            The image to rescale.
        scale (`float`):
            The scale to use for rescaling the image.
        data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the image. If not provided, it will be the same as the input image.
        dtype (`np.dtype`, *optional*, defaults to `np.float32`):
            The dtype of the output image. Defaults to `np.float32`. Used for backwards compatibility with feature
            extractors.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If not provided, it will be inferred from the input image.

    Returns:
        `np.ndarray`: The rescaled image.
    r   )r"   r#   r$   r%   r&   astypefloat64r1   )r   r3   r4   r5   r6   rescaled_images         r0   rescaler;   a   sk    4 eRZZ(FtE{mTUU\\"**-5N4^[Rcd#**51Nr2   c                    | j                   t        j                  k(  rd}|S t        j                  | | j	                  t
                    rbt        j                  d| k        rt        j                  | dk        rd}|S t        d| j                          d| j                          d      t        j                  d| k        rt        j                  | dk        rd}|S t        d	| j                          d| j                          d      )
z
    Detects whether or not the image needs to be rescaled before being converted to a PIL image.

    The assumption is that if the image is of type `np.float` and all values are between 0 and 1, it needs to be
    rescaled.
    Fr      zZThe image to be converted to a PIL image contains values outside the range [0, 255], got [z, z%] which cannot be converted to uint8.r	   TzXThe image to be converted to a PIL image contains values outside the range [0, 1], got [)
r5   r#   uint8allcloser8   intallr-   minmax)r   
do_rescales     r0   _rescale_for_pil_conversionrE      s     {{bhh
   
UELL-	.66!u*"&&#"6J  		}Buyy{m3XZ  
U
	uz 2
 	 IIK=599;-/TV
 	
r2   zPIL.Image.Imagetorch.Tensor	tf.Tensorzjnp.ndarrayrD   
image_modec                    t        t        dg       t        | t        j                  j                        r| S t        |       st        |       r| j                         } nRt        |       rt        j                  |       } n1t        | t        j                        st        dt        |              t        | t        j                   |      } | j"                  d   dk(  rt        j$                  | d      n| } |t'        |       n|}|rt)        | d      } | j+                  t        j,                        } t        j                  j/                  | |      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` or `tf.Tensor`):
            The image to convert to the `PIL.Image` format.
        do_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 and casting to `int` would result in a loss of precision,
            and `False` otherwise.
        image_mode (`str`, *optional*):
            The mode to use for the PIL image. If unset, will use the default mode for the input image type.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.

    Returns:
        `PIL.Image.Image`: The converted image.
    visionz Input image type not supported: r	   axisr=   mode)r   to_pil_imager"   PILImager   r   numpyr   r#   arrayr$   r-   r&   r1   r
   r,   shapesqueezerE   r;   r8   r>   	fromarray)r   rD   rH   r6   s       r0   rP   rP      s   2 lXJ/%) ue!4	u	rzz*;DK=IJJ (/?/D/DFWXE +0++b/Q*>BJJu2&EE 8B7I,U3zJs#LL"E99u:66r2   c                    | \  }}d}|St        t        ||f            }t        t        ||f            }||z  |z  |kD  r||z  |z  }t        t	        |            }||k  r||k(  s
||k  r||k(  r||}	}||	fS ||k  r0|}	||t        ||z  |z        }||	fS t        ||z  |z        }||	fS |}||t        ||z  |z        }	||	fS t        ||z  |z        }	||	fS )aC  
    Computes the output image size given the input image size and the desired output size.

    Args:
        image_size (`tuple[int, int]`):
            The input image size.
        size (`int`):
            The desired output size.
        max_size (`int`, *optional*):
            The maximum allowed output size.
    N)floatrB   rC   r@   round)

image_sizesizemax_sizeheightwidthraw_sizemin_original_sizemax_original_sizeohows
             r0   get_size_with_aspect_ratiore      sF    MFEH!#vuo"67!#vuo"670047(B"336GGHuX'D%FdNETMB 8O 
H$8X&./B 8O TF]U*+B 8O H$8X%./B 8O TE\F*+B8Or2   input_imager\   default_to_squarer]   c                    t        |t        t        f      r8t        |      dk(  rt        |      S t        |      dk(  r|d   }nt	        d      |r||fS t        | |      \  }}||k  r||fn||f\  }}|}	|	t        |	|z  |z        }}
|.||	k  rt	        d| d|       ||kD  rt        ||
z  |z        |}}
||k  r||
fS |
|fS )a  
    Find the target (height, width) dimension of the output image after resizing given the input image and the desired
    size.

    Args:
        input_image (`np.ndarray`):
            The image to resize.
        size (`int` or `tuple[int, int]` or list[int] or `tuple[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).
        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*):
            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`.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.

    Returns:
        `tuple`: The target (height, width) dimension of the output image after resizing.
    r!   r	   r   z7size must have 1 or 2 elements if it is a list or tuplezmax_size = zN must be strictly greater than the requested size for the smaller edge size = )r"   tupler(   lenr-   r   r@   )rf   r\   rg   r]   r6   r^   r_   shortlongrequested_new_short	new_shortnew_longs               r0   get_resize_output_image_sizerp      s   J $&t9>;Y!^7DVWWd|";0ABMFE%*f_5&/65/KE4-s3F3MPU3U/VxI**hZ (4486;  h"%h&:X&E"FxI$)VOHi N)X9NNr2   resampler   reducing_gapreturn_numpyc                 2   t        t        dg       ||nt        j                  }t	        |      dk(  st        d      |t        |       }||n|}d}t        | t        j                  j                        st        |       }t        | ||      } |\  }}	| j                  |	|f||      }
|rit        j                  |
      }
|
j                  dk(  rt        j                  |
d      n|
}
t!        |
|t"        j$                  	      }
|rt'        |
d
      n|
}
|
S )a  
    Resizes `image` to `(height, width)` specified by `size` using the PIL library.

    Args:
        image (`np.ndarray`):
            The image to resize.
        size (`tuple[int, int]`):
            The size to use for resizing the image.
        resample (`int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
            The filter to user for resampling.
        reducing_gap (`int`, *optional*):
            Apply optimization by resizing the image in two steps. The bigger `reducing_gap`, the closer the result to
            the fair resampling. See corresponding Pillow documentation for more details.
        data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the output image. If unset, will use the inferred format from the input.
        return_numpy (`bool`, *optional*, defaults to `True`):
            Whether or not to return the resized image as a numpy array. If False a `PIL.Image.Image` object is
            returned.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.

    Returns:
        `np.ndarray`: The resized image.
    rJ   r!   zsize must have 2 elementsF)rD   r6   )rq   rr   rK   rL   r   gp?)r   resizer   BILINEARrj   r-   r   r"   rQ   rR   rE   rP   r#   rT   r*   expand_dimsr1   r
   r,   r;   )r   r\   rq   rr   r4   rs   r6   rD   r^   r_   resized_images              r0   rv   rv   C  s   B fxj)#/x5G5P5PHt9>455  :5A'2':#K JeSYY__-07
UzM^_MFELL%8R^L_M/ CPBTBTXYBY}2>_l3;:J:O:O

 <Fw7=r2   meanstdc                 N   t        | t        j                        st        d      |t	        |       }t        | |      }| j                  |   }t        j                  | j                  t        j                        s| j                  t        j                        } t        |t              r(t        |      |k7  r t        d| dt        |             |g|z  }t        j                  || j                        }t        |t              r(t        |      |k7  r t        d| dt        |             |g|z  }t        j                  || j                        }|t         j"                  k(  r	| |z
  |z  } n| j$                  |z
  |z  j$                  } |t'        | ||      } | S | } | S )a  
    Normalizes `image` using the mean and standard deviation specified by `mean` and `std`.

    image = (image - mean) / std

    Args:
        image (`np.ndarray`):
            The image to normalize.
        mean (`float` or `Collection[float]`):
            The mean to use for normalization.
        std (`float` or `Collection[float]`):
            The standard deviation to use for normalization.
        data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the output image. If unset, will use the inferred format from the input.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If unset, will use the inferred format from the input.
    zimage must be a numpy array)r6   zmean must have z$ elements if it is an iterable, got r5   zstd must have )r"   r#   r$   r%   r   r   rU   
issubdtyper5   floatingr8   float32r   rj   r-   rT   r
   r,   Tr1   )r   rz   r{   r4   r6   channel_axisnum_channelss          r0   	normalizer     s   0 eRZZ(566 :5A-eGXYL;;|,L ==bkk2RZZ($
#t9$|n<`adeiaj`klmmv$88D,D#z"s8|#~l^;_`cdg`h_ijkkel"
((3ekk
*C,111$''D.C'**R]Ri'{<MNEL puELr2   c                    t        t        dg       t        | t        j                        st        dt        |              t        |t              rt        |      dk7  rt        d      |t        |       }||n|}t        | t        j                  |      } t        | t        j                        \  }}|\  }}t        |      t        |      }}||z
  dz  }	|	|z   }
||z
  dz  }||z   }|	dk\  r8|
|k  r3|dk\  r.||k  r)| d|	|
||f   } t        | |t        j                        } | S t!        ||      }t!        ||      }| j"                  dd ||fz   }t        j$                  | |	      }t'        ||z
  dz        }||z   }t'        ||z
  dz        }||z   }| |d||||f<   |	|z  }	|
|z  }
||z  }||z  }|dt!        d|	      t)        ||
      t!        d|      t)        ||      f   }t        ||t        j                        }|S )
a  
    Crops the `image` to the specified `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 will always be of size `size`).

    Args:
        image (`np.ndarray`):
            The image to crop.
        size (`tuple[int, int]`):
            The target size for the cropped image.
        data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use the inferred format of the input image.
        input_data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use the inferred format of the input image.
    Returns:
        `np.ndarray`: The cropped image.
    rJ   r   r!   zOsize must have 2 elements representing the height and width of the output imageNr   .)rU   )r   center_cropr"   r#   r$   r%   r&   r   rj   r-   r   r1   r
   r'   r   r@   rC   rU   
zeros_liker   rB   )r   r\   r4   r6   output_data_formatorig_height
orig_widthcrop_height
crop_widthtopbottomleftright
new_height	new_width	new_shape	new_imagetop_pad
bottom_padleft_pad	right_pads                        r0   r   r     s]   8 kH:.eRZZ(FtE{mTUUdH%Tajkk :5A(3(?EV (/?/E/EGXYE,U4D4J4JKK"K!+.JK $
*C;F#)D:E axFk)daiEZ<Oc3v:tEz12+E3EGWG]G]^ [+.JJ
+ICR J	#::Ie95I J,12G;&JY+q01H:%I=BIc7:%x	'99:7NC
gFHD	XE#s1c{SV-DDc!TlUXYbdiUjFjjkI+I7IK[KaKabIr2   bboxes_centerc                     | j                  d      \  }}}}t        j                  |d|z  z
  |d|z  z
  |d|z  z   |d|z  z   gd      }|S )NrK         ?dimunbindtorchstack)r   center_xcenter_yr_   r^   bbox_cornerss         r0   _center_to_corners_format_torchr     si    (5(<(<R(@%Hhv;;
S5[
 HsV|$;x#PU+?UYadgjpdpYprL
 r2   c                     | j                   \  }}}}t        j                  |d|z  z
  |d|z  z
  |d|z  z   |d|z  z   gd      }|S )Nr   rK   rL   r   r#   r   r   r   r   r_   r^   bboxes_cornerss         r0   _center_to_corners_format_numpyr   %  sa    (5%HhvXX	C%K	C&L!8(S5[:PRZ]`ci]iRijN
 r2   c                     t        j                  | d      \  }}}}t        j                  |d|z  z
  |d|z  z
  |d|z  z   |d|z  z   gd      }|S )NrK   rL   r   tfunstackr   r   s         r0   _center_to_corners_format_tfr   /  sh    (*

=r(J%HhvXX	C%K	C&L!8(S5[:PRZ]`ci]iRijN
 r2   c                     t        |       rt        |       S t        | t        j                        rt        |       S t        |       rt        |       S t        dt        |              )a}  
    Converts bounding boxes from center format to corners format.

    center format: contains the coordinate for the center of the box and its width, height dimensions
        (center_x, center_y, width, height)
    corners format: contains the coordinates for the top-left and bottom-right corners of the box
        (top_left_x, top_left_y, bottom_right_x, bottom_right_y)
    Unsupported input type )
r   r   r"   r#   r$   r   r   r   r-   r&   )r   s    r0   center_to_corners_formatr   :  s\     }%.}==	M2::	..}==	m	$+M::
.tM/B.CD
EEr2   r   c                     | j                  d      \  }}}}||z   dz  ||z   dz  ||z
  ||z
  g}t        j                  |d      S )NrK   r!   r   r   )r   
top_left_x
top_left_ybottom_right_xbottom_right_ybs         r0   _corners_to_center_format_torchr   O  s`    =K=R=RSU=V:J
NN	n	$)	n	$)	*	$	*	$		A ;;qb!!r2   c                     | j                   \  }}}}t        j                  ||z   dz  ||z   dz  ||z
  ||z
  gd      }|S )Nr!   rK   rL   r   r   r   r   r   r   r   s         r0   _corners_to_center_format_numpyr   Z  s`    =K=M=M:J
NNHH.(A-.(A-j(j(		
 M r2   c                     t        j                  | d      \  }}}}t        j                  ||z   dz  ||z   dz  ||z
  ||z
  gd      }|S )NrK   rL   r!   r   r   s         r0   _corners_to_center_format_tfr   h  sf    =?ZZ]_=`:J
NNHH.(A-.(A-j(j(		
 M r2   c                     t        |       rt        |       S t        | t        j                        rt        |       S t        |       rt        |       S t        dt        |              )a  
    Converts bounding boxes from corners format to center format.

    corners format: contains the coordinates for the top-left and bottom-right corners of the box
        (top_left_x, top_left_y, bottom_right_x, bottom_right_y)
    center format: contains the coordinate for the center of the box and its the width, height dimensions
        (center_x, center_y, width, height)
    r   )
r   r   r"   r#   r$   r   r   r   r-   r&   )r   s    r0   corners_to_center_formatr   v  s\     ~&.~>>	NBJJ	/.~>>	n	%+N;;
.tN/C.DE
FFr2   c                 v   t        | t        j                        rt        | j                        dk(  rh| j
                  t        j                  k(  r| j                  t        j                        } | dddddf   d| dddddf   z  z   d| dddddf   z  z   S t        | d   d| d   z  z   d| d   z  z         S )z*
    Converts RGB color to unique ID.
    r    Nr      r	   i   r!   )
r"   r#   r$   rj   rU   r5   r>   r8   int32r@   )colors    r0   	rgb_to_idr     s     %$U[[)9Q)>;;"(("LL*EQ1W~eAq!Gn 44y5Aq>7QQQuQx#a.(9uQx+??@@r2   c                    t        | t        j                        rx| j                         }t	        t        | j                        dgz         }t        j                  |t        j                        }t        d      D ]  }|dz  |d|f<   |dz  } |S g }t        d      D ]  }|j                  | dz         | dz  }  |S )z*
    Converts unique ID to RGB color.
    r    r}   r   .)r"   r#   r$   copyri   r(   rU   zerosr>   r)   append)id_mapid_map_copy	rgb_shapergb_mapir   _s          r0   	id_to_rgbr     s     &"**%kkm$v||,s23	((9BHH5q 	 A)C/GCFOCK	  E1X Vc\"3 Lr2   c                        e Zd ZdZdZdZdZdZy)PaddingModezP
    Enum class for the different padding modes to use when padding images.
    constantreflect	replicate	symmetricN)__name__
__module____qualname____doc__CONSTANTREFLECT	REPLICATE	SYMMETRIC r2   r0   r   r     s     HGIIr2   r   g        paddingrO   constant_valuesc                     t                fd} ||      }|t        j                  k(  r" ||      }t        j                   |d|       n|t        j
                  k(  rt        j                   |d       nf|t        j                  k(  rt        j                   |d       n:|t        j                  k(  rt        j                   |d       nt        d|       |t         |        S    S )	a  
    Pads the `image` with the specified (height, width) `padding` and `mode`.

    Args:
        image (`np.ndarray`):
            The image to pad.
        padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`):
            Padding to apply to the edges of the height, width axes. Can be one of three formats:
            - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis.
            - `((before, after),)` yields same before and after pad for height and width.
            - `(pad,)` or int is a shortcut for before = after = pad width for all axes.
        mode (`PaddingMode`):
            The padding mode to use. Can be one of:
                - `"constant"`: pads with a constant value.
                - `"reflect"`: pads with the reflection of the vector mirrored on the first and last values of the
                  vector along each axis.
                - `"replicate"`: pads with the replication of the last value on the edge of the array along each axis.
                - `"symmetric"`: pads with the reflection of the vector mirrored along the edge of the array.
        constant_values (`float` or `Iterable[float]`, *optional*):
            The value to use for the padding if `mode` is `"constant"`.
        data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use same as the input image.
        input_data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use the inferred format of the input image.

    Returns:
        `np.ndarray`: The padded image.

    c                    t        | t        t        f      r	| | f| | ff} nt        | t              r#t	        |       dk(  r| d   | d   f| d   | d   ff} nxt        | t              r&t	        |       dk(  rt        | d   t              r| | f} nBt        | t              r$t	        |       dk(  rt        | d   t              r| } nt        d|        t        j                  k(  rdg| ng | d} j                  dk(  rdg| } | S | } | S )za
        Convert values to be in the format expected by np.pad based on the data format.
        r	   r   r!   zUnsupported format: )r   r      )	r"   r@   rY   ri   rj   r-   r
   r'   r*   )valuesr   r6   s    r0   _expand_for_data_formatz$pad.<locals>._expand_for_data_format  s!    fsEl+v&(89F&3v;!+;ay&),vay&).DEF&3v;!+;
6RS9VY@Zf%F&3v;!+;
6RS9V[@\F3F8<== '8;K;Q;Q&Q&"6"WhY_WhagWh ',jjAo&"6" <Br2   r   )rO   r   r   rN   edger   zInvalid padding mode: )
r   r   r   r#   padr   r   r   r-   r1   )r   r   rO   r   r4   r6   r   s   `    ` r0   r   r     s    V  :5A, &g.G{###1/BugJX	$$	$ugI6	&&	&ugF3	&&	&ugK81$899R]Ri'{<MNEL puELr2   c                     t        t        dg       t        | t        j                  j                        s| S | j
                  dk(  r| S | j                  d      } | S )z
    Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image
    as is.
    Args:
        image (Image):
            The image to convert.
    rJ   RGB)r   convert_to_rgbr"   rQ   rR   rO   convert)r   s    r0   r   r     sK     nxj1eSYY__-zzUMM% ELr2   c                     |t        |       n|}|t        j                  k(  r| ddddf   } n,|t        j                  k(  r| ddddf   } nt	        d|       |t        | ||      } | S )a  
    Flips the channel order of the image.

    If the image is in RGB format, it will be converted to BGR and vice versa.

    Args:
        image (`np.ndarray`):
            The image to flip.
        data_format (`ChannelDimension`, *optional*):
            The channel dimension format for the output image. Can be one of:
                - `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use same as the input image.
        input_data_format (`ChannelDimension`, *optional*):
            The channel dimension format for the input image. Can be one of:
                - `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            If unset, will use the inferred format of the input image.
    N.rK   zUnsupported channel dimension: ru   )r   r
   r,   r'   r-   r1   )r   r4   r6   s      r0   flip_channel_orderr   !  s    0 BSAZ6u=`q,111c4R4i 	.44	4ddCi :;L:MNOO+E;RcdLr2   c                 F    | j                         r| S | j                         S N)is_floating_pointrY   )xs    r0   _cast_tensor_to_floatr   G  s    779r2   	is_nestedc                    t        t              }i }|s| gn| } t        |       D ]Y  \  }}t        |      D ]F  \  }}|r||fn|}|j                  dd }	||	   j	                  |       |	t        ||	         dz
  f||<   H [ ||fS )zXHelper function to flatten a single level of nested image structures and group by shape.r	   N)r   r(   	enumeraterU   r   rj   )
nested_imagesr   grouped_imagesgrouped_images_indexr   sublistjr   keyrU   s
             r0   _group_images_by_shaper   M  s     &N+4]O-M. P
7!'* 	PHAu%1a&1CKKOE5!((/).N54I0JQ0N(O %		PP ///r2   c                 \   t        d | D              }dg|dz   z  }t        t              }| D ]  \  }}||   j                  |        t	        |dz         D ]R  }||v st        ||         }dg|dz   z  }t	        |dz         D ]  }||f| v s
| ||f   \  }	}
||	   |
   ||<     |||<   T |S )z?Helper function to reconstruct a single level nested structure.c              3   &   K   | ]	  }|d      yw)r   Nr   ).0idxs     r0   	<genexpr>z0_reconstruct_nested_structure.<locals>.<genexpr>_  s     23A2s   Nr	   )rC   r   r(   r   r)   )indicesprocessed_imagesmax_outer_idxresultnested_indicesr   r   inner_max_idx
inner_listrU   r  s              r0   _reconstruct_nested_structurer  \  s     2'22M V}q()F !&N $1q  #$ =1$% #q 12M=1#45J=1,- Aq6W$!(!QJE3$4U$;C$@JqMA #F1I# Mr2   imagesdisable_groupingc                    |(|r| d   d   j                   n| d   j                   }|dk(  }|r|rt        t        |             D ci c]8  }t        t        | |               D ]  }||f| |   |   j                  d       : c}}t        t        |             D ci c]'  }t        t        | |               D ]  }||f||fdf ) c}}fS t        t        |             D ci c]  }|| |   j                  d       c}t        t        |             D ci c]  }||df	 c}fS t	        | |      \  }}|j                         D 	ci c]  \  }}	|t        j                  |	d       }}}	||fS c c}}w c c}}w c c}w c c}w c c}	}w )a  
    Groups images by shape.
    Returns a dictionary with the shape as key and a list of images with that shape as value,
    and a dictionary with the index of the image in the original list as key and the shape and index in the grouped list as value.

    The function supports both flat lists of tensors and nested structures.
    The input must be either all flat or all nested, not a mix of both.

    Args:
        images (Union[list["torch.Tensor"], "torch.Tensor"]):
            A list of images or a single tensor
        disable_grouping (bool):
            Whether to disable grouping. If None, will be set to True if the images are on CPU, and False otherwise.
            This choice is based on empirical observations, as detailed here: https://github.com/huggingface/transformers/pull/38157
        is_nested (bool, *optional*, defaults to False):
            Whether the images are nested.

    Returns:
        tuple[dict[tuple[int, int], list["torch.Tensor"]], dict[Union[int, tuple[int, int]], tuple[tuple[int, int], int]]]:
            - A dictionary with shape as key and list of images with that shape as value
            - A dictionary mapping original indices to (shape, index) tuples
    r   cpur   )devicer)   rj   	unsqueezer   itemsr   r   )
r  r  r   r  r   r   r   r   rU   images_lists
             r0   group_images_by_shaper  v  s   < (11$$vay7G7G!U??DS[?Qq![`adeklmenao[pqVWQFF1IaL22155qFq-23v;-?t()sSYZ[S\~I^tDEA!Q#tt   8=S[7IJ!Avay**1--J`efijpfq`rLs[\QQRTUPVYLsss ,B&)+T(N( XfWkWkWmnASeU[[!<<nNn/// r t KLs os   =E3,E9$E?F
"F	r  r   c                     |s4t        t        |            D cg c]  }| ||   d      ||   d       c}S t        ||       S c c}w )a  
    Reconstructs images in the original order, preserving the original structure (nested or not).
    The input structure is either all flat or all nested.

    Args:
        processed_images (dict[tuple[int, int], "torch.Tensor"]):
            Dictionary mapping shapes to batched processed images.
        grouped_images_index (dict[Union[int, tuple[int, int]], tuple[tuple[int, int], int]]):
            Dictionary mapping original indices to (shape, index) tuples.
        is_nested (bool, *optional*, defaults to False):
            Whether the images are nested. Cannot be infered from the input, as some processing functions outputs nested images.
            even with non nested images,e.g functions splitting images into patches. We thus can't deduce is_nested from the input.


    Returns:
        Union[list["torch.Tensor"], "torch.Tensor"]:
            Images in the original structure.
    r   r	   )r)   rj   r  )r  r   r   r   s       r0   reorder_imagesr    sf    .  3345
 1!4Q789Ma9PQR9ST
 	

 ))=?OPP
s   Ac                   0    e Zd ZdZdej
                  fdZy)NumpyToTensorz4
    Convert a numpy array to a PyTorch tensor.
    r   c                 j    t        j                  |j                  ddd            j                         S )Nr!   r   r	   )r   
from_numpyr+   
contiguous)selfr   s     r0   __call__zNumpyToTensor.__call__  s+     1a 89DDFFr2   N)r   r   r   r   r#   r$   r  r   r2   r0   r  r    s    Gbjj Gr2   r  r   )NNN)TNN)NNNTN)NN)r   rF   r   rF   )r   rG   r   rG   )r   rF   r   rF   )r   rG   r   rG   )F)Mcollectionsr   collections.abcr   r   mathr   typingr   r   rS   r#   image_utilsr
   r   r   r   r   utilsr   r   r   r   r   utils.import_utilsr   r   r   r   r   rQ   r   r   
tensorflowr   	jax.numpyjnpr$   strr1   r   rY   r5   r;   rE   boolrP   ri   r@   re   r(   rp   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  dictr  r  r  r   r2   r0   <module>r+     s   $ 0  "   Z Y  / AE(::(',-(  &6&; <=( ZZ	(\ /3jj@D#::## *+# 88	#
  c+;&; <=# ZZ#L: "& $@D	37.]Z[3737 37  c+;&; <=	37
 37l$5c? $V #"@D@O@O
U38_d3is;
<@O @O sm	@O
  c+;&; <=@O @OL &*"&.2@DD::D
S/D #D 3-	D
 *+D D  c+;&; <=D ZZDV /3@D::::
z%((
): 
uj''	(: *+	:
  c+;&; <=: ZZ:@ ;?@D	Q::Q
S/Q %%5 567Q  c+;&; <=	Q
 ZZQh2:: "** FJ F: F*"BJJ 2:: GZ GJ G.A&,  $,,58:>@DS::S3c3h%S/)BBCS S 5(5/12	S
 %%5 567S  c+;&; <=S ZZSn*  , /3@D#::#*+#  c+;&; <=# ZZ	#L0T 0: 00$~&670000 00 sCx$~.	./eCsCx<P6QSXY^_bdg_gYhjmYmSn6n1oo	00l Q5c?N:;QuS%S/%9:E%S/SVBV<WWXQ Q 4/0	Q@G Gr2   