
    IgF                         d dl Z d dlmZmZ ddlmZmZmZmZm	Z	 ddl
mZmZ  e       rd dlmZ ddlmZ  e       rdd	lmZ  ej&                  e      Z e ed
             G d de             Zy)    N)ListUnion   )add_end_docstringsis_torch_availableis_vision_availableloggingrequires_backends   )Pipelinebuild_pipeline_init_args)Image)
load_image)(MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMEST)has_image_processorc                   j     e Zd ZdZ fdZd
deeee   ded   f   f fdZddZ	d
dZ
d Zd	 Z xZS )DepthEstimationPipelinea  
    Depth estimation pipeline using any `AutoModelForDepthEstimation`. This pipeline predicts the depth of an image.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> depth_estimator = pipeline(task="depth-estimation", model="LiheYoung/depth-anything-base-hf")
    >>> output = depth_estimator("http://images.cocodataset.org/val2017/000000039769.jpg")
    >>> # This is a tensor with the values being the depth expressed in meters for each pixel
    >>> output["predicted_depth"].shape
    torch.Size([1, 384, 384])
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)


    This depth estimation pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"depth-estimation"`.

    See the list of available models on [huggingface.co/models](https://huggingface.co/models?filter=depth-estimation).
    c                 f    t        |   |i | t        | d       | j                  t               y )Nvision)super__init__r
   check_model_typer   )selfargskwargs	__class__s      d/var/www/html/answerous/venv/lib/python3.12/site-packages/transformers/pipelines/depth_estimation.pyr   z DepthEstimationPipeline.__init__3   s.    $)&)$)FG    inputszImage.Imagec                 h    d|v r|j                  d      }|t        d      t        |   |fi |S )a  
        Predict the depth(s) of the image(s) passed as inputs.

        Args:
            inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`):
                The pipeline handles three types of images:

                - A string containing a http link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL directly

                The pipeline accepts either a single image or a batch of images, which must then be passed as a string.
                Images in a batch must all be in the same format: all as http links, all as local paths, or all as PIL
                images.
            parameters (`Dict`, *optional*):
                A dictionary of argument names to parameter values, to control pipeline behaviour.
                The only parameter available right now is `timeout`, which is the length of time, in seconds,
                that the pipeline should wait before giving up on trying to download an image.

        Return:
            A dictionary or a list of dictionaries containing result. If the input is a single image, will return a
            dictionary, if the input is a list of several images, will return a list of dictionaries corresponding to
            the images.

            The dictionaries contain the following keys:

            - **predicted_depth** (`torch.Tensor`) -- The predicted depth by the model as a `torch.Tensor`.
            - **depth** (`PIL.Image`) -- The predicted depth by the model as a `PIL.Image`.
        imageszECannot call the depth-estimation pipeline without an inputs argument!)pop
ValueErrorr   __call__)r   r   r   r   s      r   r$   z DepthEstimationPipeline.__call__8   sA    > vZZ)F>deew1&11r   c                     i }|t        j                  dt               ||d<   t        |t              rd|v r|d   |d<   |i i fS )NzUThe `timeout` argument is deprecated and will be removed in version 5 of Transformerstimeout)warningswarnFutureWarning
isinstancedict)r   r&   
parametersr   preprocess_paramss        r   _sanitize_parametersz,DepthEstimationPipeline._sanitize_parameters]   s[    MMgiv ,3i(j$'I,C+5i+@i( "b((r   c                     t        ||      }| j                  || j                        }| j                  dk(  r|j                  | j                        }|j
                  d d d   |d<   |S )N)r!   return_tensorspttarget_size)r   image_processor	frameworktotorch_dtypesize)r   imager&   model_inputss       r   
preprocessz"DepthEstimationPipeline.preprocessh   sd    5'*++5+X>>T!'??4+;+;<L&+jj2&6]#r   c                 V    |j                  d      } | j                  di |}||d<   |S )Nr3    )r"   model)r   r:   r3   model_outputss       r   _forwardz DepthEstimationPipeline._forwardp   s5    "&&}5"

2\2'2m$r   c                    | j                   j                  ||d   g      }g }|D ]  }|d   j                         j                         j	                         }||j                         z
  |j                         |j                         z
  z  }t        j                  |dz  j                  d            }|j                  |d   |d        t        |      dk(  r|d   S |S )Nr3   predicted_depth   uint8)rB   depthr   r   )r4   post_process_depth_estimationdetachcpunumpyminmaxr   	fromarrayastypeappendlen)r   r?   outputsformatted_outputsoutputrE   s         r   postprocessz#DepthEstimationPipeline.postprocessv   s    &&DD =)*	
  	eF,-446::<BBDEUYY[(UYY[599;-FGEOOUS[$8$8$ABE$$@Q9R]b%cd	e (+7|q'8 #O>OOr   )N)NN)__name__
__module____qualname____doc__r   r   strr   r$   r.   r;   r@   rS   __classcell__)r   s   @r   r   r      sG    0H
#2uS$s)]DDW%WX #2J	)Pr   r   )r'   typingr   r   utilsr   r   r   r	   r
   baser   r   PILr   image_utilsr   models.auto.modeling_autor   
get_loggerrT   loggerr   r=   r   r   <module>rb      sp       5 (T			H	% ,FGlPh lP HlPr   