Ë
    îÍ:j  ã                   óâ   — 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      €ú|/home/mcse/projects/srt_converter/srt-converter-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ÑØ—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_parametersl   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ÚdtypeÚsize)r   Úimager-   Úmodel_inputss       r   Ú
preprocessz"DepthEstimationPipeline.preprocesst   sb   € Ü˜5 'Ó*ˆØ×+Ñ+°5ÈÏÉÐ+ÓXˆØ�>‰>˜TÒ!Ø'Ÿ?™?¨4¯:©:Ó6ˆLØ&+§j¡j±°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Ó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   )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ð
 Ø_˜u S¨-Ð%7Ñ8Ð_ÀCÐ_ÈDÐQTÐVYÐQYÉNÒ_ó Ø_àØk˜t E¨#¨}Ð*<Ñ$=Ñ>ÐkÈ#ÐkÐRVÐW[Ð\_ÐadÐ\dÑWeÑRfÒkó Økð(2Ø˜C  c¡¨M¸4ÀÑ;NÐNÑOð(2Ø[^ð(2à	ˆt�C˜�H‰~˜t D¨¨c¨¡NÑ3Ð3Ñ	4õ(2óT)óòö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   