§
    Þ˜ºhÂ!  ã                   ó  — 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         ed         f         d	ed
edeeeeef                           fd„¦   «         Zdeeeeef                  eeeeef                           f         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 |¤Ž | j        dk    rt          d| j        › d�¦  «        ‚t          | d¦  «         t          j        ¦   «         }|                     t          ¦  «         |  
                    |¦  «         d S )NÚtfzThe z is only available in PyTorch.Úvision)ÚsuperÚ__init__Ú	frameworkÚ
ValueErrorÚ	__class__r   r   ÚcopyÚupdater   Úcheck_model_type)ÚselfÚargsÚkwargsÚmappingr   s       €úo/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/transformers/pipelines/object_detection.pyr   z ObjectDetectionPipeline.__init__8   s“   ø€ Ø�‰ŒÔ˜$Ð) &Ð)Ð)Ð)àŒ>˜TÒ!Ð!ÝÐR D¤NÐRÐRÐRÑSÔSÐSå˜$ Ñ)Ô)Ð)Ý:Ô?Ñ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_parametersC   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__L   s   € ØmpÐmpr'   c                 ó   — d S r2   r+   r3   s       r&   r4   z ObjectDetectionPipeline.__call__O   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   r4   )r"   r#   r$   r   s      €r&   r4   z ObjectDetectionPipeline.__call__T   sM   ø€ ð@ �vÐÐ (°&Ð"8Ð"8Ø%Ÿzšz¨(Ñ3Ô3ˆF�8ÑØ�u‰wŒwÔ Ð0¨Ð0Ð0Ð0r'   c                 óD  — t          ||¬¦  «        }t          j        |j        |j        gg¦  «        }|                      |gd¬¦  «        }| j        dk    r|                     | j        ¦  «        }| j	        �$|  	                    |d         |d         d¬¦  «        }||d<   |S )N)r)   Úpt)r7   Úreturn_tensorsÚwordsÚboxes)Útextr>   r<   Útarget_size)
r   ÚtorchÚ	IntTensorÚheightÚwidthÚimage_processorr   ÚtoÚdtypeÚ	tokenizer)r"   r/   r)   r@   r8   s        r&   Ú
preprocessz"ObjectDetectionPipeline.preprocessx   s    € Ý˜5¨'Ð2Ñ2Ô2ˆÝ”o¨¬°e´kÐ'BÐ&CÑDÔDˆØ×%Ò%¨e¨WÀTÐ%ÑJÔJˆØŒ>˜TÒ!Ð!Ø—Y’Y˜tœzÑ*Ô*ˆFØŒ>Ð%Ø—^’^¨°¬ÀÀwÄÐ`d�^ÑeÔeˆFØ +ˆˆ}ÑØˆr'   c                 ó    — |                      d¦  «        } | j        di |¤Ž}|                     d|i|¥¦  «        }| j        �|d         |d<   |S )Nr@   Úbboxr+   )r9   Úmodelr   rH   )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_boxrA   ÚTensor)rK   rC   r"   rD   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+   )rL   ÚconfigÚid2label)Ú.0Ú
predictionr"   s     €r&   ú
<listcomp>z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>Ÿ   s&   ø€ Ð`Ð`Ð`À�d”jÔ'Ô0°Ô<Ð`Ð`Ð`r'   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r+   r+   )r^   rK   rW   s     €r&   r`   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>    s#   ø€ ÐTÐTÐT¨4�[�[ Ñ&Ô&ÐTÐTÐTr'   rK   )Ú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+   )rL   r\   r]   Úitem)r^   rc   r"   s     €r&   r`   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>¬   s/   ø€ Ð'eÐ'eÐ'eÐUZ¨¬
Ô(9Ô(BÀ5Ç:Â:Á<Ä<Ô(PÐ'eÐ'eÐ'er'   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS r+   )rU   )r^   rd   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+   rf   )r^   ri   rj   s     €r&   r`   z7ObjectDetectionPipeline.postprocess.<locals>.<listcomp>±   s9   ø€ ð ð ð àõ •S˜˜t‘_”_Ñ%Ô%ðð ð r'   )rH   ÚtolistÚsqueezeÚsoftmaxÚmaxrh   rE   Úpost_process_object_detection)r"   rO   r*   r@   rk   Úclassesrl   r>   Ú
annotationÚraw_annotationsÚraw_annotationrC   rj   rW   rD   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'   rd   ztorch.Tensorc                 óž   — | j         dk    rt          d¦  «        ‚|                     ¦   «                              ¦   «         \  }}}}||||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.
        r;   z9The ObjectDetectionPipeline is only available in PyTorch.)ÚxminÚyminÚxmaxÚymax)r   r   Úintrq   )r"   rd   r|   r}   r~   r   rK   s          r&   rU   z)ObjectDetectionPipeline._get_bounding_box¸   sa   € ð Œ>˜TÒ!Ð!ÝÐXÑYÔYÐYØ!$§¢¡¤×!1Ò!1Ñ!3Ô!3Ñˆˆd�D˜$àØØØð	
ð 
ˆð ˆr'   r2   )rQ   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r.   r   r   Ústrr   Úlistrg   r4   rI   rP   rz   r€   rU   Ú__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àð)Ø˜4 œ9 d¨=Ô&9Ð9Ô:ð)ØCFð)ØRUð)à	ˆd�4˜˜S˜”>Ô"Ô	#ð)ð )ð )ñ „Xð)ð"1¨5°°d¸3À¸8´nÔ1EÀtÈDÐQUÐVYÐ[^ÐV^ÔQ_ÔL`ÔGaÐ1aÔ+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   rA   Ú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ðqð qð qð qð q˜hñ qô qñ HÔGðqð qð qr'   