Ë
    S^(hiU  ã                   ó  — d Z ddlmZmZmZm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 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m Z m!Z! dd	l"m#Z#  e!jH                  e%«      Z& e#«       rddl'Z' G d
„ de	«      Z(dgZ)y)z Image processor class for Donut.é    )ÚDictÚListÚOptionalÚUnionNé   )ÚBaseImageProcessorÚBatchFeatureÚget_size_dict)Ú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_list_of_imagesÚto_numpy_arrayÚvalid_imagesÚvalidate_preprocess_arguments)Ú
TensorTypeÚfilter_out_non_signature_kwargsÚlogging)Úis_vision_availablec            %       ó  ‡ — e Zd ZdZdgZddej                  ddddddddfdedee	e
f   d	ed
ededededee
ef   dedeeeee   f      deeeee   f      ddfˆ fd„Z	 	 d dej"                  dee	e
f   deee	ef      deee	ef      dej"                  f
d„Z	 	 	 d!dej"                  dee	e
f   dedeee	ef      deee	ef      dej"                  fd„Zd„ Zej,                  ddfdej"                  dee	e
f   d	edeee	ef      deee	ef      dej"                  fd„Zej,                  ddfdej"                  dee	e
f   d	edeee	ef      deee	ef      dej"                  fd„Z e«       dddddddddddddej4                  dfdedee   dee	e
f   d	ed
ee   dee   dee   dedee   dee   dee   deeeee   f      deeeee   f      deee	ef      dee   deee	ef      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 amont 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ÚresampleÚdo_thumbnailÚdo_align_long_axisÚ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Úwidthéÿÿÿÿ© )ÚsuperÚ__init__Ú
isinstanceÚtupleÚlistr
   r"   r#   r$   r%   r&   r'   r(   r)   r*   r   r+   r   r,   )Úselfr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   ÚkwargsÚ	__class__s                €ún/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/donut/image_processing_donut.pyr4   zDonutImageProcessor.__init__\   s°   ø€ ô 	‰ÑÑ"˜6Ò"àÐ'‰t¸ÀtÑ-LˆÜ�dœU¤D˜MÔ*à™˜"˜‘:ˆDÜ˜TÓ"ˆà"ˆŒØˆŒ	Ø ˆŒØ(ˆÔØ"4ˆÔØˆŒØ$ˆŒØ,ˆÔØ(ˆÔØ(2Ð(>™*ÔDZˆŒØ&/Ð&;˜ˆ�ÔAVˆ�ó    ÚimageÚdata_formatÚinput_data_formatc                 ó®   — t        ||¬«      \  }}|d   |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/   r0   r   ©Úinput_channel_dim)r   ÚnpÚrot90r   )	r8   r=   r#   r>   r?   Úinput_heightÚinput_widthÚoutput_heightÚoutput_widths	            r;   Úalign_long_axisz#DonutImageProcessor.align_long_axis   so   € ô. %3°5ÐFWÔ$XÑ!ˆ�kØ&*¨8¡n°d¸7±m�|ˆà˜=Ò(¨[¸<Ò-GØ˜=Ò(¨[¸<Ò-Gä—H‘H˜U AÓ&ˆEàÐ"Ü/°°{ÐVgÔhˆEàˆ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/   r0   rA   r   é   )ÚlowÚhighé   )r>   r?   )r   rE   ÚrandomÚrandintr   )r8   r=   r#   rL   r>   r?   rI   rJ   rG   rH   Údelta_widthÚdelta_heightÚpad_topÚpad_leftÚ
pad_bottomÚ	pad_rightÚpaddings                    r;   Ú	pad_imagezDonutImageProcessor.pad_image£   sÇ   € ð. '+¨8¡n°d¸7±m�|ˆÜ$2°5ÐFWÔ$XÑ!ˆ�kà" [Ñ0ˆØ$ |Ñ3ˆáÜ—i‘i×'Ñ'¨A°LÀ1Ñ4DÐ'ÓEˆGÜ—y‘y×(Ñ(¨Q°[À1±_Ð(ÓE‰Hà" aÑ'ˆGØ" aÑ'ˆHà! GÑ+ˆ
Ø (Ñ*ˆ	à˜ZÐ(¨8°YÐ*?Ð@ˆÜ�5˜'¨{ÐN_Ô`Ð`r<   c                 óP   — t         j                  d«        | j                  |i |¤ŽS )NzTpad is deprecated and will be removed in version 4.27. Please use pad_image instead.)ÚloggerÚinfor[   )r8   Úargsr9   s      r;   r   zDonutImageProcessor.padÍ   s%   € Ü�‰ÐjÔkØˆt�~‰~˜tÐ. vÑ.Ð.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.
        rA   r/   r0   g       @)r#   r$   Úreducing_gapr>   r?   )r   ÚminÚintr   )r8   r=   r#   r$   r>   r?   r9   rG   rH   rI   rJ   r/   r0   s                r;   Ú	thumbnailzDonutImageProcessor.thumbnailÑ   s¿   € ô2 %3°5ÐFWÔ$XÑ!ˆ�kØ&*¨8¡n°d¸7±m�|ˆô �\ =Ó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/   r0   F)r#   Údefault_to_squarer?   )r#   r$   r>   r?   )r
   rb   r   r   )
r8   r=   r#   r$   r>   r?   r9   Úshortest_edgeÚoutput_sizeÚresized_images
             r;   r   zDonutImageProcessor.resize  sh   € ô0 ˜TÓ"ˆÜ˜D ™N¨D°©MÓ:ˆÜ2Ø˜¸ÐRcô
ˆô Øð
àØØ#Ø/ñ
ð ñ
ˆð Ðr<   ÚimagesÚreturn_tensorsc                 ó  — |�|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'        |«      ‘Œ }}|	r#t)        |d   «      rt*        j-                  d«       |€t/        |d   «      }|r!|D �cg c]  }| j1                  |||¬«      ‘Œ }}|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;                  ||||¬«      ‘Œ }}|D �cg c]  }t=        |||¬«      ‘Œ }}d|i}t?        ||¬«      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 )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
                amont 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.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.
        Nr1   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'   Úsize_divisibilityr"   r#   r$   r   z­It 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.)r#   r?   )r=   r#   r$   r?   )r=   r#   r?   )r=   r#   rL   r?   )r=   Úscaler?   )r=   ÚmeanÚstdr?   rC   r!   )ÚdataÚtensor_type) r"   r#   r5   r6   r7   r
   r$   r%   r&   r'   r(   r)   r*   r+   r,   r   r   Ú
ValueErrorr   r   r   r]   Úwarning_oncer   rK   r   rd   r[   ÚrescaleÚ	normalizer   r	   )r8   rj   r"   r#   r$   r%   r&   r'   rL   r(   r)   r*   r+   r,   rk   r>   r?   r=   rq   s                      r;   Ú
preprocesszDonutImageProcessor.preprocess*  s  € ðV "+Ð!6‘I¸D¿N¹Nˆ	ØÐ'‰t¨T¯Y©YˆÜ�dœU¤D˜MÔ*à™˜"˜‘:ˆDÜ˜TÓ"ˆØ'Ð3‘8¸¿¹ˆØ'3Ð'?‘|ÀT×EVÑEVˆØ3EÐ3QÑ/ÐW[×WnÑWnÐØ!Ð-‘°4·;±;ˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø+9Ð+E™È4×K^ÑK^ˆØ'3Ð'?‘|ÀT×EVÑEVˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø!*Ð!6‘I¸D¿N¹Nˆ	ä$ VÓ,ˆä˜FÔ#Üð:óð ô 	&Ø!Ø)Ø%Ø!ØØØ"ØØØõ	
ð 6<Ö<¨E”. Õ'Ð<ˆÐ<áœ/¨&°©)Ô4Ü×Ñðsôð
 Ð$ä >¸vÀa¹yÓ IÐáØouÖvÐfk�d×*Ñ*¨5°tÐO`Ð*ÕaÐvˆFÐváð $öàð —‘ %¨d¸XÐYj�ÕkðˆFð ñ
 ØouÖvÐfk�d—n‘n¨5°tÐO`�nÕaÐvˆFÐváð
 $ö	ð ð —‘Ø d¸>Ð]nð õ ðˆFð ñ ð $öàð —‘ 5°ÐRc�ÕdðˆFð ñ
 ð $öàð —‘ U°ÀÐ^o�ÕpðˆFð ð ouö
ØejÔ'¨¨{ÐN_Ö`ð
ˆð 
ð  Ð'ˆÜ °>ÔBÐBùòc =ùò wùòùò wùòùòùòùò

s0   ÄIÅ#I#ÆI(Æ*I-ÇI2Ç1I7ÈI<È6J)NN)FNN)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBILINEARÚboolr   Ústrrc   r   Úfloatr   r   r4   rE   Úndarrayr   rK   r[   r   ÚBICUBICrd   r   r   ÚFIRSTr   r   ÚPILÚImagerw   Ú__classcell__)r:   s   @r;   r    r    5   s…  ø„ ñ"ðH (Ð(Ðð Ø#Ø'9×'BÑ'BØ!Ø#(ØØØ,3Ø!Ø:>Ø9=ñ!Wàð!Wð �3˜�8‰nð!Wð %ð	!Wð
 ð!Wð !ð!Wð ð!Wð ð!Wð ˜c 5˜jÑ)ð!Wð ð!Wð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ð!Wð ˜E %¨¨e©Ð"4Ñ5Ñ6ð!Wð 
õ!WðN ?CØDHñ"à�z‰zð"ð �3˜�8‰nð"ð ˜e CÐ)9Ð$9Ñ:Ñ;ð	"ð
 $ E¨#Ð/?Ð*?Ñ$@ÑAð"ð 
�‰ó"ðP  %Ø>BØDHñ(aà�z‰zð(að �3˜�8‰nð(að ð	(að
 ˜e CÐ)9Ð$9Ñ:Ñ;ð(að $ E¨#Ð/?Ð*?Ñ$@ÑAð(að 
�‰ó(aòT/ð (:×'AÑ'AØ>BØDHñ0
à�z‰zð0
ð �3˜�8‰nð0
ð %ð	0
ð
 ˜e CÐ)9Ð$9Ñ:Ñ;ð0
ð $ E¨#Ð/?Ð*?Ñ$@ÑAð0
ð 
�‰ó0
ðl (:×'AÑ'AØ>BØDHñ%à�z‰zð%ð �3˜�8‰nð%ð %ð	%ð
 ˜e CÐ)9Ð$9Ñ:Ñ;ð%ð $ E¨#Ð/?Ð*?Ñ$@ÑAð%ð 
�‰ó%ñN %Ó&ð %)Ø#Ø'+Ø'+Ø-1Ø!%Ø$Ø%)Ø*.Ø'+Ø:>Ø9=Ø;?Ø2B×2HÑ2HØDHñ#`Càð`Cð ˜D‘>ð`Cð �3˜�8‰nð	`Cð
 %ð`Cð ˜t‘nð`Cð % T™Nð`Cð ˜‘ð`Cð ð`Cð ˜T‘Nð`Cð ! ™ð`Cð ˜t‘nð`Cð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ð`Cð ˜E %¨¨e©Ð"4Ñ5Ñ6ð`Cð !  s¨J Ñ!7Ñ8ð`Cð  Ð.Ñ/ð!`Cð" $ E¨#Ð/?Ð*?Ñ$@ÑAð#`Cð$ 
�‰�‰ò%`Có 'ô`Cr<   r    )*r{   Útypingr   r   r   r   ÚnumpyrE   Úimage_processing_utilsr   r	   r
   Úimage_transformsr   r   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   r   r   r   Úutilsr   r   r   Úutils.import_utilsr   Ú
get_loggerrx   r]   r„   r    Ú__all__r2   r<   r;   ú<module>r�      s~   ðñ 'ç .Ó .ã ç UÑ U÷ó ÷÷ ÷ ó ÷ JÑ IÝ 5ð 
ˆ×	Ñ	˜HÓ	%€ñ ÔÛôVCÐ,ô VCðr !Ð
!�r<   