
    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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       ڃ/root/GenerationalWealth/GenerationalWealth/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+$ )-NougatImageProcessor8   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!&3F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),Aq)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     ZZ&9<<UYYG
ZZ&9<<UYYG
!!}jmu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  ppUSS2XU-   2XU
-   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     ##Eq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      %*KK$4!&*kk4::|([-G([-GKKq1v6E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      %*KK$4!&*kk4::| \1K.!e&:L%,|;<E'-;<F?zz%9N9N9V9V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XX4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     %*KK$4!&*kk4::|"0$3!#!#!+
*	i<wwu&&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    * DKK4/RbRhRh
 w~88A;hqk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B6%d&&u-6FB 0EV/o,,!#%3%9%9%;!E>!!%!5!5NQU!5!V!%>!`!%n4!P!%~D!Q,:"5) &< ((>@TU 0E^fv/w,,#% %3%9%9%;!E>!77
LV_N /=$U+ &< **BDXY.2B!CQ_``= 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 4v>X7Y 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#   