
    ^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mZ ddlmZ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 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                   0    e Zd ZU dZeed<   eed<   eed<   y)NougatImageProcessorKwargsa  
    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_axisN)__name__
__module____qualname____doc__bool__annotations__     }/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/nougat/image_processing_nougat.pyr   r   )   s     r    r   F)totalc            !           e 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Zdee   f fdZededee   d	ef fd
       Z	 	 d'dZd Z	 d(ddded	dfdZddded	dfdZddded	dfdZ ddded	dfdZ!	 	 d)dddeddde"d	df
 fdZ#	 	 	 d*de$d   de"dedd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&e'z  dz  d#e"d$e"d%e"d	ef d&Z( xZ)S )+NougatImageProcessori  i  heightwidthTFkwargsc                 $    t        |   di | y )Nr   )super__init__)selfr(   	__class__s     r!   r+   zNougatImageProcessor.__init__G   s    "6"r    imagesreturnc                 $    t        |   |fi |S )N)r*   
preprocess)r,   r.   r(   r-   s      r!   r1   zNougatImageProcessor.preprocessJ   s    w!&3F33r    imagetorch.Tensorc                 p    t        j                  |d      }|ddddgf   }|j                  ddd      }|S )zGThis is a reimplementation of a findNonZero function equivalent to cv2.F)as_tupleN      )torchnonzeroreshape)r,   r2   non_zero_indicesidxvecs       r!   python_find_non_zeroz)NougatImageProcessor.python_find_non_zeroN   s>     !==?!!aV),Aq)r    c                    t        j                  |d      j                  t         j                        }t        j                  |d      j                  t         j                        }|d   |d   }}|d   |z
  dz   }|d   |z
  dz   }||||fS )zHThis is a reimplementation of a BoundingRect function equivalent to cv2.)r   r7   )axisr   r7   )r9   amintointamax)r,   coordinates
min_values
max_valuesx_miny_minr'   r&   s           r!   python_bounding_rectz)NougatImageProcessor.python_bounding_rectY   s     ZZ&9<<UYYG
ZZ&9<<UYYG
!!}jmu1%)A&*eUF**r    gray_thresholdc                 2   t        j                  |d      }t        j                  |      }t        j                  |      }||k(  r|S ||z
  ||z
  z  dz  }||k  }| j                  |      }| j                  |      \  }}	}
}|dd|	|	|z   |||
z   f   }|S )am  
        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.
        r7   )num_output_channels   N)tvFrgb_to_grayscaler9   maxminr>   rJ   )r,   r2   rK   datamax_valmin_valgraycoordsrH   rI   r'   r&   s               r!   crop_marginz NougatImageProcessor.crop_margind   s     ##EqA))D/))D/gLw7W#45;n$**40&*&?&?&G#ueVa/1FFGr    sizec                     |j                   dd \  }}|j                  |j                  }}||k  r||kD  s
||kD  r||k  rt        j                  |dddg      }|S )aL  
        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   r7   r6   )dims)shaper&   r'   r9   rot90)r,   r2   rY   input_heightinput_widthoutput_heightoutput_widths          r!   align_long_axisz$NougatImageProcessor.align_long_axis   sc      %*KK$4!k&*kk4::|=([<-G=([<-GKKq1v6Er    c                 `   |j                   dd \  }}|j                  |j                  }}t        ||      }t        ||      }||k(  r||k(  r|S ||kD  rt	        ||z  |z        }n||kD  rt	        ||z  |z        }||f}	t        j                  ||	t
        j                  j                        S )aW  
        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.
        r[   N)interpolation)	r]   r&   r'   rR   rC   rO   resizeInterpolationModeBICUBIC)
r,   r2   rY   r_   r`   ra   rb   r&   r'   new_sizes
             r!   	thumbnailzNougatImageProcessor.thumbnail   s      %*KK$4!k&*kk4::| \=1K.\!e{&:L+%f,|;<E<'-;<FE?zz%9N9N9V9VWWr    c                     |j                   dd \  }}|j                  |j                  }}||z
  }||z
  }|dz  }	|dz  }
||	z
  }||
z
  }|
|	||f}t        j                  ||      S )a
  
        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.
        r[   Nr6   )r]   r&   r'   rO   pad)r,   r2   rY   r_   r`   ra   rb   delta_widthdelta_heightpad_toppad_left
pad_bottom	pad_rightpaddings                 r!   
pad_imageszNougatImageProcessor.pad_images   s     %*KK$4!k&*kk4::|"[0$|3!#!#!G+
(*	Wi<wwug&&r    Nresamplez7PILImageResampling | tvF.InterpolationMode | int | None	antialiasc                     t        |j                  |j                        }t        ||dt        j
                        }t        |   |t        |d   |d         f||d|S )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)rY   default_to_squareinput_data_formatr   r7   r%   )ru   rv   )	rR   r&   r'   r   r   FIRSTr*   rf   r   )	r,   r2   rY   ru   rv   r(   shortest_edgeri   r-   s	           r!   rf   zNougatImageProcessor.resize   sm    * DKK4/RbRhRh
 w~88A;hqkB
MUaj
nt
 	
r    	do_resize
do_rescalerescale_factordo_normalize
image_mean	image_stddo_paddisable_groupingreturn_tensorsr   r   r   c           	      ,   |r|D cg c]  }| j                  |       }}t        ||      \  }}i }|j                         D ]_  \  }}|r| j                  ||      }|r| j	                  |||      }|r| j                  ||      }|
r| j                  ||      }|||<   a t        ||      }t        ||      \  }}i }|j                         D ]   \  }}| j                  ||||||	      }|||<   " t        ||      }t        d|i|      S c c}w )N)r   )r2   rY   )r2   rY   ru   pixel_values)rS   tensor_type)
rX   r   itemsrc   rf   rj   rt   r	   rescale_and_normalizer   )r,   r.   r|   rY   ru   r}   r~   r   r   r   r   r   r   r   r   r   r(   r2   grouped_imagesgrouped_images_indexresized_images_groupedr]   stacked_imagesresized_imagesprocessed_images_groupedprocessed_imagess                             r!   _preprocessz NougatImageProcessor._preprocess   sc   ( ;AB%d&&u-BFB 0EV^n/o,,!#%3%9%9%; 		;!E>!!%!5!5NQU!5!V!%>W_!`!%n4!P!%~D!Q,:"5)		; ((>@TU 0E^fv/w,,#% %3%9%9%; 	=!E>!77
NL*V_N /=$U+	= **BDXY.2B!CQ_``= Cs   D)r2   r3   )   )NT)FTT)*r   r   r   r   valid_kwargsr   BILINEARru   r
   r   r   r   rY   r|   r   r   r   r   r}   r   r   r+   r   r   r   r1   r>   rJ   rC   rX   r   rc   rj   rt   r   rf   listfloatstrr   r   __classcell__)r-   s   @r!   r$   r$   8   sb   -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r    r$   )r   r9   torchvision.transforms.v2r   rO   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    