
    Z j                         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  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\SS9r\ " S S\5      5       rS/rg)z Image processor class for Donut.    N)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_STD
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringc                   .    \ rS rSr% Sr\\S'   \\S'   Srg)DonutImageProcessorKwargs!   a5  
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_thumbnaildo_align_long_axis N)__name__
__module____qualname____firstlineno____doc__bool__annotations____static_attributes__r       ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/donut/image_processing_donut.pyr   r   !   s     r    r   F)totalc            %         ^  \ rS 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\\   4U 4S	 jjr\S
\S\\   S\4U 4S jj5       rSSS\SS4S jr S'SSS\S\SS4S jjr 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-  S#\S$\S\4$S% jjr$S&r%U =r&$ )*DonutImageProcessor-   zMTorchvision backend for Donut with align_long_axis, thumbnail, and pad_image.i 
  i  )heightwidthTFkwargsc                    > UR                  SS 5      n[        U[        [        45      (       a  US S S2   nUb  X!S'   [        TU ]  " S0 UD6  g )Nsizer   )pop
isinstancetuplelistsuper__init__)selfr(   r*   	__class__s      r!   r1   DonutImageProcessor.__init__>   sN    zz&$'dUDM**":D!6N"6"r    imagesreturnc                    > [        U5      nSU;   a+  US   n[        U[        [        45      (       a  US S S2   US'   [        TU ]  " U40 UD6$ )Nr*   r+   )dictr-   r.   r/   r0   
preprocess)r2   r5   r(   r*   r3   s       r!   r9   DonutImageProcessor.preprocessF   sV     fV&>D$..!%ddvw!&3F33r    imageztorch.Tensorr*   c                     UR                   SS u  p4UR                  UR                  peXe:  a  XC:  d
  Xe:  aA  XC:  a<  UR                  5       S-
  UR                  5       S-
  p[        R
                  " USXx/S9nU$ )zKAlign the long axis of the image to the longest axis of the specified size.N      r   )dims)shaper&   r'   dimtorchrot90)	r2   r;   r*   input_heightinput_widthoutput_heightoutput_width
height_dim	width_dims	            r!   align_long_axis#DonutImageProcessor.align_long_axisS   sq     %*KK$4!&*kk4::|([-G([-G$)IIK!OUYY[1_	KKq
/FGEr    random_paddingc                 n   UR                   UR                  pTUR                  SS u  pgXW-
  nXF-
  n	U(       aS  [        R                  " SU	S-   S5      R                  5       n
[        R                  " SUS-   S5      R                  5       nO
U	S-  n
US-  nX-
  nX-
  nXX4n[        R                  " X5      $ )z$Pad the image to the specified size.r=   Nr   r?   r   r>   )r&   r'   rA   rC   randintitemtvFpad)r2   r;   r*   rM   rG   rH   rE   rF   delta_widthdelta_heightpad_toppad_left
pad_bottom	pad_rightpaddings                  r!   	pad_imageDonutImageProcessor.pad_imaged   s     '+kk4::|$)KK$4!"0$3mmA|a'7<AACG}}Qa<AACH"a'G"a'H!+
*	i<wwu&&r    Nresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                 $  > UR                   SS u  pVUR                  UR                  p[        XW5      n	[        Xh5      n
X:X  a  X:X  a  U$ XV:  a  [	        Xi-  U-  5      n
OXe:  a  [	        XZ-  U-  5      n	[
        TU ]  " U4[        XS9US.UD6$ )z%Resize the image to make a thumbnail.r=   N)r'   r&   )r*   r\   )rA   r&   r'   minintr0   resizer   )r2   r;   r*   r\   r(   rE   rF   rG   rH   r&   r'   r3   s              r!   	thumbnailDonutImageProcessor.thumbnail~   s     %*KK$4!&*kk4::|\1K.!e&:L%,|;<E'-;<Fw~
5
 	
 	
r    	do_resizedo_center_crop	crop_size
do_rescalerescale_factordo_normalize
image_mean	image_stddo_padpad_sizedisable_groupingreturn_tensorsr   r   c           	      p   [        XS9u  nn0 nUR                  5        H  u  nnU(       a  U R                  UU5      nU(       a:  [        UR                  UR
                  5      nU R                  U[        US9U5      nU(       a  U R                  UX45      nU(       a  U R                  UUSS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[        SU0US9$ )zCustom preprocessing for Donut.)rm   )shortest_edgeF)rM   pixel_values)datatensor_type)r   itemsrK   r^   r&   r'   r`   r   ra   rZ   r   center_croprescale_and_normalizer   )r2   r5   rc   r*   r\   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   r   r   r(   grouped_imagesgrouped_images_indexresized_images_groupedrA   stacked_imagesrp   resized_imagesprocessed_images_groupedprocessed_imagess                               r!   _preprocessDonutImageProcessor._preprocess   sP   , 0EV/o,,!#%3%9%9%;!E>!!%!5!5nd!K #DKK <!%^XTa=bdl!m!%!O!%UZ![,:"5) &< ((>@TU/D^fv/w,,#% %3%9%9%;!E>!%!1!1.)!L!77
LV_N /=$U+ &< **BDXY.2B!CQ_``r    r   )F)N)TF)'r   r   r   r   r   r   valid_kwargsr   BILINEARr\   r	   ri   r
   rj   r*   rc   rf   rh   r   r   rk   r   r1   r   r   r   r9   r   rK   r   rZ   ra   r/   floatstrr   r~   r   __classcell__)r3   s   @r!   r$   r$   -   s+   W,L!**H'J%IT*DIJLLF#(A!B # 
4
4 23
4 
	
4 
4  
	*  %	'' ' 	'
 
'< OS	

 
 L	
 

 
\ "#(%0a^$0a 0a 	0a
 L0a 0a 0a 0a 0a 0a DK'$.0a 4;&-0a t0a T/0a +0a  j(4/!0a" #0a$ !%0a( 
)0a 0ar    r$   )r   rC   torchvision.transforms.v2r   rQ   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr	   r
   r   r   r   processing_utilsr   r   utilsr   r   r   r$   __all__r   r    r!   <module>r      sf    '  7 ; 2 E  5 /	E 	 ]a, ]a ]a@ !
!r    