Ë
    T^(h8  ã                   óö   — 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 ddl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mZ  e«       rddlZ ej@                  e!«      Z"d	„ Z#d
„ Z$ G d„ de	«      Z%dgZ&y)z#Image processor class for ImageGPT.é    )ÚDictÚListÚOptionalÚUnionNé   )ÚBaseImageProcessorÚBatchFeatureÚget_size_dict)ÚrescaleÚresizeÚto_channel_dimension_format)	ÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚ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Úis_vision_availableÚloggingc                 ó$  — |j                   }t        j                  t        j                  | «      d¬«      }t        j                  t        j                  |«      d¬«      }t        j                  | |«      }|d d …d f   d|z  z
  |d d d …f   z   }|S )Né   ©Úaxisr   é   )ÚTÚnpÚsumÚsquareÚmatmul)ÚaÚbÚa2Úb2ÚabÚds         út/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/imagegpt/image_processing_imagegpt.pyÚsquared_euclidean_distancer,   ,   sr   € Ø	�‰€AÜ	�‰”—	‘	˜!“ 1Ô	%€BÜ	�‰”—	‘	˜!“ 1Ô	%€BÜ	�‰�1�a‹€BØ
Š1ˆdˆ7‰�a˜"‘fÑ˜r $ª '™{Ñ*€AØ€Hó    c                 ól   — | j                  dd«      } t        | |«      }t        j                  |d¬«      S )Néÿÿÿÿr   r   r   )Úreshaper,   r!   Úargmin)ÚxÚclustersr*   s      r+   Úcolor_quantizer4   5   s/   € Ø	�	‰	�"�aÓ€AÜ" 1 hÓ/€AÜ�9‰9�Q˜QÔÐr-   c                   óª  ‡ — e Zd ZdZdgZdddej                  ddfdeee	e	e
      ej                  f      dedeee
f   ded	ed
eddfˆ 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	 	 ddej                  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ej*                  df	dedee   deee
f   ded	ee   d
ee   deee	e	e
      ej                  f      deeeef      deeeef      deeeef      dej2                  j2                  fd„«       Zˆ xZS )ÚImageGPTImageProcessora­  
    Constructs a ImageGPT image processor. This image processor can be used to resize images to a smaller resolution
    (such as 32x32 or 64x64), normalize them and finally color quantize them to obtain sequences of "pixel values"
    (color clusters).

    Args:
        clusters (`np.ndarray` or `List[List[int]]`, *optional*):
            The color clusters to use, of shape `(n_clusters, 3)` when color quantizing. Can be overriden by `clusters`
            in `preprocess`.
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image's dimensions to `(size["height"], size["width"])`. Can be overridden by
            `do_resize` in `preprocess`.
        size (`Dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`):
            Size of the image after resizing. Can be overridden by `size` in `preprocess`.
        resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`):
            Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`.
        do_normalize (`bool`, *optional*, defaults to `True`):
            Whether to normalize the image pixel value to between [-1, 1]. Can be overridden by `do_normalize` in
            `preprocess`.
        do_color_quantize (`bool`, *optional*, defaults to `True`):
            Whether to color quantize the image. Can be overridden by `do_color_quantize` in `preprocess`.
    Úpixel_valuesNTr3   Ú	do_resizeÚsizeÚresampleÚdo_normalizeÚdo_color_quantizeÚreturnc                 óÎ   •— t        ‰| �  di |¤Ž |�|ndddœ}t        |«      }|�t        j                  |«      nd | _        || _        || _        || _        || _	        || _
        y )Né   )ÚheightÚwidth© )ÚsuperÚ__init__r
   r!   Úarrayr3   r8   r9   r:   r;   r<   )	Úselfr3   r8   r9   r:   r;   r<   ÚkwargsÚ	__class__s	           €r+   rD   zImageGPTImageProcessor.__init__U   sl   ø€ ô 	‰ÑÑ"˜6Ò"ØÐ'‰t¸ÀcÑ-JˆÜ˜TÓ"ˆØ.6Ð.BœŸ™ Ô*ÈˆŒØ"ˆŒØˆŒ	Ø ˆŒØ(ˆÔØ!2ˆÕr-   ÚimageÚdata_formatÚinput_data_formatc                 ó–   — t        |«      }d|vsd|vrt        d|j                  «       › �«      ‚|d   |d   f}t        |f||||dœ|¤ŽS )a�  
        Resize an image to `(size["height"], size["width"])`.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`.
            data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the output image. If unset, the channel dimension format of the input
                image is used. 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.
            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.

        Returns:
            `np.ndarray`: The resized image.
        r@   rA   zFThe `size` dictionary must contain the keys `height` and `width`. Got )r9   r:   rJ   rK   )r
   Ú
ValueErrorÚkeysr   )rF   rI   r9   r:   rJ   rK   rG   Úoutput_sizes           r+   r   zImageGPTImageProcessor.resizek   sy   € ôF ˜TÓ"ˆØ˜4Ñ 7°$Ñ#6ÜÐeÐfj×foÑfoÓfqÐerÐsÓtÐtØ˜H‘~ t¨G¡}Ð5ˆÜØð
àØØ#Ø/ñ
ð ñ
ð 	
r-   c                 ó.   — t        |d||¬«      }|dz
  }|S )a  
        Normalizes an images' pixel values to between [-1, 1].

        Args:
            image (`np.ndarray`):
                Image to normalize.
            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.
        g€?)rI   ÚscalerJ   rK   r   )r   )rF   rI   rJ   rK   s       r+   Ú	normalizez ImageGPTImageProcessor.normalize›   s#   € ô" ˜e¨9À+ÐarÔsˆØ˜‘	ˆØˆr-   ÚimagesÚreturn_tensorsc           	      óV  — |�|n| j                   }|�|n| j                  }t        |«      }|�|n| j                  }|�|n| j                  }|�|n| j
                  }|�|n| j                  }t        j                  |«      }t        |«      }t        |«      st        d«      ‚t        |||¬«       |r|€t        d«      ‚|D �cg c]  }t        |«      ‘Œ }}|r#t        |d   «      rt        j!                  d«       |
€t#        |d   «      }
|r"|D �cg c]  }| j%                  ||||
¬«      ‘Œ }}|r |D �cg c]  }| j'                  ||
¬«      ‘Œ }}|r’|D �cg c]  }t)        |t*        j,                  |
«      ‘Œ }}t        j                  |«      }t/        ||«      j1                  |j2                  dd	 «      }|j2                  d   }|j1                  |d	«      }t5        |«      }n|D �cg c]  }t)        ||	|
¬
«      ‘Œ }}d|i}t7        ||¬«      S c c}w c c}w c c}w c c}w c c}w )aX  
        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_normalize=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.
            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_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image
            do_color_quantize (`bool`, *optional*, defaults to `self.do_color_quantize`):
                Whether to color quantize the image.
            clusters (`np.ndarray` or `List[List[int]]`, *optional*, defaults to `self.clusters`):
                Clusters used to quantize the image of shape `(n_clusters, 3)`. Only has an effect if
                `do_color_quantize` is set to `True`.
            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.
                Only has an effect if `do_color_quantize` is set to `False`.
            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.
        NzkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r8   r9   r:   z8Clusters must be specified if do_color_quantize is True.r   z®It looks like you are trying to rescale already rescaled images. If you wish to do this, make sure to set `do_normalize` to `False` and that pixel values are between [-1, 1].)rI   r9   r:   rK   )rI   rK   r/   )Úinput_channel_dimÚ	input_ids)ÚdataÚtensor_type)r8   r9   r
   r:   r;   r<   r3   r!   rE   r   r   rM   r   r   r   ÚloggerÚwarning_oncer   r   rR   r   r   ÚLASTr4   r0   ÚshapeÚlistr	   )rF   rS   r8   r9   r:   r;   r<   r3   rT   rJ   rK   rI   Ú
batch_sizerX   s                 r+   Ú
preprocessz!ImageGPTImageProcessor.preprocess°   s`  € ðl "+Ð!6‘I¸D¿N¹Nˆ	ØÐ'‰t¨T¯Y©YˆÜ˜TÓ"ˆØ'Ð3‘8¸¿¹ˆØ'3Ð'?‘|ÀT×EVÑEVˆØ1BÐ1NÑ-ÐTX×TjÑTjÐØ'Ð3‘8¸¿¹ˆÜ—8‘8˜HÓ%ˆä$ VÓ,ˆä˜FÔ#Üð:óð ô 	&ØØØõ	
ñ  Ð!1ÜÐWÓXÐXð 6<Ö<¨E”. Õ'Ð<ˆÐ<áœO¨F°1©IÔ6Ü×Ñðhôð
 Ð$ä >¸vÀa¹yÓ IÐáð $öàð —‘ %¨d¸XÐYj�ÕkðˆFð ñ
 ØdjÖkÐ[`�d—n‘n¨5ÐDU�nÕVÐkˆFÐkáØpvÖwÐglÔ1°%Ô9I×9NÑ9NÐPaÕbÐwˆFÐwä—X‘X˜fÓ%ˆFÜ# F¨HÓ5×=Ñ=¸f¿l¹lÈ3ÈBÐ>OÓPˆFð  Ÿ™ a™ˆJØ—^‘^ J°Ó3ˆFô ˜&“\‰Fð $öàô ,¨E°;ÐRcÖdðˆFð ð
 ˜VÐ$ˆÜ °>ÔBÐBùòQ =ùòùò lùò xùòs   ÃHÄHÄ6HÅ"H!Ç*H&)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBILINEARr   r   r   Úintr!   ÚndarrayÚboolr   ÚstrrD   r   r   rR   r   ÚFIRSTr   r   ÚPILÚImager`   Ú__classcell__)rH   s   @r+   r6   r6   ;   sŸ  ø„ ñð. (Ð(Ðð
 BFØØ#Ø'9×'BÑ'BØ!Ø"&ñ3ð ˜5  d¨3¡i¡°"·*±*Ð!<Ñ=Ñ>ð3ð ð	3ð
 �3˜�8‰nð3ð %ð3ð ð3ð  ð3ð 
õ3ð4 (:×'BÑ'BØ>BØDHñ.
à�z‰zð.
ð �3˜�8‰nð.
ð %ð	.
ð
 ˜e CÐ)9Ð$9Ñ:Ñ;ð.
ð $ E¨#Ð/?Ð*?Ñ$@ÑAð.
ð 
�‰ó.
ðf ?CØDHñ	à�z‰zðð ˜e CÐ)9Ð$9Ñ:Ñ;ðð $ E¨#Ð/?Ð*?Ñ$@ÑAð	ð
 
�‰óñ* %Ó&ð %)Ø#Ø'+Ø'+Ø,0ØAEØ;?Ø>N×>TÑ>TØDHñzCàðzCð ˜D‘>ðzCð �3˜�8‰nð	zCð
 %ðzCð ˜t‘nðzCð $ D™>ðzCð ˜5  d¨3¡i¡°"·*±*Ð!<Ñ=Ñ>ðzCð !  s¨J Ñ!7Ñ8ðzCð ˜e CÐ)9Ð$9Ñ:Ñ;ðzCð $ E¨#Ð/?Ð*?Ñ$@ÑAðzCð 
�‰�‰òzCó 'ôzCr-   r6   )'rd   Útypingr   r   r   r   Únumpyr!   Úimage_processing_utilsr   r	   r
   Úimage_transformsr   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   Úutilsr   r   r   r   rl   Ú
get_loggerra   rZ   r,   r4   r6   Ú__all__rB   r-   r+   ú<module>rw      s|   ðñ *ç .Ó .ã ç UÑ Uß LÑ L÷
÷ 
õ 
÷ _Ó ^ñ ÔÛð 
ˆ×	Ñ	˜HÓ	%€òò ôpCÐ/ô pCðf $Ð
$�r-   