§
    9²¸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mZ  e¦   «         rd dlZd d	lmZ dd
lmZ  ej        e¦  «        Z e ed¬¦  «        ¦  «         G d„ de¦  «        ¦   «         ZdS )é    )ÚAnyÚUnionÚoverloadé   )Úadd_end_docstringsÚis_torch_availableÚis_vision_availableÚloggingÚrequires_backendsé   )ÚChunkPipelineÚbuild_pipeline_init_args)ÚImage)Ú
load_imageÚvalid_imagesN)ÚBaseModelOutput)Ú2MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_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ee         z  ded	eeeef                  fd
„¦   «         Ze	deeeef                  ded	eeeeef                           fd„¦   «         Z	 dde
edeeeef                  f         deee         z  dz  ded	eeeef                  eeeeef                           z  fˆ fd„Zd„ Zdd„Zd„ Zdd„Zddd	eeef         fd„Zˆ xZS )ÚZeroShotObjectDetectionPipelineaª  
    Zero shot object detection pipeline using `OwlViTForObjectDetection`. This pipeline predicts bounding boxes of
    objects when you provide an image and a set of `candidate_labels`.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> detector = pipeline(model="google/owlvit-base-patch32", task="zero-shot-object-detection")
    >>> detector(
    ...     "http://images.cocodataset.org/val2017/000000039769.jpg",
    ...     candidate_labels=["cat", "couch"],
    ... )
    [{'score': 0.287, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}}, {'score': 0.254, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}}, {'score': 0.121, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}}]

    >>> detector(
    ...     "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png",
    ...     candidate_labels=["head", "bird"],
    ... )
    [{'score': 0.119, 'label': 'bird', 'box': {'xmin': 71, 'ymin': 170, 'xmax': 410, 'ymax': 508}}]
    ```

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

    This object detection pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"zero-shot-object-detection"`.

    See the list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=zero-shot-object-detection).
    FTc                 óŽ   •—  t          ¦   «         j        di |¤Ž t          | d¦  «         |                      t          ¦  «         d S )NÚvision© )ÚsuperÚ__init__r   Úcheck_model_typer   )ÚselfÚkwargsÚ	__class__s     €ú�/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/pipelines/zero_shot_object_detection.pyr   z(ZeroShotObjectDetectionPipeline.__init__=   sK   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"å˜$ Ñ)Ô)Ð)Ø×ÒÕPÑQÔQÐQÐQÐQó    ÚimagezImage.ImageÚcandidate_labelsr   Úreturnc                 ó   — d S ©Nr   )r   r"   r#   r   s       r    Ú__call__z(ZeroShotObjectDetectionPipeline.__call__C   s	   € ð  #˜sr!   c                 ó   — d S r&   r   )r   r"   r   s      r    r'   z(ZeroShotObjectDetectionPipeline.__call__H   s   € ØbeÐber!   Nc           	      ó–  •— d|v r|                      d¦  «        }t          |t          t          j        f¦  «        r||dœ}nmt          |t          t
          f¦  «        rNt          |¦  «        r?t	           t          ¦   «         j        d„ t          ||¦  «        D ¦   «         fi |¤Ž¦  «        S 	 |} t          ¦   «         j        |fi |¤Ž}|S )a|  
        Detect objects (bounding boxes & classes) in the image(s) passed as inputs.

        Args:
            image (`str`, `PIL.Image` or `list[dict[str, Any]]`):
                The pipeline handles three types of images:

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

                You can use this parameter to send directly a list of images, or a dataset or a generator like so:

                ```python
                >>> from transformers import pipeline

                >>> detector = pipeline(model="google/owlvit-base-patch32", task="zero-shot-object-detection")
                >>> detector(
                ...     [
                ...         {
                ...             "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                ...             "candidate_labels": ["cat", "couch"],
                ...         },
                ...         {
                ...             "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                ...             "candidate_labels": ["cat", "couch"],
                ...         },
                ...     ]
                ... )
                [[{'score': 0.287, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}}, {'score': 0.25, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}}, {'score': 0.121, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}}], [{'score': 0.287, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}}, {'score': 0.254, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}}, {'score': 0.121, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}}]]
                ```


            candidate_labels (`str` or `list[str]` or `list[list[str]]`):
                What the model should recognize in the image.

            threshold (`float`, *optional*, defaults to 0.1):
                The probability necessary to make a prediction.

            top_k (`int`, *optional*, defaults to None):
                The number of top predictions that will be returned by the pipeline. If the provided number is `None`
                or higher than the number of predictions available, it will default to the number of predictions.

            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 list of lists containing prediction results, one list per input image. Each list contains dictionaries
            with the following keys:

            - **label** (`str`) -- Text query corresponding to the found object.
            - **score** (`float`) -- Score corresponding to the object (between 0 and 1).
            - **box** (`dict[str,int]`) -- Bounding box of the detected object in image's original size. It is a
              dictionary with `x_min`, `x_max`, `y_min`, `y_max` keys.
        Útext_queries©r"   r#   c              3   ó&   K  — | ]\  }}||d œV — ŒdS )r+   Nr   )Ú.0ÚimgÚlabelss      r    ú	<genexpr>z;ZeroShotObjectDetectionPipeline.__call__.<locals>.<genexpr>’   s.   è è € ÐpÐpÁKÀCÈ˜s¸Ð?Ð?ÐpÐpÐpÐpÐpÐpr!   )
ÚpopÚ
isinstanceÚstrr   ÚlistÚtupler   r   r'   Úzip)r   r"   r#   r   ÚinputsÚresultsr   s         €r    r'   z(ZeroShotObjectDetectionPipeline.__call__K   só   ø€ ð~ ˜VÐ#Ð#Ø%Ÿzšz¨.Ñ9Ô9Ðå�e�c¥5¤;Ð/Ñ0Ô0ð 	Ø$Ð:JÐKÐKˆFˆFÝ˜¥¥e˜}Ñ-Ô-ð 	µ,¸uÑ2EÔ2Eð 	ÝØ •‘”Ô ØpÐpÕSVÐW\Ð^nÑSoÔSoÐpÑpÔpðð àðð ñô ð ðð ˆFà"•%‘'”'Ô" 6Ð4Ð4¨VÐ4Ð4ˆØˆr!   c                 ón   — i }d|v r|d         |d<   i }d|v r|d         |d<   d|v r|d         |d<   |i |fS )NÚtimeoutÚ	thresholdÚtop_kr   )r   r   Úpreprocess_paramsÚpostprocess_paramss       r    Ú_sanitize_parametersz4ZeroShotObjectDetectionPipeline._sanitize_parameters£   sl   € ØÐØ˜ÐÐØ+1°)Ô+<Ð˜iÑ(ØÐØ˜&Ð Ð Ø.4°[Ô.AÐ˜{Ñ+Ø�fÐÐØ*0°¬/Ð˜wÑ'Ø  "Ð&8Ð8Ð8r!   c              #   óð  K  — t          |d         |¬¦  «        }|d         }t          |t          ¦  «        r|                     d¦  «        }t	          j        |j        |j        ggt          j        ¬¦  «        }t          |¦  «        D ]m\  }}|  
                    |d¬¦  «        }|                      |d¬¦  «        }	|	                     | j        ¦  «        }	|t          |¦  «        dz
  k    ||d	œ|¥|	¥V — Œnd S )
Nr"   )r:   r#   ú,)ÚdtypeÚpt)Úreturn_tensorsr   )Úis_lastÚtarget_sizeÚcandidate_label)r   r2   r3   ÚsplitÚtorchÚtensorÚheightÚwidthÚint32Ú	enumerateÚ	tokenizerÚimage_processorÚtorB   Úlen)
r   r7   r:   r"   r#   rF   ÚirG   Útext_inputsÚimage_featuress
             r    Ú
preprocessz*ZeroShotObjectDetectionPipeline.preprocess®   s(  è è € Ý˜6 'œ?°GÐ<Ñ<Ô<ˆØ!Ð"4Ô5ÐÝÐ&­Ñ,Ô,ð 	;Ø/×5Ò5°cÑ:Ô:Ðå”l U¤\°5´;Ð$?Ð#@ÍÌÐTÑTÔTˆÝ"+Ð,<Ñ"=Ô"=ð 
	ð 
	ÑˆAˆØŸ.š.¨È˜.ÑNÔNˆKØ!×1Ò1°%ÈÐ1ÑMÔMˆNØ+×.Ò.¨t¬zÑ:Ô:ˆNà¥Ð$4Ñ 5Ô 5¸Ñ 9Ò9Ø*Ø#2ðð ð ð	ð
 !ðð ð ð ð ð	
	ð 
	r!   c                 ó®   — |                      d¦  «        }|                      d¦  «        }|                      d¦  «        } | j        di |¤Ž}|||dœ|¥}|S )NrF   rG   rE   )rF   rG   rE   r   )r1   Úmodel)r   Úmodel_inputsrF   rG   rE   ÚoutputsÚmodel_outputss          r    Ú_forwardz(ZeroShotObjectDetectionPipeline._forwardÁ   so   € Ø"×&Ò& }Ñ5Ô5ˆØ&×*Ò*Ð+<Ñ=Ô=ˆØ×"Ò" 9Ñ-Ô-ˆà�$”*Ð,Ð,˜|Ð,Ð,ˆà(3ÈÐdkÐwÐwÐovÐwˆØÐr!   çš™™™™™¹?c                 óÐ  — g }|D ]Á}|d         }t          |¦  «        }| j                             |||d         ¬¦  «        d         }|d                              ¦   «         D ]d}|d         |                              ¦   «         }	|                      |d         |         d         ¦  «        }
|	||
dœ}|                     |¦  «         ŒeŒÂt          |d„ d	¬
¦  «        }|r
|d |…         }|S )NrG   rF   )rZ   r;   Útarget_sizesr   ÚscoresÚboxes)ÚscoreÚlabelÚboxc                 ó   — | d         S )Nrb   r   )Úxs    r    ú<lambda>z=ZeroShotObjectDetectionPipeline.postprocess.<locals>.<lambda>Û   s
   € °°'´
€ r!   T)ÚkeyÚreverse)r   rP   Úpost_process_object_detectionÚnonzeroÚitemÚ_get_bounding_boxÚappendÚsorted)r   r[   r;   r<   r8   Úmodel_outputrc   rZ   Úindexrb   rd   Úresults               r    Úpostprocessz+ZeroShotObjectDetectionPipeline.postprocessË   s  € ØˆØ)ð 	'ð 	'ˆLØ Ð!2Ô3ˆEÝ*¨<Ñ8Ô8ˆLØÔ*×HÒHØ$°	ÈÐUbÔHcð Iñ ô àôˆGð ! Ô*×2Ò2Ñ4Ô4ð 'ð '�Ø Ô)¨%Ô0×5Ò5Ñ7Ô7�Ø×,Ò,¨W°WÔ-=¸eÔ-DÀQÔ-GÑHÔH�à#(°5ÀÐEÐE�Ø—’˜vÑ&Ô&Ð&Ð&ð'õ ˜Ð&:Ð&:ÀDÐIÑIÔIˆØð 	&Ø˜f˜u˜f”oˆGàˆr!   rd   ztorch.Tensorc                 ój   — |                      ¦   «                              ¦   «         \  }}}}||||dœ}|S )a%  
        Turns list [xmin, xmax, ymin, ymax] into dict { "xmin": xmin, ... }

        Args:
            box (`torch.Tensor`): Tensor containing the coordinates in corners format.

        Returns:
            bbox (`dict[str, int]`): Dict containing the coordinates in corners format.
        )ÚxminÚyminÚxmaxÚymax)ÚintÚtolist)r   rd   ru   rv   rw   rx   Úbboxs          r    rm   z1ZeroShotObjectDetectionPipeline._get_bounding_boxá   sE   € ð "%§¢¡¤×!1Ò!1Ñ!3Ô!3Ñˆˆd�D˜$àØØØð	
ð 
ˆð ˆr!   r&   )r]   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r   r   r3   r4   r   Údictr'   r?   rV   r\   rs   ry   rm   Ú__classcell__)r   s   @r    r   r      s  ø€ € € € € ðð ð@ €OØ ÐØ#ÐØ€OðRð Rð Rð Rð Rð ð#Ø˜3 Ð-Ô.ð#ØBEÈÈSÌ	Á/ð#Ø]`ð#à	ˆd�3˜�8ŒnÔ	ð#ð #ð #ñ „Xð#ð Øe˜d 4¨¨S¨¤>Ô2Ðe¸cÐeÀdÈ4ÐPTÐUXÐZ]ÐU]ÔP^ÔK_ÔF`ÐeÐeÐeñ „XØeð
 48ðVð Và�S˜-¨¨d°3¸°8¬nÔ)=Ð=Ô>ðVð   S¤	™/¨DÑ0ðVð ð	Vð
 
ˆd�3˜�8ŒnÔ	  T¨$¨s°C¨x¬.Ô%9Ô :Ñ	:ðVð Vð Vð Vð Vð Vðp	9ð 	9ð 	9ðð ð ð ð&ð ð ðð ð ð ð, ^ð ¸¸SÀ#¸X¼ð ð ð ð ð ð ð ð r!   r   )Útypingr   r   r   Úutilsr   r   r	   r
   r   Úbaser   r   ÚPILr   Úimage_utilsr   r   rI   Útransformers.modeling_outputsr   Úmodels.auto.modeling_autor   Ú
get_loggerr|   Úloggerr   r   r!   r    ú<module>r�      sY  ðØ 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kØ 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9ð ÐÑÔð 7ØÐÐÐÐÐà6Ð6Ð6Ð6Ð6Ð6Ð6Ð6àÐÑÔð _Ø€L€L€Là=Ð=Ð=Ð=Ð=Ð=à^Ð^Ð^Ð^Ð^Ð^à	ˆÔ	˜HÑ	%Ô	%€ð ÐÐ,Ð,ÀÐFÑFÔFÑGÔGð[ð [ð [ð [ð [ mñ [ô [ñ HÔGð[ð [ð [r!   