
     `i1                     4   d Z ddl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            rddl#Z# e!j$        e%          Z& ed           G d de                      Z'dgZ(dS )zImage processor class for GLPN.    )TYPE_CHECKINGOptional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_flat_list_of_imagesto_numpy_arrayvalid_imagesvalidate_preprocess_arguments)
TensorTypefilter_out_non_signature_kwargsloggingrequires_backends)vision)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                 r    || _         || _        || _        || _         t	                      j        di | d S )N )r!   r#   r"   resamplesuper__init__)selfr!   r"   r'   r#   kwargs	__class__s         /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/glpn/image_processing_glpn.pyr)   zGLPNImageProcessor.__init__I   sE     #$( ""6"""""    imager'   data_formatinput_data_formatc                 v    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.resizeW   sy    H 'u:KLLL,&5%4EN
 #/
 
 
 
 r.   imageszPIL.Image.Imagereturn_tensorsc	                     ||n j         }||n j        }n j        n j        t	          |          }t          |          st          d          t          |           d |D             }|r/t          |d                   rt          
                    d           t          |d                   |r fd|D             }|r fd|D             }fd	|D             }d
|i}	t          |	|          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                 ,    g | ]}t          |          S r&   )r   ).0imgs     r-   
<listcomp>z1GLPNImageProcessor.preprocess.<locals>.<listcomp>   s     888#.%%888r.   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                 B    g | ]}                     |           S ))r"   r'   r1   )r   )r=   r/   r1   r'   r*   r"   s     r-   r?   z1GLPNImageProcessor.preprocess.<locals>.<listcomp>   s>        Exctuu  r.   c                 @    g | ]}                     |d           S )gp?)scaler1   )rescale)r=   r/   r1   r*   s     r-   r?   z1GLPNImageProcessor.preprocess.<locals>.<listcomp>   s-    rrrbgdll5K\l]]rrrr.   c                 4    g | ]}t          |           S ))input_channel_dim)r   )r=   r/   r0   r1   s     r-   r?   z1GLPNImageProcessor.preprocess.<locals>.<listcomp>   s7     
 
 
ej'{N_```
 
 
r.   r   )datatensor_type)r!   r#   r"   r'   r   r   
ValueErrorr   r   loggerwarning_oncer   r
   )
r*   r8   r!   r"   r'   r#   r9   r0   r1   rF   s
   `  ``  `` r-   
preprocesszGLPNImageProcessor.preprocess   s   ^ "+!6IIDN	#-#9ZZt
'3'?||TEV'388)&11F## 	:   	&	
 	
 	
 	
 98888 	/&)44 	s  
 $ >vay I I 	      #  F
  	srrrrrkqrrrF
 
 
 
 
nt
 
 
 '>BBBBr.   outputsr   target_sizesc                    t          | d           |j        }|/t          |          t          |          k    rt          d          g }|dgt          |          z  n|}t	          ||          D ]b\  }}|D|d         }t
          j        j                            ||dd          }|	                                }|
                    d|i           c|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   rS   lenrH   ziprO   nn
functionalinterpolatesqueezeappend)r*   rL   rM   rS   resultsdepthtarget_sizes          r-   post_process_depth_estimationz0GLPNImageProcessor.post_process_depth_estimation   s    ( 	$(((!1$3+?+?3|CTCT+T+To   8D8LvO 4 444R^"%o|"D"D 	7 	7E;&o.+77KV_ot7uuNN-u56666r.   )N)__name__
__module____qualname____doc__model_input_namesr   BILINEARboolintr)   npndarrayr   r   r   strr   r   FIRSTr   listr
   rK   tupledictr^   __classcell__)r,   s   @r-   r   r   4   si        " (( #,# ## #
 # 
# # # # # #$ (:'B26DH0 0z0 0 %	0
 ./0 $E#/?*?$@A0 
0 0 0 0d %$&& %)&*%);?(8(>DH^C ^C'T:K5LdS]N^^_^C D>^C sm	^C TN^C !z3!78^C &^C $E#/?*?$@A^C 
^C ^C ^C '&^CF RV' ''' uZeCHo1F%LMN' 
d3
?#	$	' ' ' ' ' ' ' 'r.   r   ))rb   typingr   r   r   utils.import_utilsr   modeling_outputsr   numpyrg   	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   rO   
get_loggerr_   rI   r   __all__r&   r.   r-   <module>r{      s   & % 1 1 1 1 1 1 1 1 1 1 * * * * * *  9888888         F F F F F F F F C C C C C C C C                        ] \ \ \ \ \ \ \ \ \ \ \  LLL 
	H	%	% 
;\ \ \ \ \+ \ \  \~  
 r.   