o
    Zh1                     @   s   d Z ddlmZmZmZmZmZmZ ddl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 rcddl&Z&e$'e(Z)e	ddG dd deZ*dgZ+dS )zImage processor class for GLPN.    )TYPE_CHECKINGDictListOptionalTupleUnion   )requires)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_backends)Zvision)backendsc                       s>  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
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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dZ		ddddeeeeeeef  d	f  deeeef  fddZ  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                    s.   || _ || _|| _|| _t jdi | d S )N )r!   r#   r"   resamplesuper__init__)selfr!   r"   r&   r#   kwargs	__class__r%   ]/var/www/auris/lib/python3.10/site-packages/transformers/models/glpn/image_processing_glpn.pyr(   I   s
   zGLPNImageProcessor.__init__imager&   data_formatinput_data_formatc                 K   sJ   t ||d\}}|| | }	|| | }
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.
        )Zchannel_dim)r&   r/   r0   )r   r   )r)   r.   r"   r&   r/   r0   r*   heightwidthZnew_hZnew_wr%   r%   r-   r   W   s   $zGLPNImageProcessor.resizeimageszPIL.Image.Imagereturn_tensorsc	           
         s  |dur|nj }|dur|nj}durnjdur!njt|}t|s0tdt|d dd |D }|rKt|d rKt	
d du rUt|d |rcfdd|D }|rofd	d|D } fd
d|D }d|i}	t|	|dS )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.
        NzkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)r!   sizer&   c                 S   s   g | ]}t |qS r%   )r   ).0Zimgr%   r%   r-   
<listcomp>   s    z1GLPNImageProcessor.preprocess.<locals>.<listcomp>r   zIt 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.c                    s   g | ]}j | d qS ))r"   r&   r0   )r   r6   r.   )r0   r&   r)   r"   r%   r-   r7      s    c                    s   g | ]
}j |d  dqS )gp?)scaler0   )Zrescaler8   )r0   r)   r%   r-   r7      s    c                    s   g | ]	}t | d qS ))Zinput_channel_dim)r   r8   )r/   r0   r%   r-   r7      s    r   )dataZtensor_type)r!   r#   r"   r&   r   r   
ValueErrorr   r   loggerZwarning_oncer   r   )
r)   r3   r!   r"   r&   r#   r4   r/   r0   r:   r%   )r/   r0   r&   r)   r"   r-   
preprocess   s@   /zGLPNImageProcessor.preprocessoutputsr
   target_sizesc                 C   s   t | d |j}|durt|t|krtdg }|du r%dgt| n|}t||D ]"\}}|durG|d }tjjj||ddd}|	 }|
d|i q,|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.ZbicubicF)r5   modeZalign_cornerspredicted_depth)r   rB   lenr;   zipr@   nnZ
functionalZinterpolateZsqueezeappend)r)   r>   r?   rB   resultsdepthZtarget_sizer%   r%   r-   post_process_depth_estimation   s   
z0GLPNImageProcessor.post_process_depth_estimation)N)__name__
__module____qualname____doc__Zmodel_input_namesr   ZBILINEARboolintr(   npZndarrayr   r   r   strr   r   ZFIRSTr   r   r   r=   r   r   rI   __classcell__r%   r%   r+   r-   r   4   s    
2	
cr   ),rM   typingr   r   r   r   r   r   Zutils.import_utilsr	   Zmodeling_outputsr
   numpyrP   Z	PIL.ImageZPILZimage_processing_utilsr   r   Zimage_transformsr   r   Zimage_utilsr   r   r   r   r   r   r   r   r   r   utilsr   r   r   r   r@   Z
get_loggerrJ   r<   r   __all__r%   r%   r%   r-   <module>   s$    0
 
`