Ë
    T^(hÔ>  ã                   óà  — d Z ddlZddlmZ ddlmZmZmZmZ ddl	Z
ddl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 jB                  e"«      Z# e«       rdd
l$m%Z%  ed¬«      	 dde&de&de&de&de'dee&e&f   fd„«       Z(de
jR                  de&de
jR                  fd„Z*dde
jR                  de&de&dee
jR                  e
jR                  f   fd„Z+ G d„ de«      Z,dgZ-y)z"Image processor class for SigLIP2.é    N)Ú	lru_cache)ÚListÚOptionalÚTupleÚUnioné   )ÚBaseImageProcessorÚBatchFeature)Úconvert_to_rgbÚresizeÚto_channel_dimension_format)	ÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚ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Úlogging)ÚImageé   )ÚmaxsizeÚimage_heightÚimage_widthÚ
patch_sizeÚmax_num_patchesÚepsÚreturnc                 ó   — dt         dt        dt        dt        fd„}|dz  d}}||z
  |k\  r:||z   dz  } ||| |«      }	 ||||«      }
|	|z  |
|z  z  }||k  r|}n|}||z
  |k\  rŒ:|} ||| |«      }	 ||||«      }
|	|
fS )	a"  
    Determine image size based on max number of patches, ensure dimensions are divisible by patch size and image is at least 1 patch.

    Args:
        image_height (`int`):
            Original image height.
        image_width (`int`):
            Original image width.
        patch_size (`int`):
            Patch size for processing.
        max_num_patches (`int`):
            Maximum number of patches.
        eps (`float`):
            Small threshold for binary search.

    Returns:
        Tuple: (target_height, target_width)
    ÚscaleÚsizer    r#   c                 óp   — || z  }t        j                  ||z  «      |z  }t        ||«      }t        |«      S )N)ÚmathÚceilÚmaxÚint)r%   r&   r    Úscaled_sizes       úr/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/siglip2/image_processing_siglip2.pyÚget_scaled_image_sizezAget_image_size_for_max_num_patches.<locals>.get_scaled_image_sizeI   s:   € Ø˜U‘lˆÜ—i‘i ¨jÑ 8Ó9¸JÑFˆÜ˜* kÓ2ˆÜ�;ÓÐó    é
   g      Y@é   )Úfloatr+   )r   r   r    r!   r"   r.   Ú	scale_minÚ	scale_maxr%   Útarget_heightÚtarget_widthÚnum_patchess               r-   Ú"get_image_size_for_max_num_patchesr8   2   sÍ   € ð. ¤Uð  ´#ð  Ä3ð  Ì3ó  ð  ™8 Uˆy€IØ�yÑ  SÒ
(Ø˜YÑ&¨!Ñ+ˆÙ-¨e°\À:ÓNˆÙ,¨U°KÀÓLˆØ$ zÑ1°lÀZÑ6OÑPˆà˜/Ò)Ø‰IàˆIð �yÑ  SÓ
(ð €EÙ)¨%°¸zÓJ€MÙ(¨°¸ZÓH€LØ˜,Ð&Ð&r/   Úimagec                 ó¸   — | j                   \  }}}||z  }||z  }| j                  |||||«      }|j                  ddddd«      }|j                  ||z  d«      }|S )zË
    Convert 3D array image of shape (image_height, image_width, num_channels) into 2D array of patches of shape
    (num_patches_height * num_patches_width, patch_size * patch_size * num_channels).
    r   r1   é   r   é   éÿÿÿÿ)ÚshapeÚreshapeÚ	transpose)r9   r    r   r   Únum_channelsÚnum_patches_heightÚnum_patches_widthÚpatched_images           r-   Úconvert_image_to_patchesrE   b   s|   € ð
 /4¯k©kÑ+€L�+˜|Ø%¨Ñ3ÐØ# zÑ1ÐØ—M‘MÐ"4°jÐBSÐU_ÐamÓn€MØ!×+Ñ+¨A¨q°!°Q¸Ó:€MØ!×)Ñ)Ð*<Ð?PÑ*PÐRTÓU€MØÐr/   ÚarrayÚtarget_lengthÚ	pad_valuec                 óø   — | j                   d   }||z
  }t        j                  |ft        j                  ¬«      }|dkD  r8d|fgdg| j                  dz
  z  z   }t        j
                  | |d|¬«      } d|| d | |fS )z2
    Pad the array along the first dimension.
    r   )Údtype)r   r   r;   Úconstant)ÚmodeÚconstant_valuesN)r>   ÚnpÚonesÚint32ÚndimÚpad)rF   rG   rH   Úcurrent_lengthÚpadding_lengthÚmaskÚpaddingss          r-   Úpad_along_first_dimrW   p   sˆ   € ð —[‘[ ‘^€NØ" ^Ñ3€NÜ�7‰7�MÐ#¬2¯8©8Ô4€DØ˜ÒØ˜Ð'Ð(¨F¨8°u·z±zÀA±~Ñ+FÑFˆÜ—‘�u˜h¨ZÈÔSˆØ!"ˆˆnˆ_ÐÐØ�$ˆ;Ðr/   c                   óš  ‡ — e Zd ZdZg d¢Zdej                  ddddddddf
ded	ed
ededede	e
eee   f      de	e
eee   f      de	e   dedefˆ fd„Z e«       	 	 	 	 	 	 	 	 	 	 	 	 ddede	e   d	e	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
eef      de	e   de	e   de	e   ddfd„«       Zˆ xZS )ÚSiglip2ImageProcessora3	  
    Constructs a SigLIP2 image processor.

    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image's dimensions to fit `max_num_patches` according to given `patch_size`.
            Can be overridden by `do_resize` 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.
        patch_size (`int`, *optional*, defaults to 16):
            The size (resolution) of each patch the image will be split to.
        max_num_patches (`int`, *optional*, defaults to 256):
            The image will be resized to have at most this number of patches,
            and then padded in "patch" dimension to match this number exactly.
    ©Úpixel_valuesÚpixel_attention_maskÚspatial_shapesTgp?Né   r   Ú	do_resizeÚresampleÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanÚ	image_stdÚdo_convert_rgbr    r!   c                 óÐ   •— t        ‰| �  di |¤Ž |�|ng d¢}|�|ng d¢}|| _        || _        || _        || _        || _        || _        || _        || _	        |	| _
        |
| _        y )N)ç      à?rh   rh   © )ÚsuperÚ__init__r_   r`   ra   rb   rc   rd   re   rf   r    r!   )Úselfr_   r`   ra   rb   rc   rd   re   rf   r    r!   ÚkwargsÚ	__class__s               €r-   rk   zSiglip2ImageProcessor.__init__£   sw   ø€ ô 	‰ÑÑ"˜6Ò"à#-Ð#9‘Zºˆ
Ø!*Ð!6‘IºOˆ	à"ˆŒØ ˆŒØ$ˆŒØ,ˆÔØ(ˆÔØ$ˆŒØ"ˆŒØ,ˆÔØ$ˆŒØ.ˆÕr/   ÚimagesÚreturn_tensorsÚinput_data_formatr#   zImage.Imagec                 óÂ  — |�|n| j                   }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j
                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }t        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   «      }
g }g }g }|D ]ð  }t-        |||
¬«      }|r=t/        |j0                  d   |j0                  d   ||¬«      \  }}t3        |||f||¬«      }|r| j5                  |||¬	«      }|r| j7                  ||||¬
«      }t9        ||«      }t;        ||«      \  }}|j0                  d   |z  }|j0                  d   |z  }|j=                  ||f«       |j=                  |«       |j=                  |«       Œò t?        |||dœ|	¬«      }|S 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`.
            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.
            patch_size (`int`, *optional*, defaults to `self.patch_size`):
                Patch size for processing, same as the patch size used in the model.
            max_num_patches (`int`, *optional*, defaults to `self.max_num_patches`):
                Maximum number of patches per image, the image will be resized to have at most this number of patches.
        zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)ra   rb   rc   rd   re   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.)Úinput_channel_dimr;   )r   r   r    r!   )r9   r&   r`   rq   )r9   r%   rq   )r9   ÚmeanÚstdrq   rZ   )ÚdataÚtensor_type) r_   r`   ra   rb   rc   rd   re   rf   r    r!   r   ÚLASTr   r   Ú
ValueErrorr   r   r   r   ÚloggerÚwarning_oncer   r   r8   r>   r   ÚrescaleÚ	normalizerE   rW   Úappendr
   )rl   ro   r_   r`   ra   rb   rc   rd   re   rp   rq   rf   r    r!   Údata_formatr9   Úpixel_masksr[   r]   ÚheightÚwidthÚpatchesrU   rB   rC   Úbatch_features                             r-   Ú
preprocessz Siglip2ImageProcessor.preprocessÁ   s°  € ð| "+Ð!6‘I¸D¿N¹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^ˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø-<Ð-H™/Èd×NbÑNbˆô '×+Ñ+ˆä)¨&Ó1ˆä˜FÔ#Üð:óð ô 	&Ø!Ø)Ø%Ø!Øõ	
ñ Ø9?Ö@°”n UÕ+Ð@ˆFÐ@ð 6<Ö<¨E”. Õ'Ð<ˆÐ<áœ/¨&°©)Ô4Ü×Ñðsôð
 Ð$ä >¸vÀa¹yÓ IÐàˆØˆØˆàò 	%ˆEÜ/°°{ÐVgÔhˆEáÜ BØ!&§¡¨Q¡Ø %§¡¨A¡Ø)Ø$3ô	!‘�˜ô  U°&¸%°È8ÐgrÔs�áØŸ™¨5¸ÐZe˜Óf�áØŸ™¨U¸ÈÐfq˜Ór�ä.¨u°jÓAˆGÜ/°¸ÓI‰MˆG�TØ!&§¡¨Q¡°:Ñ!=ÐØ %§¡¨A¡°*Ñ <Ðà×!Ñ!Ð#5Ð7HÐ"IÔJØ×Ñ Ô(Ø×Ñ˜tÕ$ð3	%ô6 %à ,Ø(3Ø"0ñð
 'ô
ˆð Ðùòo Aùò =s   Ã(IÄ I)NNNNNNNNNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBILINEARÚboolr2   r   r   r   r+   rk   r   r   Ústrr   r   r…   Ú__classcell__)rn   s   @r-   rY   rY   ~   s  ø„ ñ òD SÐð Ø'9×'BÑ'BØØ 'Ø!Ø:>Ø9=Ø)-ØØ"ñ/àð/ð %ð/ð ð	/ð
 ð/ð ð/ð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ð/ð ˜E %¨¨e©Ð"4Ñ5Ñ6ð/ð ! ™ð/ð ð/ð õ/ñ< %Ó&ð %)Ø15Ø%)Ø*.Ø'+Ø:>Ø9=Ø;?ØDHØ)-Ø$(Ø)-ñRàðRð ˜D‘>ðRð Ð-Ñ.ð	Rð
 ˜T‘NðRð ! ™ðRð ˜t‘nðRð ˜U 5¨$¨u©+Ð#5Ñ6Ñ7ðRð ˜E %¨¨e©Ð"4Ñ5Ñ6ðRð !  s¨J Ñ!7Ñ8ðRð $ E¨#Ð/?Ð*?Ñ$@ÑAðRð ! ™ðRð ˜S‘MðRð " #™ðRð 
òRó 'ôRr/   rY   )gñhãˆµøä>)r   ).r‰   r(   Ú	functoolsr   Útypingr   r   r   r   ÚnumpyrN   Úimage_processing_utilsr	   r
   Úimage_transformsr   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   Úutilsr   r   r   r   Ú
get_loggerr†   rz   ÚPILr   r+   r2   r8   ÚndarrayrE   rW   rY   Ú__all__ri   r/   r-   ú<module>rš      s,  ðñ )ã Ý ß /Ó /ã ç F÷ñ ÷

÷ 
õ 
÷ _Ó ^ð 
ˆ×	Ñ	˜HÓ	%€ñ ÔÝñ �3Ôà]añ,'Øð,'Ø$'ð,'Ø58ð,'ØKNð,'ØUZð,'à
ˆ3�ˆ8�_ò,'ó ð,'ð^ B§J¡Jð ¸Cð ÀBÇJÁJó ñ˜rŸz™zð ¸#ð È#ð ÐV[Ð\^×\fÑ\fÐhj×hrÑhrÐ\rÑVsó ôVÐ.ô Vðr #Ð
#�r/   