ó
    qyüi=*  ã                   óÂ   • S 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Jr  SSKJr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\SS9r\ " S S\5      5       rS/rg)z!Image processor class for Nougat.é    N)Ú
functionalé   )ÚTorchvisionBackend)ÚBatchFeature)Úget_resize_output_image_sizeÚgroup_images_by_shapeÚreorder_images)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDict)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringc                   ó8   • \ rS rSr% Sr\\S'   \\S'   \\S'   Srg)ÚNougatImageProcessorKwargsé)   a¤  
do_crop_margin (`bool`, *optional*, defaults to `self.do_crop_margin`):
    Whether to crop the image margins.
do_thumbnail (`bool`, *optional*, defaults to `self.do_thumbnail`):
    Whether to resize the image using thumbnail method.
do_align_long_axis (`bool`, *optional*, defaults to `self.do_align_long_axis`):
    Whether to align the long axis of the image with the long axis of `size` by rotating by 90 degrees.
Údo_crop_marginÚdo_thumbnailÚdo_align_long_axis© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚboolÚ__annotations__Ú__static_attributes__r   ó    Úo/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/nougat/image_processing_nougat.pyr   r   )   s   ‡ ñð ÓØÓØÖr#   r   F)Útotalc            !       óà  ^ • \ rS rSr\r\R                  r\	r
\rSSS.rSrSrSrSrSrSrSrS\\   4U 4S jjr\S	\S\\   S
\4U 4S jj5       r  S)S jrS r S*SSS\S
S4S jjrSSS\S
S4S jr SSS\S
S4S jr!SSS\S
S4S jr"  S+SSS\SSS\#S
S4
U 4S j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
\4 S' jjr)S(r*U =r+$ )-ÚNougatImageProcessoré8   i€  i   ©ÚheightÚwidthTFÚkwargsc                 ó&   >• [         TU ]  " S0 UD6  g )Nr   )ÚsuperÚ__init__)Úselfr,   Ú	__class__s     €r$   r/   ÚNougatImageProcessor.__init__G   s   ø€ Ü‰ÒÑ"˜6Ó"r#   ÚimagesÚreturnc                 ó&   >• [         TU ]  " U40 UD6$ )N)r.   Ú
preprocess)r0   r3   r,   r1   s      €r$   r6   ÚNougatImageProcessor.preprocessJ   s   ø€ ä‰wÒ! &Ñ3¨FÑ3Ð3r#   Úimageútorch.Tensorc                 ól   • [         R                  " USS9nUSS2SS/4   nUR                  SSS5      nU$ )zGThis is a reimplementation of a findNonZero function equivalent to cv2.F)Úas_tupleNé   é   éÿÿÿÿ)ÚtorchÚnonzeroÚreshape)r0   r8   Únon_zero_indicesÚidxvecs       r$   Úpython_find_non_zeroÚ)NougatImageProcessor.python_find_non_zeroN   s>   € ô !Ÿ=š=¨¸Ñ?ÐØ!¢! a¨ V )Ñ,ˆØ—‘  A qÓ)ˆØˆr#   c                 ó  • [         R                  " USS9R                  [         R                  5      n[         R                  " USS9R                  [         R                  5      nUS   US   pTUS   U-
  S-   nUS   U-
  S-   nXEXg4$ )zHThis is a reimplementation of a BoundingRect function equivalent to cv2.)r   r=   )Úaxisr   r=   )r?   ÚaminÚtoÚintÚamax)r0   ÚcoordinatesÚ
min_valuesÚ
max_valuesÚx_minÚy_minr+   r*   s           r$   Úpython_bounding_rectÚ)NougatImageProcessor.python_bounding_rectY   s‚   € ô —Z’Z °&Ñ9×<Ñ<¼U¿Y¹YÓGˆ
Ü—Z’Z °&Ñ9×<Ñ<¼U¿Y¹YÓGˆ
à! !‘} j°¡mˆuØ˜1‘ Ñ%¨Ñ)ˆØ˜A‘ Ñ&¨Ñ*ˆØ˜UÐ*Ð*r#   Úgray_thresholdc                 ó$  • [         R                  " USS9n[        R                  " U5      n[        R                  " U5      nXE:X  a  U$ X5-
  XE-
  -  S-  nX2:  nU R                  U5      nU R                  U5      u  p‰p«USS2X™U-   2XˆU
-   24   nU$ )a-  
Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the
threshold).

Args:
    image (`torch.Tensor`):
        The image to be cropped.
    gray_threshold (`int`, *optional*, defaults to `200`)
        Value below which pixels are considered to be gray.
r=   )Únum_output_channelséÿ   N)ÚtvFÚrgb_to_grayscaler?   ÚmaxÚminrD   rQ   )r0   r8   rS   ÚdataÚmax_valÚmin_valÚgrayÚcoordsrO   rP   r+   r*   s               r$   Úcrop_marginÚ NougatImageProcessor.crop_margind   s    € ô ×#Ò# E¸qÑAˆä—)’)˜D“/ˆÜ—)’)˜D“/ˆàÓØˆLØ‘ 7Ñ#4Ñ5¸Ñ;ˆØÑ$ˆØ×*Ñ*¨4Ó0ˆØ&*×&?Ñ&?ÀÓ&GÑ#ˆ�eØ’a˜¨¡Ð/°À¹Ð1FÐFÑGˆàˆr#   Úsizec                 ó®   • UR                   SS u  p4UR                  UR                  peXe:  a  XC:”  d
  Xe:”  a  XC:  a  [        R                  " USSS/S9nU$ )a  
Align the long axis of the image to the longest axis of the specified size.

Args:
    image (`torch.Tensor`):
        The image to be aligned.
    size (`SizeDict`):
        The size to align the long axis to.
Returns:
    `torch.Tensor`: The aligned image.
éþÿÿÿNr   r=   r<   )Údims)Úshaper*   r+   r?   Úrot90)r0   r8   rb   Úinput_heightÚinput_widthÚoutput_heightÚoutput_widths          r$   Úalign_long_axisÚ$NougatImageProcessor.align_long_axis‚   sW   € ð  %*§K¡K°°Ð$4Ñ!ˆØ&*§k¡k°4·:±:�|àÓ(¨[Ó-GØÓ(¨[Ó-Gä—K’K  q°°1¨vÑ6ˆEàˆr#   c                 óL  • UR                   SS u  p4UR                  UR                  pe[        X55      n[        XF5      nXs:X  a  X„:X  a  U$ X4:”  a  [	        XG-  U-  5      nOXC:”  a  [	        X8-  U-  5      nXx4n	[
        R                  " X[
        R                  R                  S9$ )a  
Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any
corresponding dimension of the specified size.

Args:
    image (`torch.tensor`):
        The image to be resized.
    size (`SizeDict`):
        The size to resize the image to.
rd   N)Úinterpolation)	rf   r*   r+   rZ   rJ   rW   ÚresizeÚInterpolationModeÚBICUBIC)
r0   r8   rb   rh   ri   rj   rk   r*   r+   Únew_sizes
             r$   Ú	thumbnailÚNougatImageProcessor.thumbnailœ   s    € ð  %*§K¡K°°Ð$4Ñ!ˆØ&*§k¡k°4·:±:�|ô �\Ó1ˆÜ�KÓ.ˆàÓ! eÓ&:ØˆLàÓ%Ü˜Ñ,¨|Ñ;Ó<‰EØÓ'Ü˜Ñ-°Ñ;Ó<ˆFà�?ˆä�zŠz˜%¼×9NÑ9N×9VÑ9VÑWÐWr#   c                 óº   • UR                   SS u  p4UR                  UR                  peXd-
  nXS-
  nUS-  n	US-  n
X‰-
  nXz-
  nX©XË4n[        R                  " X5      $ )zÒ
Pads a batch of images to the specified size at the top, bottom, left and right.

Args:
    image (`torch.tensor`):
        The image to be padded.
    size (`SizeDict`):
        The size to pad the image to.
rd   Nr<   )rf   r*   r+   rW   Úpad)r0   r8   rb   rh   ri   rj   rk   Údelta_widthÚdelta_heightÚpad_topÚpad_leftÚ
pad_bottomÚ	pad_rightÚpaddings                 r$   Ú
pad_imagesÚNougatImageProcessor.pad_images¿   ss   € ð %*§K¡K°°Ð$4Ñ!ˆØ&*§k¡k°4·:±:�|à"Ñ0ˆØ$Ñ3ˆà !Ñ#ˆØ !Ñ#ˆà!Ñ+ˆ
ØÑ*ˆ	à iÐ<ˆÜ�wŠw�uÓ&Ð&r#   NÚresamplez7PILImageResampling | tvF.InterpolationMode | int | NoneÚ	antialiasc                 óº   >• [        UR                  UR                  5      n[        XS[        R
                  S9n[        TU ]  " U[        US   US   S94X4S.UD6$ )a_  
Resize an image to `(size.height, size.width)`.

Args:
    image (`torch.Tensor`):
        Image to resize.
    size (`SizeDict`):
        Size of the output image.
    resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*):
        Resampling filter to use when resizing the image.
Returns:
    `torch.Tensor`: The resized image.
F)rb   Údefault_to_squareÚinput_data_formatr   r=   r)   )r�   r‚   )	rZ   r*   r+   r   r   ÚFIRSTr.   rp   r   )	r0   r8   rb   r�   r‚   r,   Úshortest_edgers   r1   s	           €r$   rp   ÚNougatImageProcessor.resizeÜ   sh   ø€ ô* ˜DŸK™K¨¯©Ó4ˆä/Ø¸ÔRb×RhÑRhñ
ˆô ‰wŠ~Ø”8 8¨A¡;°h¸q±kÑBð
ØMUñ
Øntñ
ð 	
r#   Ú	do_resizeÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdo_padÚdisable_groupingÚreturn_tensorsr   r   r   c           	      ó:  • U(       a!  U Vs/ s H  nU R                  U5      PM     nn[        XS9u  nn0 nUR                  5        Hg  u  nnU(       a  U R                  UUS9nU(       a  U R	                  UX4S9nU(       a  U R                  UUS9nU
(       a  U R                  UUS9nUUU'   Mi     [        UU5      n[        UUS9u  nn0 nUR                  5        H  u  nnU R                  UXVXxU	5      nUUU'   M!     [        UU5      n[        SU0US9$ s  snf )N)r�   )r8   rb   )r8   rb   r�   Úpixel_values)r[   Útensor_type)
r`   r   Úitemsrl   rp   rt   r   r	   Úrescale_and_normalizer   )r0   r3   r‰   rb   r�   rŠ   r‹   rŒ   r�   rŽ   r�   r�   r‘   r   r   r   r,   r8   Úgrouped_imagesÚgrouped_images_indexÚresized_images_groupedrf   Ústacked_imagesÚresized_imagesÚprocessed_images_groupedÚprocessed_imagess                             r$   Ú_preprocessÚ NougatImageProcessor._preprocessú   sP  € ö( Ù;AÓBº6°%�d×&Ñ& uÖ-¹6ˆFÐBô 0EÀVÑ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>Þ!Ø!%×!5Ñ!5¸NÐQUÐ!5Ð!V�ÞØ!%§¡°>È Ð!`�ÞØ!%§¡°nÈ4 Ð!P�ÞØ!%§¡°~ÈD Ð!Q�Ø,:Ð" 5Ó)ñ &<ô (Ð(>Ð@TÓUˆô 0EÀ^ÐfvÑ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>à!×7Ñ7Ø 
¸LÐV_óˆNð /=Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐä .Ð2BÐ!CÐQ_Ñ`Ð`ùò= Cs   ŒDr   )r8   r9   )éÈ   )NT)FTT),r   r   r   r   r   Úvalid_kwargsr   ÚBILINEARr�   r
   r�   r   rŽ   rb   r‰   rŒ   r   r   r�   rŠ   r   r   r/   r   r   r   r6   rD   rQ   rJ   r`   r   rl   rt   r   r    rp   ÚlistÚfloatÚstrr   rž   r"   Ú__classcell__)r1   s   @r$   r'   r'   8   sm  ø† à-€LØ!×*Ñ*€HØ&€JØ$€IØ CÑ(€DØ€IØ€LØ€LØÐØ€FØ€JØ€Nð# Ð(BÑ!C÷ #ð ð4 ð 4°vÐ>XÑ7Yð 4Ð^jö 4ó ð4ð	àô	ò	+ð "ñàðð ðð 
õ	ð<àðð ðð 
ô	ð4!Xàð!Xð ð!Xð 
ô	!XðF'àð'ð ð'ð 
ô	'ðB OSØñ
àð
ð ð
ð Lð	
ð
 ð
ð 
÷
ð 
ðX $)Ø!Ø#ñ!3aà�^Ñ$ð3að ð3að ð	3að
 Lð3að ð3að ð3að ð3að ˜D ™KÑ'¨$Ñ.ð3að ˜4 ™;Ñ&¨Ñ-ð3að �t‘ð3að  ™+ð3að ˜jÑ(¨4Ñ/ð3að !ð3að ð3að  ð!3að$ 
÷%3aó 3ar#   r'   )r   r?   Útorchvision.transforms.v2r   rW   Úimage_processing_backendsr   Úimage_processing_utilsr   Úimage_transformsr   r   r	   Úimage_utilsr
   r   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r'   Ú__all__r   r#   r$   Ú<module>r¯      so   ðñ (ã Ý 7å ;Ý 2÷ñ ÷
÷ ÷ 5÷ô °Uò ð ôtaÐ-ó taó ðtaðn "Ð
"�r#   