Ë
    T^(ht  ã                   óÞ   — 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)é    )Ú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      €úe/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/pipelines/depth_estimation.pyr   z DepthEstimationPipeline.__init__2   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.
            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   Ú__call__)r   r   r   r   s      €r   r$   z DepthEstimationPipeline.__call__7   sB   ø€ ðD �vÑØ—Z‘Z Ó)ˆ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_parameters_   sG   € ØÐØÐØ+2Ð˜iÑ(Ü�j¤$Ô'¨I¸Ñ,CØ+5°iÑ+@Ð˜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.preprocessg   sd   € Ü˜5 'Ó*ˆØ×+Ñ+°5ÈÏÉÐ+ÓXˆØ�>‰>˜TÒ!Ø'Ÿ?™?¨4×+;Ñ+;Ó<ˆLØ&+§j¡j±°2°Ñ&6ˆ�]Ñ#ØÐr   c                 óV   — |j                  d«      } | j                  di |¤Ž}||d<   |S )Nr0   © )r"   Úmodel)r   r7   r0   Úmodel_outputss       r   Ú_forwardz DepthEstimationPipeline._forwardo   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 )Nr0   Úpredicted_depthéÿ   Úuint8)r?   Údepthr   r   )r1   Úpost_process_depth_estimationÚdetachÚcpuÚnumpyÚminÚmaxr   Ú	fromarrayÚastypeÚappendÚlen)r   r<   ÚoutputsÚformatted_outputsÚoutputrB   s         r   Úpostprocessz#DepthEstimationPipeline.postprocessu   sã   € Ø×&Ñ&×DÑDØð ˜=Ñ)Ð*ó	
ˆð ÐØò 	eˆFØÐ,Ñ-×4Ñ4Ó6×:Ñ:Ó<×BÑBÓDˆEØ˜UŸY™Y›[Ñ(¨U¯Y©Y«[¸5¿9¹9»;Ñ-FÑGˆEÜ—O‘O U¨S¡[×$8Ñ$8¸Ó$AÓBˆEà×$Ñ$¸Ð@QÑ9RÐ]bÑ%cÕdð	eô (+¨7£|°qÒ'8Ð  Ñ#ÐOÐ>OÐOr   )N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   r$   r+   r8   r=   rP   Ú__classcell__)r   s   @r   r   r      sG   ø„ ñô0Hñ
&2˜u S¨$¨s©)°]ÀDÈÑDWÐ%WÑXõ &2óP)óòöPr   r   N)Útypingr   r   Úutilsr   r   r   r	   r
   Úbaser   r   ÚPILr   Úimage_utilsr   Úmodels.auto.modeling_autor   Ú
get_loggerrQ   Úloggerr   r:   r   r   ú<module>r_      sm   ðß ÷õ ÷ 5ñ ÔÝå(áÔÝTà	ˆ×	Ñ	˜HÓ	%€ñ Ñ,ÀÔFÓGôlP˜hó lPó HñlPr   