Ë
    T^(h€T  ã                   ó  — d Z ddlmZ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 ddlmZmZmZm Z m!Z!m"Z"  e!«       rddl#Z# e«       rddl$Z$ e"jJ                  e&«      Z' G d	„ d
e
«      Z(d
gZ)y)z$Image processor class for MobileViT.é    )ÚDictÚListÚOptionalÚTupleÚUnionNé   )ÚBaseImageProcessorÚBatchFeatureÚget_size_dict)Úflip_channel_orderÚget_resize_output_image_sizeÚ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_torch_availableÚis_torch_tensorÚis_vision_availableÚloggingc                   óB  ‡ — e Zd ZdZdgZddej                  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
f   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d!ˆ fd„	Z	 	 	 	 	 d"deded	edededeee	e
f      ded
ee   deee	e
f      deee	ef      fd„Z	 	 	 	 	 	 	 	 	 	 d#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
f   dee   deee	ef      deee	ef      dej                  fd„Z	 	 	 	 	 d"dedee   dee	e
f   dee   d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ej4                  dfdedee   dee   dee	e
f   ded	ee   d
ee   dee   dee	e
f   dee   deee	ef      dedeee	ef      dej:                  j:                  fd„«       Zd!dee    fd„Z!ˆ xZ"S )$ÚMobileViTImageProcessora$  
    Constructs a MobileViT 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 the
            `do_resize` parameter in the `preprocess` method.
        size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`):
            Controls the size of the output image after resizing. Can be overridden by the `size` parameter in the
            `preprocess` method.
        resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`):
            Defines the resampling filter to use if resizing the image. Can be overridden by the `resample` parameter
            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 the `do_rescale`
            parameter 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 the `rescale_factor` parameter in the
            `preprocess` method.
        do_center_crop (`bool`, *optional*, defaults to `True`):
            Whether to crop the input at the center. If the input size is smaller than `crop_size` along any edge, the
            image is padded with 0's and then center cropped. Can be overridden by the `do_center_crop` parameter in
            the `preprocess` method.
        crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 256, "width": 256}`):
            Desired output size `(size["height"], size["width"])` when applying center-cropping. Can be overridden by
            the `crop_size` parameter in the `preprocess` method.
        do_flip_channel_order (`bool`, *optional*, defaults to `True`):
            Whether to flip the color channels from RGB to BGR. Can be overridden by the `do_flip_channel_order`
            parameter in the `preprocess` method.
    Úpixel_valuesTNgp?Ú	do_resizeÚsizeÚresampleÚ
do_rescaleÚrescale_factorÚdo_center_cropÚ	crop_sizeÚdo_flip_channel_orderÚreturnc	                 óê   •— t        ‰
| �  d	i |	¤Ž |�|nddi}t        |d¬«      }|�|ndddœ}t        |d¬«      }|| _        || _        || _        || _        || _        || _        || _	        || _
        y )
NÚshortest_edgeéà   F©Údefault_to_squareé   )ÚheightÚwidthr(   ©Ú
param_name© )ÚsuperÚ__init__r   r"   r#   r$   r%   r&   r'   r(   r)   )Úselfr"   r#   r$   r%   r&   r'   r(   r)   ÚkwargsÚ	__class__s             €úv/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/mobilevit/image_processing_mobilevit.pyr7   z MobileViTImageProcessor.__init__X   s‹   ø€ ô 	‰ÑÑ"˜6Ò"ØÐ'‰t¨o¸sÐ-CˆÜ˜T°UÔ;ˆØ!*Ð!6‘IÀsÐUXÑ<Yˆ	Ü! )¸ÔDˆ	à"ˆŒØˆŒ	Ø ˆŒØ$ˆŒØ,ˆÔØ,ˆÔØ"ˆŒØ%:ˆÕ"ó    ÚimageÚdata_formatÚinput_data_formatc                 óš   — d}d|v r|d   }d}nd|v rd|v r|d   |d   f}nt        d«      ‚t        ||||¬«      }t        |f||||dœ|¤ŽS )	a[  
        Resize an image. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge
        resized to keep the input aspect ratio.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`Dict[str, int]`):
                Size of the output image.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                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.
        Tr,   Fr1   r2   zASize must contain either 'shortest_edge' or 'height' and 'width'.)r#   r/   r?   )r#   r$   r>   r?   )Ú
ValueErrorr   r   )	r8   r=   r#   r$   r>   r?   r9   r/   Úoutput_sizes	            r;   r   zMobileViTImageProcessor.resizet   s‘   € ð2 !ÐØ˜dÑ"Ø˜Ñ(ˆDØ %ÑØ˜Ñ '¨T¡/Ø˜‘N D¨¡MÐ2‰DäÐ`ÓaÐaä2ØØØ/Ø/ô	
ˆô Øð
àØØ#Ø/ñ
ð ñ
ð 	
r<   c                 ó   — t        |||¬«      S )a  
        Flip the color channels from RGB to BGR or vice versa.

        Args:
            image (`np.ndarray`):
                The image, represented as a numpy array.
            data_format (`ChannelDimension` or `str`, *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?   )r   )r8   r=   r>   r?   s       r;   r   z*MobileViTImageProcessor.flip_channel_order¥   s   € ô" " %°[ÐTeÔfÐfr<   c                 ó(   •— t        ‰| �  |fd|i|¤ŽS )zó
        Preprocesses a batch of images and optionally segmentation maps.

        Overrides the `__call__` method of the `Preprocessor` class so that both images and segmentation maps can be
        passed in as positional arguments.
        Úsegmentation_maps)r6   Ú__call__)r8   ÚimagesrE   r9   r:   s       €r;   rF   z MobileViTImageProcessor.__call__¸   s    ø€ ô ‰wÑ ÑVÐ:KÐVÈvÑVÐVr<   c                 ó¶   — |r| j                  ||||
¬«      }|r| j                  |||
¬«      }|r| j                  ||	|
¬«      }|r| j                  ||
¬«      }|S )N)r=   r#   r$   r?   )r=   Úscaler?   )r=   r#   r?   )r?   )r   ÚrescaleÚcenter_cropr   )r8   r=   r"   r%   r'   r)   r#   r$   r&   r(   r?   s              r;   Ú_preprocessz#MobileViTImageProcessor._preprocessÁ   so   € ñ Ø—K‘K e°$ÀÐ]n�KÓoˆEáØ—L‘L u°NÐVg�LÓhˆEáØ×$Ñ$¨5°yÐTeÐ$ÓfˆEá Ø×+Ñ+¨EÐEVÐ+ÓWˆEàˆr<   c                 óÌ   — t        |«      }|r t        |«      rt        j                  d«       |€t	        |«      }| j                  |||||||||	|¬«
      }t        ||
|¬«      }|S )zPreprocesses a single image.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?   )Úinput_channel_dim)r   r   ÚloggerÚwarning_oncer   rL   r   )r8   r=   r"   r#   r$   r%   r&   r'   r(   r)   r>   r?   s               r;   Ú_preprocess_imagez)MobileViTImageProcessor._preprocess_imageÜ   sˆ   € ô  ˜uÓ%ˆÙœ/¨%Ô0Ü×Ñðsôð Ð$Ü >¸uÓ EÐà× Ñ ØØØØØ!Ø)Ø)ØØ"7Ø/ð !ó 
ˆô ,¨E°;ÐRcÔdˆàˆr<   Úsegmentation_mapc                 ó@  — t        |«      }|j                  dk(  rd}|d   }t        j                  }nd}|€t	        |d¬«      }| j                  |||t        j                  d||d|¬«	      }|r|j                  d«      }|j                  t        j                  «      }|S )	zPreprocesses a single mask.é   T)N.Fé   )Únum_channels)	r=   r"   r#   r$   r%   r'   r(   r)   r?   r   )r   Úndimr   ÚFIRSTr   rL   r   ÚNEARESTÚsqueezeÚastypeÚnpÚint64)r8   rR   r"   r#   r'   r(   r?   Úadded_channel_dims           r;   Ú_preprocess_maskz(MobileViTImageProcessor._preprocess_mask  s¾   € ô *Ð*:Ó;Ðà× Ñ  AÒ%Ø $ÐØ/°	Ñ:ÐÜ 0× 6Ñ 6Ñà %ÐØ Ð(Ü$BÐCSÐbcÔ$dÐ!à×+Ñ+Ø"ØØÜ'×/Ñ/ØØ)ØØ"'Ø/ð ,ó 

Ðñ Ø/×7Ñ7¸Ó:ÐØ+×2Ñ2´2·8±8Ó<ÐØÐr<   rG   rE   Úreturn_tensorsc                 óÞ  — |�|n| j                   }|�|n| j                  }|�|n| j                  }|�|n| j                  }|�|n| j                  }|
�|
n| j
                  }
|�|n| j                  }t        |d¬«      }|	�|	n| j                  }	t        |	d¬«      }	t        |«      }|�t        |d¬«      }t        |«      }t        |«      st        d«      ‚|�t        |«      st        d«      ‚t        ||||	|||¬	«       |D �cg c]  }| j                  ||||||||	|
||¬
«      ‘Œ  }}d|i}|�)|D �cg c]  }| j                  |||||	|¬«      ‘Œ }}||d<   t        ||¬«      S c c}w c c}w )aj  
        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`.
            segmentation_maps (`ImageInput`, *optional*):
                Segmentation map to preprocess.
            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 by rescale factor.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`):
                Whether to center crop the image.
            crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`):
                Size of the center crop if `do_center_crop` is set to `True`.
            do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`):
                Whether to flip the channel order of the image.
            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.
            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.
        Fr.   r(   r3   rT   )Úexpected_ndimszkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.zvInvalid segmentation map 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"   r#   r$   r%   r&   r'   r(   r)   r>   r?   r!   )rR   r"   r#   r'   r(   r?   Úlabels)ÚdataÚtensor_type)r"   r$   r%   r&   r'   r)   r#   r   r(   r   r   rA   r   rQ   r_   r
   )r8   rG   rE   r"   r#   r$   r%   r&   r'   r(   r)   r`   r>   r?   Úimgrd   rR   s                    r;   Ú
preprocessz"MobileViTImageProcessor.preprocess,  sø  € ðz "+Ð!6‘I¸D¿N¹Nˆ	Ø'Ð3‘8¸¿¹ˆØ#-Ð#9‘Z¸t¿¹ˆ
Ø+9Ð+E™È4×K^ÑK^ˆØ+9Ð+E™È4×K^ÑK^ˆà%:Ð%FÑ!ÈD×LfÑLfð 	ð Ð'‰t¨T¯Y©YˆÜ˜T°UÔ;ˆØ!*Ð!6‘I¸D¿N¹Nˆ	Ü! )¸ÔDˆ	ä$ VÓ,ˆàÐ(Ü 3Ð4EÐVWÔ XÐä$ VÓ,ˆä˜FÔ#Üð:óð ð
 Ð(´Ð>OÔ1PÜð:óð ô
 	&Ø!Ø)Ø)ØØØØõ	
ð0 ö
ð ð ×"Ñ"ØØ#ØØ!Ø%Ø-Ø-Ø#Ø&;Ø'Ø"3ð #õ ð
ˆð 
ð"  Ð'ˆàÐ(ð ):ö
!ð %ð ×%Ñ%Ø%5Ø'ØØ#1Ø'Ø&7ð &õ ð
!Ðð 
!ð /ˆD�‰Nä °>ÔBÐBùòE
ùò(
!s   Ä#E%Ä3E*Útarget_sizesc                 ó&  — |j                   }|�Ãt        |«      t        |«      k7  rt        d«      ‚t        |«      r|j	                  «       }g }t        t        |«      «      D ]k  }t        j                  j                  j                  ||   j                  d¬«      ||   dd¬«      }|d   j                  d¬«      }|j                  |«       Œm |S |j                  d¬«      }t        |j                  d   «      D �cg c]  }||   ‘Œ	 }}|S c c}w )a@  
        Converts the output of [`MobileViTForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch.

        Args:
            outputs ([`MobileViTForSemanticSegmentation`]):
                Raw outputs of the model.
            target_sizes (`List[Tuple]` of length `batch_size`, *optional*):
                List of tuples corresponding to the requested final size (height, width) of each prediction. If unset,
                predictions will not be resized.

        Returns:
            semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic
            segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is
            specified). Each entry of each `torch.Tensor` correspond to a semantic class id.
        zTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   )ÚdimÚbilinearF)r#   ÚmodeÚalign_cornersrU   )ÚlogitsÚlenrA   r   ÚnumpyÚrangeÚtorchÚnnÚ
functionalÚinterpolateÚ	unsqueezeÚargmaxÚappendÚshape)	r8   Úoutputsrh   rn   Úsemantic_segmentationÚidxÚresized_logitsÚsemantic_mapÚis	            r;   Ú"post_process_semantic_segmentationz:MobileViTImageProcessor.post_process_semantic_segmentation¹  s(  € ð" —‘ˆð Ð#Ü�6‹{œc ,Ó/Ò/Ü Øjóð ô ˜|Ô,Ø+×1Ñ1Ó3�à$&Ð!äœS ›[Ó)ò ;�Ü!&§¡×!4Ñ!4×!@Ñ!@Ø˜3‘K×)Ñ)¨aÐ)Ó0°|ÀCÑ7HÈzÐinð "Aó "�ð  .¨aÑ0×7Ñ7¸AÐ7Ó>�Ø%×,Ñ,¨\Õ:ð;ð %Ð$ð %+§M¡M°a MÓ$8Ð!ÜGLÐMb×MhÑMhÐijÑMkÓGlÖ$mÀ!Ð%:¸1Ó%=Ð$mÐ!Ð$mà$Ð$ùò %ns   Ã>D)NN)N)NNNNN)
NNNNNNNNNN)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBILINEARÚboolr   ÚstrÚintr   Úfloatr7   r\   Úndarrayr   r   r   r   rF   r   rL   rQ   r_   r   rX   r   ÚPILÚImagerg   r   r   r€   Ú__classcell__)r:   s   @r;   r    r    6   sð  ø„ ñð> (Ð(Ðð Ø#Ø'9×'BÑ'BØØ,3Ø#Ø$(Ø&*ñ;àð;ð �3˜�8‰nð;ð %ð	;ð
 ð;ð ˜c 5˜jÑ)ð;ð ð;ð ˜˜S˜‘>ð;ð  $ð;ð 
õ;ð@ (:×'BÑ'BØ>BØDHñ/
à�z‰zð/
ð �3˜�8‰nð/
ð %ð	/
ð
 ˜e CÐ)9Ð$9Ñ:Ñ;ð/
ð $ E¨#Ð/?Ð*?Ñ$@ÑAð/
ð 
�‰ó/
ðh ?CØDHñ	gà�z‰zðgð ˜e CÐ)9Ð$9Ñ:Ñ;ðgð $ E¨#Ð/?Ð*?Ñ$@ÑAð	gð
 
�‰ógõ&Wð  *.Ø'+Ø*.Ø.2ØDHñàðð ðð ð	ð
 ðð  $ðð �t˜C ˜H‘~Ñ&ðð %ðð ! ™ðð ˜D  c ™NÑ+ðð $ E¨#Ð/?Ð*?Ñ$@ÑAóð< %)Ø#Ø'+Ø%)Ø*.Ø)-Ø$(Ø04Ø>BØDHñ(àð(ð ˜D‘>ð(ð �3˜�8‰nð	(ð
 %ð(ð ˜T‘Nð(ð ! ™ð(ð ! ™ð(ð ˜˜S˜‘>ð(ð  (¨™~ð(ð ˜e CÐ)9Ð$9Ñ:Ñ;ð(ð $ E¨#Ð/?Ð*?Ñ$@ÑAð(ð 
�‰ó(ðZ %)Ø#Ø)-Ø$(ØDHñ$ à$ð$ ð ˜D‘>ð$ ð �3˜�8‰nð	$ ð
 ! ™ð$ ð ˜˜S˜‘>ð$ ð $ E¨#Ð/?Ð*?Ñ$@ÑAð$ ð 
�‰ó$ ñL %Ó&ð 37Ø$(Ø#Ø'+Ø%)Ø*.Ø)-Ø$(Ø04Ø;?Ø(8×(>Ñ(>ØDHñICàðICð $ JÑ/ðICð ˜D‘>ð	ICð
 �3˜�8‰nðICð %ðICð ˜T‘NðICð ! ™ðICð ! ™ðICð ˜˜S˜‘>ðICð  (¨™~ðICð !  s¨J Ñ!7Ñ8ðICð &ðICð $ E¨#Ð/?Ð*?Ñ$@ÑAðICð 
�‰�‰òICó 'ðICñX)%ÈÈUÉ÷ )%r<   r    )*r„   Útypingr   r   r   r   r   rp   r\   Úimage_processing_utilsr	   r
   r   Úimage_transformsr   r   r   r   Úimage_utilsr   r   r   r   r   r   r   r   r   Úutilsr   r   r   r   r   r   rŒ   rr   Ú
get_loggerr�   rO   r    Ú__all__r5   r<   r;   ú<module>r–      sz   ðñ +ç 5Õ 5ã ç UÑ Uß uÓ u÷
÷ 
õ 
÷÷ ñ ÔÛáÔÛð 
ˆ×	Ñ	˜HÓ	%€ôl%Ð0ô l%ð^ %Ð
%�r<   