
    gF                        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dS )    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                   |     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                      t                      j        |i | t          | d           |                     t                     d S )Nvision)super__init__r
   check_model_typer   )selfargskwargs	__class__s      c/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/pipelines/depth_estimation.pyr   z DepthEstimationPipeline.__init__3   sJ    $)&)))$)))FGGGGG    NinputszImage.Imagec                     d|v r|                     d          }|t          d           t                      j        |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`.
        imagesNzECannot call the depth-estimation pipeline without an inputs argument!)pop
ValueErrorr   __call__)r   r   r   r   s      r   r$   z DepthEstimationPipeline.__call__8   sT    > vZZ))F>deeeuww11&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]   so    Mgiv   ,3i(j$'' 	AI,C,C+5i+@i( "b((r   c                     t          ||          }|                     || j                  }| j        dk    r|                    | 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   si    5'**++5+XX>T!!'??4+;<<L&+j2&6]#r   c                 T    |                     d          } | j        di |}||d<   |S )Nr3    )r"   model)r   r:   r3   model_outputss       r   _forwardz DepthEstimationPipeline._forwardp   s<    "&&}55"
22\22'2m$r   c                    | j                             ||d         g          }g }|D ]}|d                                                                                                         }||                                z
  |                                |                                z
  z  }t          j        |dz  	                    d                    }|
                    |d         |d           t          |          dk    r|d         n|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 	eF,-4466::<<BBDDEUYY[[(UYY[[599;;-FGEOUS[$8$8$A$ABBE$$@Q9R]b%c%cdddd'*7||q'8'8 ##>OOr   )N)NN)__name__
__module____qualname____doc__r   r   strr   r$   r.   r;   r@   rS   __classcell__)r   s   @r   r   r      s         0H H H H H
#2 #2uS$s)]DDW%WX #2 #2 #2 #2 #2 #2J	) 	) 	) 	)     P P P P P P 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      sc                         5 4 4 4 4 4 4 4  )(((((( UTTTTTT		H	%	% ,,FFFGGlP lP lP lP lPh lP lP HGlP lP lPr   