Ë
    S^(h“1  ã                   ó  — d Z ddlmZmZmZmZmZmZ erddlm	Z	 ddl
ZddlZddlmZmZ ddl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 dd	lm Z m!Z!m"Z"m#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 GLPN.é    )ÚTYPE_CHECKINGÚDictÚListÚOptionalÚTupleÚUnioné   )ÚDepthEstimatorOutputN)ÚBaseImageProcessorÚBatchFeature)ÚresizeÚto_channel_dimension_format)
ÚChannelDimensionÚPILImageResamplingÚget_image_sizeÚinfer_channel_dimension_formatÚis_scaled_imageÚis_torch_availableÚmake_list_of_imagesÚto_numpy_arrayÚvalid_imagesÚvalidate_preprocess_arguments)Ú
TensorTypeÚfilter_out_non_signature_kwargsÚloggingÚrequires_backendsc                   óÈ  ‡ — e Zd ZdZdgZddej                  dfdedededd	fˆ fd
„Z	ej                  d	d	fde
j                  dededee   deeeef      de
j                  fd„Z e«       d	d	d	d	d	ej$                  d	fdedeed   ee   f   dee   dee   dee   deeeef      dedeeeef      defd„«       Z	 ddddeeeeeeef      d	f      deeeef      fd„Zˆ xZS )ÚGLPNImageProcessoraó  
    Constructs a GLPN image processor.

    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Whether to resize the image's (height, width) dimensions, rounding them down to the closest multiple of
            `size_divisor`. Can be overridden by `do_resize` in `preprocess`.
        size_divisor (`int`, *optional*, defaults to 32):
            When `do_resize` is `True`, images are resized so their height and width are rounded down to the closest
            multiple of `size_divisor`. Can be overridden by `size_divisor` in `preprocess`.
        resample (`PIL.Image` resampling filter, *optional*, defaults to `Resampling.BILINEAR`):
            Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether or not to apply the scaling factor (to make pixel values floats between 0. and 1.). Can be
            overridden by `do_rescale` in `preprocess`.
    Úpixel_valuesTé    Ú	do_resizeÚsize_divisorÚ
do_rescaleÚreturnNc                 ó\   •— || _         || _        || _        || _        t	        ‰| �  di |¤Ž y )N© )r!   r#   r"   ÚresampleÚsuperÚ__init__)Úselfr!   r"   r'   r#   ÚkwargsÚ	__class__s         €úl/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/glpn/image_processing_glpn.pyr)   zGLPNImageProcessor.__init__F   s2   ø€ ð #ˆŒØ$ˆŒØ(ˆÔØ ˆŒÜ‰ÑÑ"˜6Ó"ó    Úimager'   Údata_formatÚinput_data_formatc                 ól   — t        ||¬«      \  }}||z  |z  }	||z  |z  }
t        ||	|
ff|||dœ|¤Ž}|S )a*  
        Resize the image, rounding the (height, width) dimensions down to the closest multiple of size_divisor.

        If the image is of dimension (3, 260, 170) and size_divisor is 32, the image will be resized to (3, 256, 160).

        Args:
            image (`np.ndarray`):
                The image to resize.
            size_divisor (`int`):
                The image is resized so its height and width are rounded down to the closest multiple of
                `size_divisor`.
            resample:
                `PIL.Image` resampling 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 `None`, the channel dimension format of the input
                image is used. 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 of the input image. If not set, 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.

        Returns:
            `np.ndarray`: The resized image.
        )Úchannel_dim)r'   r0   r1   )r   r   )r*   r/   r"   r'   r0   r1   r+   ÚheightÚwidthÚnew_hÚnew_ws              r-   r   zGLPNImageProcessor.resizeT   si   € ôH ' uÐ:KÔL‰ˆ�à˜,Ñ&¨Ñ5ˆØ˜Ñ%¨Ñ4ˆÜØØ�EˆNð
ð Ø#Ø/ñ
ð ñ
ˆð ˆr.   ÚimageszPIL.Image.ImageÚreturn_tensorsc	           	      óˆ  — |�|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]  }
| j                  |
|||¬«      ‘Œ }}
|r!|D �
cg c]  }
| j                  |
d|¬«      ‘Œ }}
|D �
cg c]  }
t        |
||¬«      ‘Œ }}
d	|i}t!        ||¬
«      S c c}	w c c}
w c c}
w c c}
w )aÅ
  
        Preprocess the given images.

        Args:
            images (`PIL.Image.Image` or `TensorType` or `List[np.ndarray]` or `List[TensorType]`):
                Images 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 input such that the (height, width) dimensions are a multiple of `size_divisor`.
            size_divisor (`int`, *optional*, defaults to `self.size_divisor`):
                When `do_resize` is `True`, images are resized so their height and width are rounded down to the
                closest multiple of `size_divisor`.
            resample (`PIL.Image` resampling filter, *optional*, defaults to `self.resample`):
                `PIL.Image` resampling filter to use if resizing the image e.g. `PILImageResampling.BILINEAR`. Only has
                an effect if `do_resize` is set to `True`.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether or not to apply the scaling factor (to make pixel values floats between 0. and 1.).
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                    - `None`: 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.
        zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r!   Úsizer'   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'   r1   gp?)Úscaler1   )Úinput_channel_dimr   )ÚdataÚtensor_type)r!   r#   r"   r'   r   r   Ú
ValueErrorr   r   r   ÚloggerÚwarning_oncer   r   Úrescaler   r   )r*   r8   r!   r"   r'   r#   r9   r0   r1   Úimgr/   r>   s               r-   Ú
preprocesszGLPNImageProcessor.preprocess†   s…  € ð^ "+Ð!6‘I¸D¿N¹Nˆ	Ø#-Ð#9‘Z¸t¿¹ˆ
Ø'3Ð'?‘|ÀT×EVÑEVˆØ'Ð3‘8¸¿¹ˆä$ VÓ,ˆä˜FÔ#Üð:óð ô 	&ØØØõ	
ð 28Ö8¨#”. Õ%Ð8ˆÐ8áœ/¨&°©)Ô4Ü×Ñðsôð
 Ð$ä >¸vÀa¹yÓ IÐáð $öàð —‘˜E°ÀxÐct�ÕuðˆFð ñ
 ØkqÖrÐbg�d—l‘l 5°ÐK\�lÕ]ÐrˆFÐrð ouö
ØejÔ'¨¨{ÐN_Ö`ð
ˆð 
ð  Ð'ˆÜ °>ÔBÐBùò5 9ùòùò sùò
s   Á4D0ÃD5Ã'D:ÄD?Úoutputsr
   Útarget_sizesc                 ó~  — t        | d«       |j                  }|�"t        |«      t        |«      k7  rt        d«      ‚g }|€dgt        |«      z  n|}t	        ||«      D ]\  \  }}|�B|d   }t
        j                  j                  j                  ||dd¬«      }|j                  «       }|j                  d|i«       Œ^ |S )	aÊ  
        Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions and depth PIL images.
        Only supports PyTorch.

        Args:
            outputs ([`DepthEstimatorOutput`]):
                Raw outputs of the model.
            target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*):
                Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size
                (height, width) of each image in the batch. If left to None, predictions will not be resized.

        Returns:
            `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth
            predictions.
        ÚtorchNz]Make sure that you pass in as many target sizes as the batch dimension of the predicted depth)NN.ÚbicubicF)r;   ÚmodeÚalign_cornersÚpredicted_depth)r   rM   Úlenr@   ÚziprI   ÚnnÚ
functionalÚinterpolateÚsqueezeÚappend)r*   rF   rG   rM   ÚresultsÚdepthÚtarget_sizes          r-   Úpost_process_depth_estimationz0GLPNImageProcessor.post_process_depth_estimationç   sÔ   € ô( 	˜$ Ô(à!×1Ñ1ˆàÐ$¬3¨Ó+?Ä3À|ÓCTÒ+TÜØoóð ð ˆØ8DÐ8L˜�v¤ OÓ 4Ò4ÐR^ˆÜ"% o°|Ó"Dò 	7ÑˆE�;ØÐ&Ø˜oÑ.�ÜŸ™×+Ñ+×7Ñ7¸ÀKÐV_ÐotÐ7Óu�ØŸ™›�à�N‰NÐ-¨uÐ5Õ6ð	7ð ˆr.   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr   ÚBILINEARÚboolÚintr)   ÚnpÚndarrayr   r   r   Ústrr   r   ÚFIRSTr   r   r   rE   r   r   rX   Ú__classcell__)r,   s   @r-   r   r   2   sñ  ø„ ñð" (Ð(Ðð ØØ#×,Ñ,Øñ#àð#ð ð#ð
 ð#ð 
õ#ð$ (:×'BÑ'BØ26ØDHñ0à�z‰zð0ð ð0ð %ð	0ð
 Ð.Ñ/ð0ð $ E¨#Ð/?Ð*?Ñ$@ÑAð0ð 
�‰ó0ñd %Ó&ð %)Ø&*ØØ%)Ø;?Ø(8×(>Ñ(>ØDHñ^CàÐ'¨°TÐ:KÑ5LÈdÐS]ÑN^Ð^Ñ_ð^Cð ˜D‘>ð^Cð ˜s‘mð	^Cð ˜T‘Nð^Cð !  z°3 Ñ!7Ñ8ð^Cð &ð^Cð $ E¨#Ð/?Ð*?Ñ$@ÑAð^Cð 
ò^Có 'ð^CðF RVñ'à'ð'ð ˜u Z°°e¸CÀ¸H±oÑ1FÈÐ%LÑMÑNð'ð 
ˆd�3˜
�?Ñ#Ñ	$÷	'r.   r   )*r\   Útypingr   r   r   r   r   r   Úmodeling_outputsr
   Únumpyra   Ú	PIL.ImageÚPILÚimage_processing_utilsr   r   Úimage_transformsr   r   Úimage_utilsr   r   r   r   r   r   r   r   r   r   Úutilsr   r   r   r   rI   Ú
get_loggerrY   rA   r   Ú__all__r&   r.   r-   ú<module>rq      su   ðñ &ç D× Dñ Ý8ã Û ç Fß C÷÷ ÷ ÷ ]Ó \ñ ÔÛð 
ˆ×	Ñ	˜HÓ	%€ô\Ð+ô \ð~  Ð
 �r.   