
    rh                          d dl mZ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)    )AnyUnionoverload   )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dZdZdZdZ fdZe	de
edf   dedeeef   fd	       Ze	dee
edf      dedeeeef      fd
       Zde
eee   ded   f   dede
eeef   eeeef      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).
    FTc                 f    t        |   |i | t        | d       | j                  t               y )Nvision)super__init__r   check_model_typer   )selfargskwargs	__class__s      z/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/pipelines/depth_estimation.pyr   z DepthEstimationPipeline.__init__7   s.    $)&)$)FG    inputszImage.Imager   returnc                      y N r   r    r   s      r   __call__z DepthEstimationPipeline.__call__<   s    \_r   c                      y r#   r$   r%   s      r   r&   z DepthEstimationPipeline.__call__?   s    hkr   c                 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.
            timeout (`float`, *optional*, defaults to None):
                The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
                the call may block forever.

        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   r&   )r   r    r   r   s      r   r&   z DepthEstimationPipeline.__call__B   sB    H vZZ)F>deew1&11r   c                 V    i }|||d<   t        |t              rd|v r|d   |d<   |i i fS )Ntimeout)
isinstancedict)r   r-   
parametersr   preprocess_paramss        r   _sanitize_parametersz,DepthEstimationPipeline._sanitize_parametersl   sG    +2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.preprocesst   sd    5'*++5+X>>T!'??4+;+;<L&+jj2&6]#r   c                 V    |j                  d      } | j                  di |}||d<   |S )Nr7   r$   )r*   model)r   r>   r7   model_outputss       r   _forwardz DepthEstimationPipeline._forward|   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 )Nr7   predicted_depth   uint8)rE   depthr   r   )r8   post_process_depth_estimationdetachcpunumpyminmaxr   	fromarrayastypeappendlen)r   rB   outputsformatted_outputsoutputrH   s         r   postprocessz#DepthEstimationPipeline.postprocess   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   )NNr#   )__name__
__module____qualname____doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr   r   r   strr   r/   r&   listr2   r?   rC   rV   __classcell__)r   s   @r   r   r      s   0 O #OH
 _uS-%78_C_DQTVYQYN_ _ktE#}*<$=>k#kRVW[\_ad\dWeRfk k(2CcM4;NNO(2[^(2	tCH~tDcN33	4(2T)Pr   r   N)typingr   r   r   utilsr   r   r	   r
   r   baser   r   PILr   image_utilsr   models.auto.modeling_autor   
get_loggerrW   loggerr   r$   r   r   <module>rj      sp    ' '  5 (T			H	% ,FGyPh yP HyPr   