
    qiV                     0   d Z ddlZddlmZmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZ dd	l m!Z!m"Z"  ejF                  e$      Z% e!       rddl&Z& G d
 ded      Z' e"d       G d de             Z(dgZ)y)z Image processor class for Donut.    N   )BaseImageProcessorBatchFeatureget_size_dict)convert_to_rgbget_resize_output_image_sizepadresizeto_channel_dimension_format)IMAGENET_STANDARD_MEANIMAGENET_STANDARD_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)ImagesKwargs)
TensorTypefilter_out_non_signature_kwargslogging)is_vision_availablerequiresc                   &    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__     b/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/donut/image_processing_donut.pyr   r   4   s     r)   r   F)total)vision)backendsc            $           e Zd ZdZdgZeZddej                  ddddddddfde	de
eef   dz  d	ed
e	de	de	de	deez  de	deee   z  dz  deee   z  dz  ddf fdZ	 	 ddej"                  de
eef   deez  dz  deez  dz  dej"                  f
dZ	 	 	 d dej"                  de
eef   de	deez  dz  deez  dz  dej"                  fdZej*                  ddfdej"                  de
eef   d	edeez  dz  deez  dz  dej"                  fdZej*                  ddfdej"                  de
eef   d	edeez  dz  deez  dz  dej"                  fdZ e       dddddddddddddej2                  dfdede	dz  de
eef   dz  d	edz  d
e	dz  de	dz  de	dz  de	de	dz  dedz  de	dz  deee   z  dz  deee   z  dz  deez  dz  dedz  deez  dz  dej:                  j:                  f"d       Z xZS )!DonutImageProcessora	  
    Constructs a Donut image processor.

    Args:
        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 `{"shortest_edge": 224}`):
            Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with
            the longest edge resized to keep the input aspect ratio. 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 image. If `random_padding` is set to `True` in `preprocess`, each image is padded with a
            random amount of padding on each size, up to the largest image size in the batch. Otherwise, all images are
            padded 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 `do_rescale` 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 `rescale_factor` 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_STANDARD_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_STANDARD_STD`):
            Image standard deviation.
    pixel_valuesTNFgp?	do_resizesizeresampler    r!   do_pad
do_rescalerescale_factordo_normalize
image_mean	image_stdreturnc                 N   t        |   di | ||nddd}t        |t        t        f      r|d d d   }t        |      }|| _        || _        || _        || _	        || _
        || _        || _        || _        |	| _        |
|
nt        | _        ||| _        y t"        | _        y )Ni 
  i  )heightwidthr(   )super__init__
isinstancetuplelistr   r1   r2   r3   r    r!   r4   r5   r6   r7   r   r8   r   r9   )selfr1   r2   r3   r    r!   r4   r5   r6   r7   r8   r9   kwargs	__class__s                r*   r@   zDonutImageProcessor.__init__i   s     	"6"'tt-LdUDM*":DT""	 ("4$,((2(>*DZ&/&;AVr)   imagedata_formatinput_data_formatc                 @   t        ||      \  }}|d   |d   }}|t        |      }|t        j                  k(  rd}	n$|t        j                  k(  rd}	nt        d|       ||k  r||kD  s
||kD  r||k  rt        j                  |d|	      }|t        |||	      }|S )
a  
        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_dimr<   r=   )r      )rM      zUnsupported data format: r   )axesinput_channel_dim)	r   r   r   LASTFIRST
ValueErrornprot90r   )
rD   rG   r2   rH   rI   input_heightinput_widthoutput_heightoutput_widthrot_axess
             r*   align_long_axisz#DonutImageProcessor.align_long_axis   s    . %35FW$X!k&*8nd7m|$ >u E 0 5 55H"2"8"88H89J8KLMM=([<-G=([<-GHHUAH5E"/{VghEr)   random_paddingc                 6   |d   |d   }}t        ||      \  }}	||	z
  }
||z
  }|rIt        j                  j                  d|dz         }t        j                  j                  d|
dz         }n
|dz  }|
dz  }||z
  }|
|z
  }||f||ff}t	        ||||      S )	a  
        Pad the image to the specified size.

        Args:
            image (`np.ndarray`):
                The image to be padded.
            size (`dict[str, int]`):
                The size `{"height": h, "width": w}` to pad the image to.
            random_padding (`bool`, *optional*, defaults to `False`):
                Whether to use random padding or not.
            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.
        r<   r=   rK   r   rM   )lowhighrN   )rH   rI   )r   rU   randomrandintr	   )rD   rG   r2   r]   rH   rI   rY   rZ   rW   rX   delta_widthdelta_heightpad_toppad_left
pad_bottom	pad_rightpaddings                    r*   	pad_imagezDonutImageProcessor.pad_image   s    . '+8nd7m|$25FW$X!k"[0$|3ii''AL14D'EGyy((Q[1_(EH"a'G"a'H!G+
(*	Z(8Y*?@5'{N_``r)   c           	          t        ||      \  }}|d   |d   }
}	t        ||	      }t        ||
      }||k(  r||k(  r|S ||kD  rt        ||z  |z        }n||kD  rt        ||z  |z        }t        |f||f|d||d|S )as  
        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.
        rK   r<   r=   g       @)r2   r3   reducing_gaprH   rI   )r   minintr
   )rD   rG   r2   r3   rH   rI   rE   rW   rX   rY   rZ   r<   r=   s                r*   	thumbnailzDonutImageProcessor.thumbnail   s    2 %35FW$X!k&*8nd7m| \=1K.\!e{&:L+%f,|;<E<'-;<F
%#/
 
 	
r)   c                     t        |      }t        |d   |d         }t        ||d|      }t        |f||||d|}	|	S )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.
        r<   r=   F)r2   default_to_squarerI   )r2   r3   rH   rI   )r   rm   r   r
   )
rD   rG   r2   r3   rH   rI   rE   shortest_edgeoutput_sizeresized_images
             r*   r
   zDonutImageProcessor.resize  sh    0 T"DNDM:2Rc
 
#/
 
 r)   imagesreturn_tensorsc           
      B   ||n| j                   }||n| j                  }t        |t        t        f      r|ddd   }t        |      }||n| j                  }||n| j                  }||n| j                  }||n| j                  }|	|	n| j                  }	|
|
n| j                  }
||n| j                  }||n| j                  }||n| j                  }t        |      }t!        |      st#        d      t%        |	|
||||||       |D cg c]  }t'        |       }}|D cg c]  }t)        |       }}|	r#t+        |d         rt,        j/                  d       |t1        |d         }|r!|D cg c]  }| j3                  |||       }}|r"|D cg c]  }| j5                  ||||       }}|r!|D cg c]  }| j7                  |||	       }}|r"|D cg c]  }| j9                  ||||
       }}|	r!|D cg c]  }| j;                  ||
|       }}|r"|D cg c]  }| j=                  ||||       }}|D cg c]  }t?        |||       }}d|i}tA        ||      S c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )a  
        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. If
                passing in images with pixel values between 0 and 1, set `do_rescale=False`.
            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 image. If `random_padding` is set to `True`, each image is padded with a random
                amount of padding on each size, up to the largest image size in the batch. Otherwise, all images are
                padded to the largest image size in the batch.
            random_padding (`bool`, *optional*, defaults to `self.random_padding`):
                Whether to use random padding when padding the image. If `True`, each image in the batch with be padded
                with a random amount of padding on each side up to the size of the largest image in the batch.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image pixel values.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            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.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.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.
        Nr>   zSInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, or torch.Tensor)r5   r6   r7   r8   r9   r1   r2   r3   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.)r2   rI   )rG   r2   r3   rI   )rG   r2   rI   )rG   r2   r]   rI   )rG   scalerI   )rG   meanstdrI   rP   r0   )datatensor_type)!r1   r2   rA   rB   rC   r   r3   r    r!   r4   r5   r6   r7   r8   r9   r   r   rT   r   r   r   r   loggerwarning_oncer   r\   r
   ro   rj   rescale	normalizer   r   )rD   ru   r1   r2   r3   r    r!   r4   r]   r5   r6   r7   r8   r9   rv   rH   rI   rG   r{   s                      r*   
preprocesszDonutImageProcessor.preprocess>  s/   R "+!6IDNN	'tTYYdUDM*":DT"'38'3'?|TEVEV3E3Q/W[WnWn!-4;;#-#9Zt
+9+E4K^K^'3'?|TEVEV#-#9Zt
!*!6IDNN	)&1F#rss%!)%!		
 6<<E.'<< 6<<E.'<</&)4s
 $ >vay Iouvfkd**5tO`*avFv $ %dXYjkF 
 ouvfkdnn5tO`navFv
 $	  d>]n  F   $ 5RcdF 
  $ U^opF  ou
ej'{N_`
 
 '>BBi = = w w

s6   I4*I99I>J J#JJ*JJ)NN)FNN) r"   r#   r$   r%   model_input_namesr   valid_kwargsr   BILINEARr&   dictstrrn   floatrC   r@   rU   ndarrayr   r\   rj   BICUBICro   r
   r   rS   r   r   PILImager   __classcell__)rF   s   @r*   r/   r/   @   sH   "H ((,L &*'9'B'B!#(&-!1504!W!W 38nt#!W %	!W
 !W !!W !W !W e!W !W DK'$.!W 4;&-!W 
!WN 6:;?-zz- 38n- ++d2	-
 !11D8- 
-f  %59;?(azz(a 38n(a 	(a
 ++d2(a !11D8(a 
(a\ (:'A'A59;?0
zz0
 38n0
 %	0

 ++d20
 !11D80
 
0
l (:'A'A59;?%zz% 38n% %	%
 ++d2% !11D8% 
%N %& "&&*.2$(*."$"&'+$(150426/?/E/E;?#[C[C $;[C 38nt#	[C
 %t+[C Tk[C !4K[C t[C [C 4K[C [C Tk[C DK'$.[C 4;&-[C j(4/[C  &,![C" !11D8#[C$ 
%[C '[Cr)   r/   )*r%   numpyrU   image_processing_utilsr   r   r   image_transformsr   r   r	   r
   r   image_utilsr   r   r   r   r   r   r   r   r   r   r   r   processing_utilsr   utilsr   r   r   utils.import_utilsr   r   
get_loggerr"   r}   r   r   r/   __all__r(   r)   r*   <module>r      s    '  U U     - I I ? 
		H	% 	E 	 
;YC, YC  YCx !
!r)   