
    Z j 7                        S r SSKJr  SSKrSSKJr  SSKJr  SSK	J
r
  SSKJrJr  SS	KJrJrJrJrJr  SS
KJrJr  SSKJrJr  \" SS9S\S\S\\\\4      4S j5       r\" SS9S\\\4   S\\\4   S\S\S\\\4   4
S j5       rS\S\S\S\S\S\4S jr S\S\S\\\4   S\\\\\\4      4S jr!\" SS9 S*S \\\4   S!\S\S"\S\\\4   4
S# jj5       r" " S$ S%\S&S'9r#\ " S( S)\5      5       r$S)/r%g)+z Image processor class for OVIS2.    )	lru_cacheN)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)OPENAI_CLIP_MEANOPENAI_CLIP_STD
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstring
   )maxsizemin_image_tilesmax_image_tilesreturnc                     / n[        SUS-   5       H?  n[        SUS-   5       H)  nX4-  U::  d  M  X4-  U :  d  M  UR                  X445        M+     MA     [        US S9$ )zYComputes all allowed aspect ratios for a given minimum and maximum number of input tiles.   c                     U S   U S   -  $ Nr   r    xs    ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/ovis2/image_processing_ovis2.py<lambda>1get_all_supported_aspect_ratios.<locals>.<lambda>,   s    qtad{    key)rangeappendsorted)r   r   aspect_ratioswidthheights        r   get_all_supported_aspect_ratiosr+   $   sh     Mq/A-.A23F~0U^5V$$e_5 4 / -%:;;r"   d   original_image_sizetarget_tile_sizec                     [        X#5      nU u  pVUu  pxXe-  n	Xe-  n
[        S5      nSnU HI  nUS   US   -  n[        X-
  5      nX:  a  UnUnM&  X:X  d  M-  U
SU-  U-  US   -  US   -  :  d  MG  UnMK     U$ )zQFind the canvas with the closest aspect ratio to the original image aspect ratio.inf)r   r   r   r   g      ?)r+   floatabs)r-   r.   r   r   possible_tile_arrangementsoriginal_heightoriginal_widthtarget_tile_heighttarget_tile_widthaspect_ratioareabest_ratio_diff	best_gridgridgrid_aspect_ratio
ratio_diffs                   r   get_optimal_tiled_canvasr?   /   s     "A!b&9#O,<)!3L+DElOI* Gd1g-9:
'(OI*c..1BBT!WLtTUwVV 	 + r"   leftupperrightlowersidec                 `    X -
  nX1-
  n[        XV5      [        XV5      peXT:  a	  Xe-  U-  nUnXV-  $ N)maxmin)r@   rA   rB   rC   rD   whs          r   compute_patch_covering_arearK   K   s;    AAq9c!iqxEDL5Lr"   rJ   rI   r<   c                     XS   -  nXS   -  n[        US   5       VVs/ s HI  n[        US   5        H3  nXd-  XS-  XbS   S-
  :X  a  UOUS-   U-  XRS   S-
  :X  a  U OUS-   U-  4PM5     MK     snn$ s  snnf r   )r%   )rJ   rI   r<   
row_height	col_widthrowcols          r   split_image_into_gridrQ   U   s    1gJ!WI a>	 "Ca>C OQ!#A#'Y)>Q!#A#'Z)?		
 "	
 "	 	 	s   AA4
image_sizetarget_patch_sizecovering_thresholdc                 *  ^ U u  pEXT-  n[        SU5      n/ n/ n	U HR  n
[        XEU
5      n[        U4S jU 5       5      U-  nUR                  X45        X:  d  M@  U	R                  X45        MT     U	(       a  [	        U	S S9S   $ [	        US S9S   $ )Nr   c              3   >   >#    U  H  n[        / UQTP76 v   M     g 7frF   )rK   ).0regionrS   s     r   	<genexpr>-get_min_tile_covering_grid.<locals>.<genexpr>t   s"     cVbF+GVG5FGVbs   c                 0    U S   S   U S   S   -  U S   * 4$ r   r   r   s    r   r    ,get_min_tile_covering_grid.<locals>.<lambda>{   s(    QqT!WqtAw=NQRSTQUPU<Vr"   r#   r   c                 0    U S   * U S   S   U S   S   -  4$ )Nr   r   r   r   s    r   r    r\   |   s$    !uad1g!Q6G.Hr"   )r+   rQ   sumr&   rH   )rR   rS   r   rT   image_heightimage_width
image_areacandidate_tile_gridsevaluated_gridssufficient_covering_grids	tile_gridtile_regionstile_covering_ratios    `           r   get_min_tile_covering_gridrh   d   s     !+L+J:1oNO ")	,\	RcVbccfpp 	 		?@3%,,i-MN * !,2VWXYZZ$HI!LLr"   c                   B    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   Srg)	Ovis2ImageProcessorKwargs   a  
crop_to_patches (`bool`, *optional*, defaults to `False`):
    Whether to crop the image to patches. Can be overridden by the `crop_to_patches` parameter in the
    `preprocess` method.
min_patches (`int`, *optional*, defaults to 1):
    The minimum number of patches to be extracted from the image. Only has an effect if `crop_to_patches` is
    set to `True`. Can be overridden by the `min_patches` parameter in the `preprocess` method.
max_patches (`int`, *optional*, defaults to 12):
    The maximum number of patches to be extracted from the image. Only has an effect if `crop_to_patches` is
    set to `True`. Can be overridden by the `max_patches` parameter in the `preprocess` method.
use_covering_area_grid (`bool`, *optional*, defaults to `True`):
    Whether to use the covering area grid to determine the number of patches. Only has an effect if
    `crop_to_patches` is set to `True`. Can be overridden by the `use_covering_area_grid` parameter in the
    `preprocess` method.
crop_to_patchesmin_patchesmax_patchesuse_covering_area_gridr   N)	__name__
__module____qualname____firstlineno____doc__bool__annotations__int__static_attributes__r   r"   r   rj   rj      s!        r"   rj   F)totalc            %         ^  \ rS rSr\R
                  r\r\	r
SSS.rSrSrSrSrSrSrSrSrSr\rS\\   4U 4S	 jjr\S
\S\\   S\4U 4S jj5       r    S%S
SS\S\S\S\S\ S-  SS4S jjr!    S&S
\"S   S\S\ SSS\S\ S\S\S\S\\"\   -  S-  S\\"\   -  S-  S \S-  S!\#\$-  S-  S"\S\S\S\S\4$S# jjr%S$r&U =r'$ )'Ovis2ImageProcessor   i  r*   r)   TFr      kwargsc                 &   > [         TU ]  " S0 UD6  g )Nr   )super__init__)selfr   	__class__s     r   r   Ovis2ImageProcessor.__init__   s    "6"r"   imagesr   c                 &   > [         TU ]  " U40 UD6$ rF   )r   
preprocess)r   r   r   r   s      r   r   Ovis2ImageProcessor.preprocess   s    w!&3F33r"   Nztorch.Tensorrm   rn   ro   rT   
patch_sizeresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                    UR                   S   nUR                  UR                  pUR                   SS u  pU(       a  [        X4U	UUS9u  pO[	        X4X4X#5      u  pX-  nX-  nX-  nU R                  U[        UUS9US9n/ n[        U5       HN  nUU-  nUU-  nUU
-  UU	-  US-   U
-  US-   U	-  4nUSUS   US	   2US   US
   24   nUR                  U5        MP     [        U5      S:w  a"  U R                  XUS9nUR                  SU5        [        R                  " USS9R                  SS5      R                  5       n[        U5       Vs/ s H  nX/PM     nnUU4$ s  snf )a[  
Crop the images to patches and return a list of cropped images.
The number of patches and their grid arrangement are determined by the original image size,
the target patch size and the minimum and maximum number of patches.
The aspect ratio of the patches grid is chosen to be the closest to the original image aspect ratio.

Args:
    images (`torch.Tensor`):
        The images to be cropped.
    min_patches (`int`):
        The minimum number of patches to be extracted from the image.
    max_patches (`int`):
        The maximum number of patches to be extracted from the image.
    use_covering_area_grid (`bool`, *optional*, defaults to `True`):
        Whether to use the original OVIS2 approach: compute the minimal number of tiles that cover at least 90%
        of the image area. If `False`, the closest aspect ratio to the target is used.
    covering_threshold (`float`, *optional*, defaults to `0.9`):
        The threshold for the covering area. Only has an effect if `use_covering_area_grid` is set to `True`.
    patch_size (`SizeDict`, *optional*):
        The size of the output patches.
    resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*):
        Resampling filter to use if resizing the image.

Returns:
    Tuple[`torch.Tensor`, `list`]: A tuple containing the processed images tensor and the grid information.
r   N)rS   r   rT   r}   )r   r   .r      )dim)shaper*   r)   rh   r?   resizer   r%   r&   leninserttorchstack	transpose
contiguous)r   r   rm   rn   ro   rT   r   r   	num_imagepatch_size_heightpatch_size_widthr4   r5   num_columnsnum_rowstarget_widthtarget_height
num_blocksresized_imageprocessed_imagesicolumnrO   boxpatch_imagethumbnail_img_r<   s                               r   crop_image_to_patches)Ovis2ImageProcessor.crop_image_to_patches   s   H LLO	.8.?.?AQAQ+*0,,rs*;'!$> 1"3 +#5	%!K %= 14E3XZe%!K
 (5)4 +
 FHMQ],^iqrz"A_F{"C))''!//q--	C (SVc!f_c!fs1vo(MNK##K0 #  A% KKXKNM##A}5 ;;'7Q?II!QOZZ\16y1AB1AA'1AB%% Cs   E(	do_resizesizedo_center_crop	crop_size
do_rescalerescale_factordo_normalize
image_mean	image_stddisable_groupingreturn_tensorsrl   c           
      
   U(       a  US:  a  [        XS9u  nn0 n0 nUR                  5        H'  u  nnU R                  UUUUUUS9u  nnUUU'   UUU'   M)     [        UU5      nU VVs/ s H  nU  H  nUPM     M     nnn[        UU5      nO&[	        [        U5      5       Vs/ s H  nSS/PM	     nn[        XS9u  nn0 nUR                  5        H"  u  nnU(       a  U R                  UX4S9nUUU'   M$     [        UU5      n[        UUS9u  nn0 nUR                  5        H8  u  nnU(       a  U R                  UU5      nU R                  UXxXU5      nUUU'   M:     [        UU5      n[        UUS.US9$ s  snnf s  snf )Nr   )r   )r   ro   r   )imager   r   )pixel_valuesgrids)datatensor_type)
r   itemsr   r	   r%   r   r   center_croprescale_and_normalizer   ) r   r   r   r   r   r   r   r   r   r   r   r   r   r   rl   rm   rn   ro   r   grouped_imagesgrouped_images_indexprocessed_images_groupedr   r   stacked_imagesr<   images_listr   r   resized_images_groupedresized_imagesr   s                                    r   _preprocessOvis2ImageProcessor._preprocess  s   * {Q3H3s0N0')$E)7)=)=)?%~'+'A'A"#+A% (B ($ 3A(/#e *@ $$<>RSF/5Ov;%e;evFO"5*>?E%*3v;%78%7aV%7E8 0EV/o,,!#%3%9%9%;!E>!%>!`,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXY2BU!Saopp9 P 9s   *E:&F r   )T?NN)Fr   r~   T)(rp   rq   rr   rs   r   BICUBICr   r
   r   r   r   r   default_to_squarer   r   r   do_convert_rgbrl   rm   rn   ro   rj   valid_kwargsr   r   r   r   r   r   rw   ru   r1   r   r   liststrr   r   rx   __classcell__)r   s   @r   r{   r{      s   !))H!JIC(DIJLNOKK!,L#(A!B # 4 4v>W7X 4]i 4 4 (,$'&*NRS&S& S& 	S&
 !%S& "S& tOS& LS&H !&'+%Aq^$Aq Aq 	Aq
 LAq Aq Aq Aq Aq Aq DK'$.Aq 4;&-Aq +Aq j(4/Aq Aq  !Aq" #Aq$ !%%Aq( 
)Aq Aqr"   r{   )r   )&rt   	functoolsr   r   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_utilsr   image_transformsr   r	   image_utilsr
   r   r   r   r   processing_utilsr   r   utilsr   r   rw   r   tupler+   r?   r1   rK   rQ   rh   rj   r{   __all__r   r"   r   <module>r      s   '   7 ; 2 E  5 / 2<S <3 <SWX]^acf^fXgSh < < 3sCxCHo  	
 38_ 6c # c # UX ]b S S c3h DsTWY\^aOaIbDc  3
 !$	Mc3hMM M 	M
 38_M M4!E !. mq, mq mq` !
!r"   