§
    ‚Štjš   ã                   ó  — d dl mZ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  e¦   «         rd dlZddlmZmZ erd d	lmZ  e	j        e¦  «        Z e ed
¬¦  «        ¦  «         G d„ de¦  «        ¦   «         ZdS )é    )ÚTYPE_CHECKINGÚAnyÚUnionÚoverloadé   )Úadd_end_docstringsÚis_torch_availableÚis_vision_availableÚloggingÚrequires_backendsé   )ÚPipelineÚbuild_pipeline_init_args)Ú
load_imageN)Ú(MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMESÚ,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES)ÚImageT)Úhas_image_processorc                   óŠ  ‡ — e Zd ZdZdZdZdZdZˆ fd„Zd„ Z	e
deedf         d	ed
edeeeef                  fd„¦   «         Ze
dee         ed         z  d	ed
edeeeeef                           fd„¦   «         Zdeeeef                  eeeeef                           z  fˆ fd„Zdd„Zd„ Zdd„Zdddeeef         fd„Zˆ xZS )ÚObjectDetectionPipelineaò  
    Object detection pipeline using any `AutoModelForObjectDetection`. This pipeline predicts bounding boxes of objects
    and their classes.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> detector = pipeline(model="facebook/detr-resnet-50")
    >>> detector("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
    [{'score': 0.997, 'label': 'bird', 'box': {'xmin': 69, 'ymin': 171, 'xmax': 396, 'ymax': 507}}, {'score': 0.999, 'label': 'bird', 'box': {'xmin': 398, 'ymin': 105, 'xmax': 767, 'ymax': 507}}]

    >>> # x, y  are expressed relative to the top left hand corner.
    ```

    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:
    `"object-detection"`.

    See the list of available models on [huggingface.co/models](https://huggingface.co/models?filter=object-detection).
    FTNc                 óÞ   •—  t          ¦   «         j        |i |¤Ž t          | d¦  «         t          j        ¦   «         }|                     t          ¦  «         |                      |¦  «         d S )NÚvision)ÚsuperÚ__init__r   r   ÚcopyÚupdater   Úcheck_model_type)ÚselfÚargsÚkwargsÚmappingÚ	__class__s       €úe/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/pipelines/object_detection.pyr   z ObjectDetectionPipeline.__init__8   sj   ø€ Ø�‰ŒÔ˜$Ð) &Ð)Ð)Ð)å˜$ Ñ)Ô)Ð)Ý:Ô?ÑAÔAˆØ�ŠÕCÑDÔDÐDØ×Ò˜gÑ&Ô&Ð&Ð&Ð&ó    c                 óP   — i }d|v r|d         |d<   i }d|v r|d         |d<   |i |fS )NÚtimeoutÚ	threshold© )r   r    Úpreprocess_paramsÚpostprocess_kwargss       r#   Ú_sanitize_parametersz,ObjectDetectionPipeline._sanitize_parameters@   sR   € ØÐØ˜ÐÐØ+1°)Ô+<Ð˜iÑ(ØÐØ˜&Ð Ð Ø.4°[Ô.AÐ˜{Ñ+Ø  "Ð&8Ð8Ð8r$   ÚimagezImage.Imager   r    Úreturnc                 ó   — d S ©Nr(   ©r   r,   r   r    s       r#   Ú__call__z ObjectDetectionPipeline.__call__I   s   € ØmpÐmpr$   c                 ó   — d S r/   r(   r0   s       r#   r1   z ObjectDetectionPipeline.__call__L   s	   € ð &) Sr$   c                 óv   •— d|v rd|vr|                      d¦  «        |d<    t          ¦   «         j        |i |¤ŽS )ai  
        Detect objects (bounding boxes & classes) in 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 an HTTP(S) 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. Images in a batch must all be in the
                same format: all as HTTP(S) links, all as local paths, or all as PIL images.
            threshold (`float`, *optional*, defaults to 0.5):
                The probability necessary to make a prediction.
            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 dictionaries or a list of list of dictionaries containing the result. If the input is a single
            image, will return a list of dictionaries, if the input is a list of several images, will return a list of
            list of dictionaries corresponding to each image.

            The dictionaries contain the following keys:

            - **label** (`str`) -- The class label identified by the model.
            - **score** (`float`) -- The score attributed by the model for that label.
            - **box** (`list[dict[str, int]]`) -- The bounding box of detected object in image's original size.
        ÚimagesÚinputs)Úpopr   r1   )r   r   r    r"   s      €r#   r1   z ObjectDetectionPipeline.__call__Q   sM   ø€ ð@ �vÐÐ (°&Ð"8Ð"8Ø%Ÿzšz¨(Ñ3Ô3ˆF�8ÑØ�u‰wŒwÔ Ð0¨Ð0Ð0Ð0r$   c                 ó.  — t          ||¬¦  «        }t          j        |j        |j        gg¦  «        }|                      |gd¬¦  «        }|                     | j        ¦  «        }| j        �$|                      |d         |d         d¬¦  «        }||d<   |S )N)r&   Úpt)r4   Úreturn_tensorsÚwordsÚboxes)Útextr;   r9   Útarget_size)	r   ÚtorchÚ	IntTensorÚheightÚwidthÚimage_processorÚtoÚdtypeÚ	tokenizer)r   r,   r&   r=   r5   s        r#   Ú
preprocessz"ObjectDetectionPipeline.preprocessu   s“   € Ý˜5¨'Ð2Ñ2Ô2ˆÝ”o¨¬°e´kÐ'BÐ&CÑDÔDˆØ×%Ò%¨e¨WÀTÐ%ÑJÔJˆØ—’˜4œ:Ñ&Ô&ˆØŒ>Ð%Ø—^’^¨°¬ÀÀwÄÐ`d�^ÑeÔeˆFØ +ˆˆ}ÑØˆr$   c                 ó    — |                      d¦  «        } | j        di |¤Ž}|                     d|i|¥¦  «        }| j        �|d         |d<   |S )Nr=   Úbboxr(   )r6   Úmodelr"   rE   )r   Úmodel_inputsr=   ÚoutputsÚmodel_outputss        r#   Ú_forwardz ObjectDetectionPipeline._forward   sf   € Ø"×&Ò& }Ñ5Ô5ˆØ�$”*Ð,Ð,˜|Ð,Ð,ˆØ×)Ò)¨=¸+Ð*QÈÐ*QÑRÔRˆØŒ>Ð%Ø$0°Ô$8ˆM˜&Ñ!ØÐr$   ç      à?c                 óL  ‡ ‡‡‡‡‡— |d         }‰ j         �æ|d                              ¦   «         \  ŠŠˆˆ ˆfd„Š|d                              d¦  «                             d¬¦  «                             d¬¦  «        \  }}ˆ fd„|                     ¦   «         D ¦   «         }ˆfd„|d	                              d¦  «        D ¦   «         }g d
¢Šˆˆfd„t          |                     ¦   «         ||¦  «        D ¦   «         }n¨‰ j                             |‰|¦  «        }	|	d         }
|
d         }|
d         }|
d         }|                     ¦   «         |
d<   ˆ fd„|D ¦   «         |
d<   ˆ fd„|D ¦   «         |
d<   g d
¢Šˆfd„t          |
d         |
d         |
d         ¦  «        D ¦   «         }|S )Nr=   r   c           
      óº   •— ‰                      t          j        ‰| d         z  dz  ‰| d         z  dz  ‰| d         z  dz  ‰| d         z  dz  g¦  «        ¦  «        S )Nr   iè  r   r   é   )Ú_get_bounding_boxr>   ÚTensor)rH   r@   r   rA   s    €€€r#   Úunnormalizez8ObjectDetectionPipeline.postprocess.<locals>.unnormalizeŽ   sq   ø€ Ø×-Ò-Ý”Là" T¨!¤W™_¨tÑ3Ø# d¨1¤gÑ-°Ñ4Ø" T¨!¤W™_¨tÑ3Ø# d¨1¤gÑ-°Ñ4ð	ñô ñ	ô 	ð 	r$   Úlogitséÿÿÿÿ)Údimc                 ó>   •— g | ]}‰j         j        j        |         ‘ŒS r(   )rI   ÚconfigÚid2label)Ú.0Ú
predictionr   s     €r#   ú
<listcomp>z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>›   s&   ø€ Ð`Ð`Ð`À�d”jÔ'Ô0°Ô<Ð`Ð`Ð`r$   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r(   r(   )r[   rH   rT   s     €r#   r]   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>œ   s#   ø€ ÐTÐTÐT¨4�[�[ Ñ&Ô&ÐTÐTÐTr$   rH   )ÚscoreÚlabelÚboxc                 ób   •— g | ]+}|d          ‰k    ¯t          t          ‰|¦  «        ¦  «        ‘Œ,S )r   ©ÚdictÚzip)r[   ÚvalsÚkeysr'   s     €€r#   r]   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>ž   s;   ø€ ÐwÐwÐw°DÐcgÐhiÔcjÐmvÒcvÐcv�$�s 4¨™œÑ/Ô/ÐcvÐcvÐcvr$   ÚscoresÚlabelsr;   c                 ób   •— g | ]+}‰j         j        j        |                     ¦   «                  ‘Œ,S r(   )rI   rY   rZ   Úitem)r[   r`   r   s     €r#   r]   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>¨   s/   ø€ Ð'eÐ'eÐ'eÐUZ¨¬
Ô(9Ô(BÀ5Ç:Â:Á<Ä<Ô(PÐ'eÐ'eÐ'er$   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r(   )rR   )r[   ra   r   s     €r#   r]   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>©   s'   ø€ Ð&TÐ&TÐ&TÀs t×'=Ò'=¸cÑ'BÔ'BÐ&TÐ&TÐ&Tr$   c                 óJ   •— g | ]}t          t          ‰|¦  «        ¦  «        ‘Œ S r(   rc   )r[   rf   rg   s     €r#   r]   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>­   s9   ø€ ð ð ð àõ •S˜˜t‘_”_Ñ%Ô%ðð ð r$   )rE   ÚtolistÚsqueezeÚsoftmaxÚmaxre   rB   Úpost_process_object_detection)r   rL   r'   r=   rh   Úclassesri   r;   Ú
annotationÚraw_annotationsÚraw_annotationr@   rg   rT   rA   s   ` `        @@@@r#   Úpostprocessz#ObjectDetectionPipeline.postprocess‡   s  øøøøøø€ Ø# MÔ2ˆØŒ>Ð%ð (¨œN×1Ò1Ñ3Ô3‰MˆF�Eð
ð 
ð 
ð 
ð 
ð 
ð 
ð ,¨HÔ5×=Ò=¸aÑ@Ô@×HÒHÈRÐHÑPÔP×TÒTÐY[ÐTÑ\Ô\‰OˆF�GØ`Ð`Ð`Ð`ÈwÏ~Ê~ÑO_ÔO_Ð`Ñ`Ô`ˆFØTÐTÐTÐT°=ÀÔ3H×3PÒ3PÐQRÑ3SÔ3SÐTÑTÔTˆEØ,Ð,Ð,ˆDØwÐwÐwÐwÐw½CÀÇÂÁÄÐQWÐY^Ñ<_Ô<_ÐwÑwÔwˆJˆJð #Ô2×PÒPÐQ^Ð`iÐkvÑwÔwˆOØ,¨QÔ/ˆNØ# HÔ-ˆFØ# HÔ-ˆFØ" 7Ô+ˆEà'-§}¢}¡¤ˆN˜8Ñ$Ø'eÐ'eÐ'eÐ'eÐ^dÐ'eÑ'eÔ'eˆN˜8Ñ$Ø&TÐ&TÐ&TÐ&TÈeÐ&TÑ&TÔ&TˆN˜7Ñ#ð -Ð,Ð,ˆDðð ð ð å ¨xÔ 8¸.ÈÔ:RÐTbÐcjÔTkÑlÔlðñ ô ˆJð
 Ðr$   ra   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)Úintrn   )r   ra   ry   rz   r{   r|   rH   s          r#   rR   z)ObjectDetectionPipeline._get_bounding_box´   sE   € ð "%§¢¡¤×!1Ò!1Ñ!3Ô!3Ñˆˆd�D˜$àØØØð	
ð 
ˆð ˆr$   r/   )rN   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r+   r   r   Ústrr   Úlistrd   r1   rF   rM   rw   r}   rR   Ú__classcell__)r"   s   @r#   r   r      s¿  ø€ € € € € ðð ð0 €OØ ÐØ#ÐØ€Oð'ð 'ð 'ð 'ð 'ð9ð 9ð 9ð Øp˜e C¨Ð$6Ô7ÐpÀÐpÈsÐpÐW[Ð\`ÐadÐfiÐaiÔ\jÔWkÐpÐpÐpñ „XØpàð)Ø˜#”Y  mÔ!4Ñ4ð)Ø=@ð)ØLOð)à	ˆd�4˜˜S˜”>Ô"Ô	#ð)ð )ð )ñ „Xð)ð"1¨4°°S¸#°X´Ô+?À$ÀtÈDÐQTÐVYÐQYÌNÔG[ÔB\Ñ+\ð "1ð "1ð "1ð "1ð "1ð "1ðHð ð ð ðð ð ð+ð +ð +ð +ðZ ^ð ¸¸SÀ#¸X¼ð ð ð ð ð ð ð ð r$   r   )Útypingr   r   r   r   Úutilsr   r	   r
   r   r   Úbaser   r   Úimage_utilsr   r>   Úmodels.auto.modeling_autor   r   ÚPILr   Ú
get_loggerr~   Úloggerr   r(   r$   r#   ú<module>r‘      sh  ðØ 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6à kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kÐ kØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4ð ÐÑÔð )Ø(Ð(Ð(Ð(Ð(Ð(ð ÐÑÔð Ø€L€L€Lðð ð ð ð ð ð ð ð
 ð ØÐÐÐÐÐà	ˆÔ	˜HÑ	%Ô	%€ð ÐÐ,Ð,ÀÐFÑFÔFÑGÔGðkð kð kð kð k˜hñ kô kñ HÔGðkð kð kr$   