ó
    qyüi 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    Úm/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/ovis2/image_processing_ovis2.pyÚ<lambda>Ú1get_all_supported_aspect_ratios.<locals>.<lambda>,   s   € ¨q°©t°a¸±dª{ó    ©Úkey)ÚrangeÚappendÚsorted)r   r   Úaspect_ratiosÚwidthÚheights        r   Úget_all_supported_aspect_ratiosr+   $   sh   € ð €MÜ�q˜/¨AÑ-Ö.ˆÜ˜A˜°Ñ2Ö3ˆFØ‰~ Õ0°U±^ÀÕ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Ø,<Ñ)ÐØ!Ñ3€LØÑ+€Dä˜E“l€OØ€IÛ*ˆØ  ™G d¨1¡gÑ-ÐÜ˜Ñ9Ó:ˆ
ØÓ'Ø(ˆOØŠIØÕ*Ø�cÐ.Ñ.Ð1BÑBÀTÈ!ÁWÑLÈtÐTUÉwÑ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Üˆq‹9”c˜!“i€qØƒxØ‰E�D‰LˆØˆØ‰5€Lr"   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¥   € Ø˜1‘g‘€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                  X¬45        XÃ:”  d  M@  U	R                  X¬4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"   øé € ÐcÒVbÈFÔ+ÐG¨VÐGÐ5F×GÒVbùs   ƒ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(   € ¸Q¸q¹TÀ!¹WÀqÈÁtÈAÁwÑ=NÐQRÐSTÑQUÐPUÑ<Vr"   r#   r   c                 ó0   • U S   * U S   S   U S   S   -  4$ )Nr   r   r   r   s    r   r    r\   |   s$   € °°!±¨u°a¸±d¸1±gÀÀ!ÁÀ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Ü:¸1¸oÓNÐØ€OØ "Ðã)ˆ	Ü,¨\È	ÓRˆäÔcÑVbÓcÓcÐfpÑpð 	ð 	×Ñ 	Ð?Ô@ØÕ3Ø%×,Ñ,¨iÐ-MÖNñ *ö !ÜÐ,Ñ2VÑWÐXYÑ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Ò! &Ñ3¨FÑ3Ð3r"   Nztorch.Tensorrm   rn   ro   rT   Ú
patch_sizeÚresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                 óÚ  • UR                   S   nUR                  UR                  p©UR                   SS u  p¼U(       a  [        X¼4U	UUS9u  pÞO[	        X¼4Xš4X#5      u  pÞX­-  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 —L‘L ‘Oˆ	Ø.8×.?Ñ.?À×AQÑAQÐ+Ø*0¯,©,°r°sÐ*;Ñ'ˆæ!ä$>Ø Ð1Ø"3Ø +Ø#5ñ	%Ñ!ˆK˜ô %=Ø Ð1Ð4EÐ3XÐZeó%Ñ!ˆKð
 (Ñ5ˆØ)Ñ4ˆØ Ñ+ˆ
ð Ÿ™ F¬H¸MÐQ]Ñ,^Ðiq˜ÐrˆàÐÜ�zÖ"ˆAØ˜‘_ˆFØ�{Ñ"ˆCàÐ)Ñ)ØÐ'Ñ'Ø˜!‘Ð/Ñ/Ø�q‘Ð-Ñ-ð	ˆCð (¨¨S°©V°c¸!±f¨_¸cÀ!¹fÀsÈ1Áv¸oÐ(MÑNˆKØ×#Ñ# KÖ0ñ #ô ÐÓ  AÓ%Ø ŸK™K¨ÀX˜KÐNˆMØ×#Ñ# A }Ô5ä Ÿ;š;Ð'7¸QÑ?×IÑIÈ!ÈQÓO×ZÑZÓ\ÐÜ16°yÔ1AÓBÒ1A¨A�Ó'Ñ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XxXšU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Ñ0ˆNÐ0Ø')Ð$ØˆEØ)7×)=Ñ)=Ö)?Ñ%��~Ø'+×'AÑ'AØ"ØØØ#Ø+AØ%ð (Bð (Ñ$� ð 3AÐ(¨Ñ/Ø#��e“ñ *@ô $Ð$<Ð>RÓSˆFÙ/5ÔOªv Ä;¸%“eÁ;‘e©vˆFÑOÜ" 5Ð*>Ó?‰Eä%*¬3¨v«;Ô%7Ó8Ò%7 �a˜“VÑ%7ˆEÐ8ô 0EÀVÑ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡°>È Ð!`�Ø,:Ð" 5Ó)ñ &<ô (Ð(>Ð@TÓUˆô 0EÀ^ÐfvÑ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%×!1Ñ!1°.À)Ó!L�à!×7Ñ7Ø 
¸LÐV_óˆNð /=Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐÜÐ2BÈUÑ!SÐaoÑpÐpùó9 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 ð 4°vÐ>WÑ7Xð 4Ð]iö 4ó ð4ð (,Ø$'Ø&*ØNRñS&àðS&ð ðS&ð ð	S&ð
 !%ðS&ð "ðS&ð ˜t‘Oð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ð <ÐSWÐX]Ð^aÐcfÐ^fÑXgÑShó <ó ð<ñ �3ÑðØ˜s C˜x™ðà˜C ˜H‘oðð ðð ð	ð
 ˆ3�ˆ8�_óó ðð6 cð °#ð ¸cð È#ð ÐUXð Ð]bô ð˜Sð  Sð °°c¸3°h±ð ÀDÈÈsÐTWÐY\Ð^aÐOaÑIbÑDcô ñ �3Ñð
 !$ñ	MØ�c˜3�h‘ðMàðMð ðMð ð	Mð
 ˆ3�ˆ8�_ôMó ðMô4! °Eò !ð. ômqÐ,ó mqó ðmqð` !Ð
!�r"   