
    cCi^                        S r SSKJrJr  SSKrSSKJrJrJ	r	  SSK
JrJrJrJrJr  SSKJrJrJrJrJrJrJrJrJrJrJrJr  SSKJrJrJ r   SS	K!J"r"  \ RF                  " \$5      r%\"" 5       (       a  SSK&r& " S
 S\5      r'S/r(g)z!Image processor class for Nougat.    )OptionalUnionN   )BaseImageProcessorBatchFeatureget_size_dict)get_resize_output_image_sizepadresizeto_channel_dimension_formatto_pil_image)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDChannelDimension
ImageInputPILImageResamplingget_image_sizeinfer_channel_dimension_formatis_scaled_imagemake_flat_list_of_imagesto_numpy_arrayvalid_imagesvalidate_preprocess_arguments)
TensorTypefilter_out_non_signature_kwargslogging)is_vision_availablec            %         ^  \ rS rSrSrS/rSSS\R                  SSSSSSSS4S\S	\S
\	\
\\4      S\S\S\S\S\S\\\4   S\S\	\\\\   4      S\	\\\\   4      SS4U 4S jjjrS\R$                  4S jrS r   S%S\R$                  S\S\	\   S\	\\\4      S\R$                  4
S jjr  S&S\R$                  S
\
\\4   S\	\\\4      S\	\\\4      S\R$                  4
S jjr  S&S\R$                  S
\
\\4   S\	\\\4      S\	\\\4      S\R$                  4
S jjr\R2                  SS4S\R$                  S
\
\\4   S\S\	\\\4      S\	\\\4      S\R$                  4S jjr\R2                  SS4S\R$                  S
\
\\4   S\S\	\\\4      S\	\\\4      S\R$                  4S  jjr\" 5       SSSSSSSSSSSSS\R:                  S4S!\S\	\   S	\	\   S
\	\
\\4      S\	\   S\	\   S\	\   S\	\   S\	\   S\	\\\4      S\	\   S\	\\\\   4      S\	\\\\   4      S"\	\\\4      S\	\   S\	\\\4      S\ RB                  RB                  4"S# jj5       r"S$r#U =r$$ )'NougatImageProcessor6   a  
Constructs a Nougat image processor.

Args:
    do_crop_margin (`bool`, *optional*, defaults to `True`):
        Whether to crop the image margins.
    do_resize (`bool`, *optional*, defaults to `True`):
        Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by
        `do_resize` in the `preprocess` method.
    size (`dict[str, int]` *optional*, defaults to `{"height": 896, "width": 672}`):
        Size of the image after resizing. Can be overridden by `size` in the `preprocess` method.
    resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`):
        Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method.
    do_thumbnail (`bool`, *optional*, defaults to `True`):
        Whether to resize the image using thumbnail method.
    do_align_long_axis (`bool`, *optional*, defaults to `False`):
        Whether to align the long axis of the image with the long axis of `size` by rotating by 90 degrees.
    do_pad (`bool`, *optional*, defaults to `True`):
        Whether to pad the images to the largest image size in the batch.
    do_rescale (`bool`, *optional*, defaults to `True`):
        Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by the `do_rescale`
        parameter in the `preprocess` method.
    rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
        Scale factor to use if rescaling the image. Can be overridden by the `rescale_factor` parameter in the
        `preprocess` method.
    do_normalize (`bool`, *optional*, defaults to `True`):
        Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method.
    image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`):
        Mean to use if normalizing the image. This is a float or list of floats the length of the number of
        channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method.
    image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`):
        Image standard deviation.
pixel_valuesTNFgp?do_crop_margin	do_resizesizeresampledo_thumbnaildo_align_long_axisdo_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturnc                   > [         TU ]  " S0 UD6  Ub  UOSSS.n[        U5      nXl        X l        X0l        X@l        XPl        X`l        Xpl	        Xl
        Xl        Xl        Ub  UO[        U l        Ub  Xl        g [        U l        g )Ni  i  )heightwidth )super__init__r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r   r,   r   r-   )selfr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   kwargs	__class__s                 l/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/nougat/image_processing_nougat.pyr4   NougatImageProcessor.__init__[   s      	"6"'tc-JT","	 ("4$,((2(>*DY&/&;AU    imagec                     [         R                  " [         R                  " U5      5      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.N   r      )npcolumn_stacknonzeroreshape)r5   r;   non_zero_indicesidxvecs       r8   python_find_non_zero)NougatImageProcessor.python_find_non_zero}   sC    ??2::e+<=!!aV),Aq)r:   c                     [         R                  " USS9R                  [        5      n[         R                  " USS9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@   minastypeintmax)r5   coordinates
min_values
max_valuesx_miny_minr1   r0   s           r8   python_bounding_rect)NougatImageProcessor.python_bounding_rect   sx    VVKf5<<SA
VVKf5<<SA
!!}jmu1%)A&*U**r:   gray_thresholddata_formatinput_data_formatc                    Uc  [        U5      n[        XS9n[        R                  " UR	                  S5      5      R                  [        R                  5      nUR                  5       nUR                  5       nXg:X  aE  [        R                  " U5      n[        X[        R                  5      nUb  [        XU5      nU$ UnU$ XW-
  Xg-
  -  S-  nXR:  nU R                  U5      n	U R                  U	5      u  ppUR                  XX-   X-   45      n[        R                  " U5      R                  [        R                  5      n[        X[        R                  5      nUb  [        XU5      nU$ U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 (`np.ndarray`):
        The image to be cropped.
    gray_threshold (`int`, *optional*, defaults to `200`)
        Value below which pixels are considered to be gray.
    data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the output image. If unset, will use the inferred format from the
        input.
    input_data_format (`ChannelDimension`, *optional*):
        The channel dimension format of the input image. If unset, will use the inferred format from the input.
rX   L   )r   r   r@   arrayconvertrL   uint8rN   rK   r   r   LASTrF   rT   crop)r5   r;   rV   rW   rX   datamax_valmin_valgraycoordsrR   rS   r1   r0   s                 r8   crop_margin NougatImageProcessor.crop_margin   sp   , $ >u EUHxxc*+22288<((*((*HHUOE/JZJ_J_`E * ,E@QR 
 L  
 L7#45;$**40&*&?&?&G#e

E%-HI&&rxx0+EFVF[F[\ S^Ri'<MN 	  pu 	 r:   c                 0   [        XS9u  pVUS   US   pUc  [        U5      nU[        R                  :X  a  Sn	O%U[        R                  :X  a  Sn	O[        SU 35      eX:  a  Xe:  d
  X:  a  Xe:  a  [        R                  " USU	S9nUb
  [        XUS	9nU$ )
aa  
Align the long axis of the image to the longest axis of the specified size.

Args:
    image (`np.ndarray`):
        The image to be aligned.
    size (`dict[str, int]`):
        The size `{"height": h, "width": w}` to align the long axis to.
    data_format (`str` or `ChannelDimension`, *optional*):
        The data format of the output image. If unset, the same format as the input image is used.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred.

Returns:
    `np.ndarray`: The aligned image.
channel_dimr0   r1   rI   )r=   r?   zUnsupported data format: r   )axesinput_channel_dim)	r   r   r   r`   FIRST
ValueErrorr@   rot90r   )
r5   r;   r$   rW   rX   input_heightinput_widthoutput_heightoutput_widthrot_axess
             r8   align_long_axis$NougatImageProcessor.align_long_axis   s    . %35$X!&*8nd7m|$ >u E 0 5 55H"2"8"88H89J8KLMM([-G([-GHHUAH5E"/VghEr:   c                 ~    US   US   pe[        XS9u  pxXh-
  n	XW-
  n
U
S-  nU	S-  nX-
  nX-
  nX4X44n[        XX4S9$ )a'  
Pad the image to the specified size at the top, bottom, left and right.

Args:
    image (`np.ndarray`):
        The image to be padded.
    size (`dict[str, int]`):
        The size `{"height": h, "width": w}` to pad the image to.
    data_format (`str` or `ChannelDimension`, *optional*):
        The data format of the output image. If unset, the same format as the input image is used.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred.
r0   r1   rj   r?   )rW   rX   )r   r
   )r5   r;   r$   rW   rX   rt   ru   rr   rs   delta_widthdelta_heightpad_toppad_left
pad_bottom	pad_rightpaddings                   r8   	pad_imageNougatImageProcessor.pad_image   sq    ( '+8nd7m|$25$X!"0$3!#!#!+
*	(8*?@5{``r:   c           	          [        XS9u  pxUS   US   p[        Xy5      n[        X5      nX:X  a  X:X  a  U$ Xx:  a  [        X-  U-  5      nOX:  a  [        X|-  U-  5      n[        U4X4USUUS.UD6$ )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 (`np.ndarray`):
        The image to be resized.
    size (`dict[str, int]`):
        The size `{"height": h, "width": w}` to resize the image to.
    resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`):
        The resampling filter to use.
    data_format (`Optional[Union[str, ChannelDimension]]`, *optional*):
        The data format of the output image. If unset, the same format as the input image is used.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred.
rj   r0   r1   g       @)r$   r%   reducing_gaprW   rX   )r   rK   rM   r   )r5   r;   r$   r%   rW   rX   r6   rr   rs   rt   ru   r0   r1   s                r8   	thumbnailNougatImageProcessor.thumbnail  s    2 %35$X!&*8nd7m| \1K.!e&:L%,|;<E'-;<F
#/
 
 	
r:   c                 x    [        U5      n[        US   US   5      n[        XSUS9n[        U4UUUUS.UD6n	U	$ )a  
Resizes `image` to `(height, width)` specified by `size` using the PIL library.

Args:
    image (`np.ndarray`):
        Image to resize.
    size (`dict[str, int]`):
        Size of the output image.
    resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`):
        Resampling filter to use when resiizing the image.
    data_format (`str` or `ChannelDimension`, *optional*):
        The channel dimension format of the image. If not provided, it will be the same as the input image.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred.
r0   r1   F)r$   default_to_squarerX   )r$   r%   rW   rX   )r   rK   r	   r   )
r5   r;   r$   r%   rW   rX   r6   shortest_edgeoutput_sizeresized_images
             r8   r   NougatImageProcessor.resizeH  sf    0 T"DNDM:2Rc
 
#/
 
 r:   imagesreturn_tensorsc                    Ub  UOU R                   nUb  UOU R                  nUb  UOU R                  nUb  UOU R                  nUb  UOU R                  nUb  UOU R
                  nUb  UOU R                  nU	b  U	OU R                  n	U
b  U
OU R                  n
Ub  UOU R                  nUb  UOU R                  nUb  UOU R                  n[        U5      n[        U5      (       d  [        S5      e[        U	U
UUUUUUS9  U Vs/ s H  n[!        U5      PM     nnU	(       a(  [#        US   5      (       a  [$        R'                  S5        Uc  [)        US   5      nU(       a   U Vs/ s H  nU R+                  UUS9PM     nnU(       a!  U Vs/ s H  nU R-                  UUUS9PM     nnU(       a!  U Vs/ s H  nU R/                  UXEUS9PM     nnU(       a!  U Vs/ s H  nU R1                  UUUS9PM     nnU(       a!  U Vs/ s H  nU R3                  UUUS9PM     nnU	(       a!  U Vs/ s H  nU R5                  UU
US	9PM     nnU(       a!  U Vs/ s H  nU R7                  UXUS
9PM     nnU Vs/ s H  n[9        UUUS9PM     nnSU0n[;        UUS9$ s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf )aW  
Preprocess an image or batch of images.

Args:
    images (`ImageInput`):
        Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255.
    do_crop_margin (`bool`, *optional*, defaults to `self.do_crop_margin`):
        Whether to crop the image margins.
    do_resize (`bool`, *optional*, defaults to `self.do_resize`):
        Whether to resize the image.
    size (`dict[str, int]`, *optional*, defaults to `self.size`):
        Size of the image after resizing. Shortest edge of the image is resized to min(size["height"],
        size["width"]) with the longest edge resized to keep the input aspect ratio.
    resample (`int`, *optional*, defaults to `self.resample`):
        Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
        has an effect if `do_resize` is set to `True`.
    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_pad (`bool`, *optional*, defaults to `self.do_pad`):
        Whether to pad the images to the largest image size in the batch.
    do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
        Whether to rescale the image by the specified scale `rescale_factor`.
    rescale_factor (`int` or `float`, *optional*, defaults to `self.rescale_factor`):
        Scale factor to use if rescaling the image.
    do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
        Whether to normalize the image.
    image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`):
        Image mean to use for normalization.
    image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`):
        Image standard deviation to use for normalization.
    return_tensors (`str` or `TensorType`, *optional*):
        The type of tensors to return. Can be one of:
        - Unset: Return a list of `np.ndarray`.
        - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
        - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
        - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
        - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
    data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
        The channel dimension format for the output image. Can be one of:
        - `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
        - `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        - Unset: defaults to the channel dimension format of the input image.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format for the input image. If unset, the channel dimension format is inferred
        from the input image. Can be one of:
        - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
        - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r)   r*   r+   r,   r-   r#   r$   r%   r   zIt looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.rZ   )r$   rX   )r;   r$   r%   rX   )r;   r$   rX   )r;   scalerX   )r;   meanstdrX   rm   r!   )rb   tensor_type)r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r   r   rp   r   r   r   loggerwarning_oncer   rg   rw   r   r   r   rescale	normalizer   r   )r5   r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r   rW   rX   r;   rb   s                      r8   
preprocessNougatImageProcessor.preprocesso  s9   N ,:+E4K^K^!*!6IDNN	'tTYY'38'3'?|TEVEV3E3Q/W[WnWn!-4;;#-#9Zt
+9+E4K^K^'3'?|TEVEV#-#9Zt
!*!6IDNN	)&1F##:  	&!)%!		
 6<<VE.'V</&)44s
 $ >vay I`fg`fW\d&&u@Q&R`fFgouvoufkd**5tO`*aouFv $#E %dYjk#  
 ouvoufkdnn5tO`naouFvouvoufkdnn5tO`naouFv $#E 5Rcd#  
  $#E U^op#   ou
ntej'{N_`nt 	 
 '>BB_ = h w w w

s6   J)J#J(8J- J2J70J<K9K)r'   r"   r+   r(   r)   r#   r&   r,   r-   r%   r*   r$   )   NN)NN)%__name__
__module____qualname____firstlineno____doc__model_input_namesr   BILINEARboolr   dictstrrM   r   floatlistr4   r@   ndarrayrF   rT   r   rg   rw   r   BICUBICr   r   r   ro   r   r   PILImager   __static_attributes____classcell__)r7   s   @r8   r   r   6   s    D ((  $)-'9'B'B!#(,3!:>9= V V  V tCH~&	 V
 % V  V ! V  V  V c5j) V  V U5$u+#567 V E%e"456 V 
 V  VD"** + "26DH2zz2 2 ./	2
 $E#/?*?$@A2 
2r ?CDH-zz- 38n- eC)9$9:;	-
 $E#/?*?$@A- 
-f ?CDH!azz!a 38n!a eC)9$9:;	!a
 $E#/?*?$@A!a 
!aP (:'A'A>BDH0
zz0
 38n0
 %	0

 eC)9$9:;0
 $E#/?*?$@A0
 
0
n (:'A'A>BDH%zz% 38n% %	%
 eC)9$9:;% $E#/?*?$@A% 
%N %& *.$()-15'+-1!%%)6:'+:>9=;?2B2H2HDH#UCUC !UC D>	UC
 tCH~&UC -.UC tnUC %TNUC UC TNUC !sEz!23UC tnUC U5$u+#567UC E%e"456UC !sJ!78UC  ./!UC" $E#/?*?$@A#UC$ 
%UC 'UCr:   r   ))r   typingr   r   numpyr@   image_processing_utilsr   r   r   image_transformsr	   r
   r   r   r   image_utilsr   r   r   r   r   r   r   r   r   r   r   r   utilsr   r   r   utils.import_utilsr   
get_loggerr   r   r   r   __all__r2   r:   r8   <module>r      s|    ( "  U U     J I 5 
		H	% OC- OCd "
"r:   