Ë
    S^(hnN  ã                   ó  — d Z ddlZddl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mZmZmZ ddlm Z m!Z!m"Z"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 Gemma3.é    N)ÚDictÚListÚOptionalÚUnioné   )ÚBaseImageProcessorÚBatchFeatureÚget_size_dict)Úconvert_to_rgbÚ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)Ú
TensorTypeÚfilter_out_non_signature_kwargsÚis_vision_availableÚloggingc            &       ó  ‡ — e Zd ZdZddgZddej                  ddddddddddfdedee	e
f   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ee   dee   dee
   dee
   dee   ddfˆ fd„Z	 	 ddej"                  de
de
dedeee	ef      deee	ef      fd„Z	 	 ddeej"                     dede
de
dedeee	ef      deee	ef      fd„Z e«       dddddddddej,                  ddddd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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e   dee   dee
   dee
   dee   dej4                  j4                  f$d„«       Zˆ xZS )ÚGemma3ImageProcessoraI
  
    Constructs a SigLIP 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 `{"height": 224, "width": 224}`):
            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_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 by the specified mean and standard deviation. Can be overridden by
            `do_normalize` in the `preprocess` method.
        image_mean (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`):
            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 `[0.5, 0.5, 0.5]`):
            Standard deviation 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_std` parameter in the `preprocess` method.
            Can be overridden by the `image_std` parameter in the `preprocess` method.
        do_convert_rgb (`bool`, *optional*, defaults to `True`):
            Whether to convert the image to RGB.
        do_pan_and_scan (`bool`, *optional*):
            Whether to apply `pan_and_scan` to images.
        pan_and_scan_min_crop_size (`int`, *optional*):
            Minimum size of each crop in pan and scan.
        pan_and_scan_max_num_crops (`int`, *optional*):
            Maximum number of crops per image in pan and scan.
        pan_and_scan_min_ratio_to_activate (`float`, *optional*):
            Minimum aspect ratio to activate pan and scan.
    Úpixel_valuesÚ	num_cropsTNgp?Ú	do_resizeÚsizeÚresampleÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdo_convert_rgbÚdo_pan_and_scanÚpan_and_scan_min_crop_sizeÚpan_and_scan_max_num_cropsÚ"pan_and_scan_min_ratio_to_activateÚreturnc                 ó.  •— t        ‰| �  di |¤Ž |�|ndddœ}t        |d¬«      }|�|nt        }|�|nt        }|| _        || _        || _        || _        || _	        || _
        || _        || _        |	| _        |
| _        || _        || _        || _        y )Néà   )ÚheightÚwidthT)Údefault_to_square© )ÚsuperÚ__init__r
   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-   r.   ÚkwargsÚ	__class__s                  €úp/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/gemma3/image_processing_gemma3.pyr7   zGemma3ImageProcessor.__init___   s®   ø€ ô" 	‰ÑÑ"˜6Ò"ØÐ'‰t¸ÀcÑ-JˆÜ˜T°TÔ:ˆØ#-Ð#9‘ZÔ?Uˆ
Ø!*Ð!6‘IÔ<Qˆ	à"ˆŒØˆŒ	Ø ˆŒØ$ˆŒØ,ˆÔØ(ˆÔØ$ˆŒØ"ˆŒØ,ˆÔØ.ˆÔØ*DˆÔ'Ø*DˆÔ'Ø2TˆÕ/ó    ÚimageÚdata_formatÚinput_data_formatc           
      óD  — t        |«      \  }}||k\  rt||z  |k  rg S t        t        j                  ||z  dz   «      «      }	t	        t        t        j                  ||z  «      «      |	«      }	t        d|	«      }	t	        ||	«      }	d}
ns||z  |k  rg S t        t        j                  ||z  dz   «      «      }
t	        t        t        j                  ||z  «      «      |
«      }
t        d|
«      }
t	        ||
«      }
d}	t        t        j                  ||	z  «      «      }t        t        j                  ||
z  «      «      }t	        ||«      |k  rg S t        |	«      D �cg c]  }||z  ‘Œ	 }}t        |
«      D �cg c]  }||z  ‘Œ	 }}|t        j                  k(  r9t        j                  ||«      D ��cg c]  \  }}||||z   …|||z   …f   ‘Œ }}}|S t        j                  ||«      D ��cg c]  \  }}|dd…|||z   …|||z   …f   ‘Œ }}}|S c c}w c c}w c c}}w c c}}w )aº  
        Pan and Scan and image, by cropping into smaller images when the aspect ratio exceeds
        minumum allowed ratio.

        Args:
            image (`np.ndarray`):
                Image to resize.
            pan_and_scan_min_crop_size (`int`, *optional*):
                Minimum size of each crop in pan and scan.
            pan_and_scan_max_num_crops (`int`, *optional*):
                Maximum number of crops per image in pan and scan.
            pan_and_scan_min_ratio_to_activate (`float`, *optional*):
                Minimum aspect ratio to activate pan and scan.
            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      à?é   é   N)r   ÚintÚmathÚfloorÚminÚmaxÚceilÚranger   ÚLASTÚ	itertoolsÚproduct)r8   r=   r,   r-   r.   r>   r?   r2   r3   Únum_crops_wÚnum_crops_hÚcrop_size_wÚcrop_size_hÚiÚcrop_positions_wÚcrop_positions_hÚpos_hÚpos_wÚimage_cropss                      r;   Úpan_and_scanz!Gemma3ImageProcessor.pan_and_scan„   sT  € ô6 ' uÓ-‰ˆ�ð �FŠ?à�v‰~Ð BÒBØ�	ô œdŸj™j¨°©¸#Ñ)=Ó>Ó?ˆKÜœc¤$§*¡*¨UÐ5OÑ-OÓ"PÓQÐS^Ó_ˆKô ˜a Ó-ˆKÜÐ8¸+ÓFˆKØ‰Kð
 ˜‰~Ð BÒBØ�	ô œdŸj™j¨°%©¸#Ñ)=Ó>Ó?ˆKÜœc¤$§*¡*¨VÐ6PÑ-PÓ"QÓRÐT_Ó`ˆKô ˜a Ó-ˆKÜÐ8¸+ÓFˆKØˆKäœ$Ÿ)™) E¨KÑ$7Ó8Ó9ˆÜœ$Ÿ)™) F¨[Ñ$8Ó9Ó:ˆô ˆ{˜KÓ(Ð+EÒEØˆIä5:¸;Ó5GÖH°˜K¨!›OÐHÐÐHÜ5:¸;Ó5GÖH°˜K¨!›OÐHÐÐHàÔ 0× 5Ñ 5Ò5ô %.×$5Ñ$5Ð6FÐHXÓ$Y÷á �E˜5ð �e˜e kÑ1Ð1°5¸5À;Ñ;NÐ3NÐNÓOðˆKñ ð Ðô %.×$5Ñ$5Ð6FÐHXÓ$Y÷á �E˜5ð ’a˜ ¨Ñ!4Ð4°e¸eÀkÑ>QÐ6QÐQÓRðˆKñ ð
 Ðùò IùÚHùóùó
s   ÅHÅ6HÆ0HÇ)HÚimagesc           	      ó¬   — g }g }	|D ]H  }
| j                  |
|||||¬«      }|j                  |
g|z   «       |	j                  t        |«      «       ŒJ ||	fS )N)r=   r,   r-   r.   r>   r?   )rW   ÚextendÚappendÚlen)r8   rX   r+   r,   r-   r.   r>   r?   Úpas_images_listr!   r=   Ú
pas_imagess               r;   Ú _process_images_for_pan_and_scanz5Gemma3ImageProcessor._process_images_for_pan_and_scanÖ   sz   € ð ˆØˆ	Øò 
	.ˆEØ×*Ñ*ØØ+EØ+EØ3UØ'Ø"3ð +ó ˆJð ×"Ñ" E 7¨ZÑ#7Ô8Ø×ÑœS ›_Õ-ð
	.ð  	Ð)Ð)r<   Úreturn_tensorsc           
      ót  — |�|n| j                   }|�|n| j                  }t        |dd¬«      }|�|n| j                  }|�|n| j                  }|�|n| j
                  }|�|n| j                  }|�|n| j                  }|	�|	n| j                  }	|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }t        |«      }t        |«      st!        d«      ‚t#        |||||	|||¬«       |r|D �cg c]  }t%        |«      ‘Œ }}|D �cg c]  }t'        |«      ‘Œ }}|r#t)        |d   «      rt*        j-                  d«       |€t/        |d   «      }|r| j1                  |||||||¬«      \  }}n|D �cg c]  }d‘Œ }}g }|D ]k  }|r|d	   |d
   }}t3        |||f||¬«      }|r| j5                  |||¬«      }|r| j7                  |||	|¬«      }t9        |||¬«      }|j;                  |«       Œm ||dœ}t=        ||
¬«      S 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.
            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_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            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. Only has an effect if `do_normalize` is set to `True`.
            image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`):
                Image standard deviation to use for normalization. Only has an effect if `do_normalize` 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:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use 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.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            do_pan_and_scan (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to apply `pan_and_scan` to images.
            pan_and_scan_min_crop_size (`int`, *optional*, defaults to `self.pan_and_scan_min_crop_size`):
                Minimum size of each crop in pan and scan.
            pan_and_scan_max_num_crops (`int`, *optional*, defaults to `self.pan_and_scan_max_num_crops`):
                Maximum number of crops per image in pan and scan.
            pan_and_scan_min_ratio_to_activate (`float`, *optional*, defaults to `self.pan_and_scan_min_ratio_to_activate`):
                Minimum aspect ratio to activate pan and scan.
        r#   F)Ú
param_namer4   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   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.)rX   r+   r,   r-   r.   r>   r?   r2   r3   )r=   r#   r$   r?   )r=   Úscaler?   )r=   ÚmeanÚstdr?   )Úinput_channel_dim)r    r!   )ÚdataÚtensor_type)r"   r#   r
   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r   r   Ú
ValueErrorr   r   r   r   ÚloggerÚwarning_oncer   r_   r   ÚrescaleÚ	normalizer   r[   r	   )r8   rX   r"   r#   r$   r%   r&   r'   r(   r)   r`   r>   r?   r*   r+   r,   r-   r.   r=   r!   Ú_Úprocessed_imagesr2   r3   rg   s                            r;   Ú
preprocesszGemma3ImageProcessor.preprocessï   sË  € ðV "+Ð!6‘I¸D¿N¹Nˆ	ØÐ'‰t¨T¯Y©YˆÜ˜T¨fÈÔNˆØ'Ð3‘8¸¿¹ˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø+9Ð+E™È4×K^ÑK^ˆØ'3Ð'?‘|ÀT×EVÑEVˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø!*Ð!6‘I¸D¿N¹Nˆ	Ø+9Ð+E™È4×K^ÑK^ˆØ-<Ð-H™/Èd×NbÑNbˆà*DÐ*PÑ&ÐVZ×VuÑVuð 	#ð +EÐ*PÑ&ÐVZ×VuÑVuð 	#ð
 2Ð=ñ /à×8Ñ8ð 	+ô *¨&Ó1ˆä˜FÔ#Üð:óð ô
 	&Ø!Ø)Ø%Ø!ØØØØõ		
ñ Ø9?Ö@°”n UÕ+Ð@ˆFÐ@ð 6<Ö<¨E”. Õ'Ð<ˆÐ<áœ/¨&°©)Ô4Ü×Ñðsôð
 Ð$ä >¸vÀa¹yÓ IÐáØ $× EÑ EØØ /Ø+EØ+EØ3UØ'Ø"3ð !Fó !ÑˆF‘Ið %+Ö+˜qšÐ+ˆIÐ+àÐØò 	+ˆEÙØ $ X¡°°W±˜�ÜØ v¨u oÀÐ\mô�ñ ØŸ™¨5¸ÐZk˜Ól�áØŸ™Ø j°iÐSdð 'ó �ô 0°°{ÐVgÔhˆEØ×#Ñ# EÕ*ð!	+ð$ !1¸yÑIˆÜ °>ÔBÐBùòc Aùò =ùò0 ,s   ÄH+Ä1H0Æ	H5)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBILINEARÚboolr   ÚstrrC   r   Úfloatr   r   r7   ÚnpÚndarrayr   rW   r_   r   ÚFIRSTr   r   ÚPILÚImagerp   Ú__classcell__)r:   s   @r;   r   r   5   sÅ  ø„ ñ%ðN (¨Ð5Ðð Ø#Ø'9×'BÑ'BØØ,3Ø!Ø:>Ø9=Ø)-Ø*.Ø48Ø48Ø>Bñ#Uàð#Uð �3˜�8‰nð#Uð %ð	#Uð
 ð#Uð ˜c 5˜jÑ)ð#Uð ð#Uð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ð#Uð ˜E %¨¨e©Ð"4Ñ5Ñ6ð#Uð ! ™ð#Uð " $™ð#Uð %-¨S¡Mð#Uð %-¨S¡Mð#Uð -5°U©Oð#Uð  
õ!#UðV ?CØDHñPà�z‰zðPð %(ðPð %(ð	Pð
 -2ðPð ˜e CÐ)9Ð$9Ñ:Ñ;ðPð $ E¨#Ð/?Ð*?Ñ$@ÑAóPðr ?CØDHñ*à�R—Z‘ZÑ ð*ð ð*ð %(ð	*ð
 %(ð*ð -2ð*ð ˜e CÐ)9Ð$9Ñ:Ñ;ð*ð $ E¨#Ð/?Ð*?Ñ$@ÑAó*ñ2 %Ó&ð %)Ø#Ø'+Ø%)Ø*.Ø'+Ø:>Ø9=Ø;?Ø2B×2HÑ2HØDHØ)-Ø*.Ø48Ø48Ø>Bñ%eCàðeCð ˜D‘>ðeCð �3˜�8‰nð	eCð
 %ðeCð ˜T‘NðeCð ! ™ðeCð ˜t‘nðeCð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ðeCð ˜E %¨¨e©Ð"4Ñ5Ñ6ðeCð !  s¨J Ñ!7Ñ8ðeCð Ð.Ñ/ðeCð $ E¨#Ð/?Ð*?Ñ$@ÑAðeCð ! ™ðeCð " $™ðeCð  %-¨S¡Mð!eCð" %-¨S¡Mð#eCð$ -5°U©Oð%eCð& 
�‰�‰ò'eCó 'ôeCr<   r   )*rt   rK   rD   Útypingr   r   r   r   Únumpyrz   Úimage_processing_utilsr   r	   r
   Úimage_transformsr   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   r   r   r   Úutilsr   r   r   r   Ú
get_loggerrq   rj   r}   r   Ú__all__r5   r<   r;   ú<module>rˆ      s�   ðñ (ã Û ß .Ó .ã ç UÑ U÷ñ ÷
÷ ÷ ó ÷ _Ó ^ð 
ˆ×	Ñ	˜HÓ	%€ñ ÔÛô`CÐ-ô `CðF "Ð
"�r<   