
    ^j                         d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZ dd	lmZmZ dd
lmZmZ  G d ded      Ze G d de             ZdgZy)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                   &    e Zd ZU dZeed<   eed<   y)DonutImageProcessorKwargsaI  
    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_axisN)__name__
__module____qualname____doc__bool__annotations__     {/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/donut/image_processing_donut.pyr   r   !   s     r   r   F)totalc            %           e Zd ZdZeZej                  Ze	Z
eZdddZdZdZdZdZdZdZdee   f fdZed	edee   d
ef fd       Zddded
dfdZ	 d%dddeded
dfdZ	 d&dddeddd
df fdZ	 	 d'd	ed   dededddededede dede ee    z  dz  de ee    z  dz  dedz  dedz  d edz  d!e!e"z  dz  d"ed#ed
ef$d$Z# xZ$S )(DonutImageProcessorzMTorchvision backend for Donut with align_long_axis, thumbnail, and pad_image.i 
  i  )heightwidthTFkwargsc                     |j                  dd       }t        |t        t        f      r|d d d   }|||d<   t	        |   di | y )Nsizer   )pop
isinstancetuplelistsuper__init__)selfr$   r&   	__class__s      r   r-   zDonutImageProcessor.__init__>   sM    zz&$'dUDM*":D!F6N"6"r   imagesreturnc                     t        |      }d|v r&|d   }t        |t        t        f      r|d d d   |d<   t	        |   |fi |S )Nr&   r'   )dictr)   r*   r+   r,   
preprocess)r.   r0   r$   r&   r/   s       r   r4   zDonutImageProcessor.preprocessF   sS     fV&>D$.!%ddvw!&3F33r   imageztorch.Tensorr&   c                    |j                   dd \  }}|j                  |j                  }}||k  r||kD  s
||kD  rE||k  r@|j                         dz
  |j                         dz
  }}t	        j
                  |d||g      }|S )zKAlign the long axis of the image to the longest axis of the specified size.N      r   )dims)shaper"   r#   dimtorchrot90)	r.   r5   r&   input_heightinput_widthoutput_heightoutput_width
height_dim	width_dims	            r   align_long_axisz#DonutImageProcessor.align_long_axisS   s     %*KK$4!k&*kk4::|=([<-G=([<-G$)IIK!OUYY[1_	JKKq
I/FGEr   random_paddingc                 p   |j                   |j                  }}|j                  dd \  }}||z
  }||z
  }	|rQt        j                  d|	dz   d      j                         }
t        j                  d|dz   d      j                         }n
|	dz  }
|dz  }|	|
z
  }||z
  }||
||f}t        j                  ||      S )z$Pad the image to the specified size.r7   Nr   r9   r   r8   )r"   r#   r;   r=   randintitemtvFpad)r.   r5   r&   rF   rA   rB   r?   r@   delta_widthdelta_heightpad_toppad_left
pad_bottom	pad_rightpaddings                  r   	pad_imagezDonutImageProcessor.pad_imaged   s     '+kk4::|$)KK$4!k"[0$|3mmA|a'7<AACG}}Qa<AACH"a'G"a'H!G+
(*	Wi<wwug&&r   Nresamplez7PILImageResampling | tvF.InterpolationMode | int | Nonec                 6   |j                   dd \  }}|j                  |j                  }}t        ||      }	t        ||      }
|	|k(  r|
|k(  r|S ||kD  rt	        ||	z  |z        }
n||kD  rt	        ||
z  |z        }	t        |   |ft        |
|	      |d|S )z%Resize the image to make a thumbnail.r7   N)r#   r"   )r&   rT   )r;   r"   r#   minintr,   resizer   )r.   r5   r&   rT   r$   r?   r@   rA   rB   r"   r#   r/   s              r   	thumbnailzDonutImageProcessor.thumbnail~   s     %*KK$4!k&*kk4::|\=1K.\!e{&:L+%f,|;<E<'-;<Fw~
f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           	      \   t        ||      \  }}i }|j                         D ]  \  }}|r| j                  ||      }|r=t        |j                  |j
                        }| j                  |t        |      |      }|r| j                  |||      }|r| j                  ||d      }|||<    t        ||      }t        ||      \  }}i }|j                         D ]4  \  }}|r| j                  ||      }| j                  ||||	|
|      }|||<   6 t        ||      }t        d|i|      S )zCustom preprocessing for Donut.)rd   )shortest_edgeF)rF   pixel_values)datatensor_type)r   itemsrE   rV   r"   r#   rX   r   rY   rS   r   center_croprescale_and_normalizer   )r.   r0   rZ   r&   rT   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   r   r   r$   grouped_imagesgrouped_images_indexresized_images_groupedr;   stacked_imagesrg   resized_imagesprocessed_images_groupedprocessed_imagess                               r   _preprocesszDonutImageProcessor._preprocess   sc   , 0EV^n/o,,!#%3%9%9%; 
	;!E>!!%!5!5nd!K #DKK <!%^XTa=bdl!m!%h!O!%UZ![,:"5)
	; ((>@TU/D^fv/w,,#% %3%9%9%; 	=!E>!%!1!1.)!L!77
NL*V_N /=$U+	= **BDXY.2B!CQ_``r   )F)N)TF)%r   r   r   r   r   valid_kwargsr   BILINEARrT   r	   r`   r
   ra   r&   rZ   r]   r_   r   r   rb   r   r-   r   r   r   r4   r   rE   r   rS   rY   r+   floatstrr   ru   __classcell__)r/   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r   r!   )r   r=   torchvision.transforms.v2r   rJ   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   