ó
    ‰Ž¡jš   ã                   ó  • S SK JrJrJrJr  SSKJrJrJrJ	r	J
r
  SSKJrJr  \" 5       (       a  SSKJr  \" 5       (       a  S SKrSSKJrJr  \(       a  S S	KJr  \	R,                  " \5      r\" \" S
S95       " S S\5      5       rg)é    )Ú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                   óB  ^ • \ rS rSrSrSrSrSrSrU 4S jr	S r
\S\\S	4   S
\S\S\\\\4      4S j5       r\S\\   \S	   -  S
\S\S\\\\\4         4S j5       rS\\\\4      \\\\\4         -  4U 4S jjrSS jrS rSS jrSSS\\\4   4S jrSrU =r$ )ÚObjectDetectionPipelineé   a¶  
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U ]  " U0 UD6  [        U S5        [        R                  " 5       nUR                  [        5        U R                  U5        g )NÚvision)ÚsuperÚ__init__r   r   ÚcopyÚupdater   Úcheck_model_type)ÚselfÚargsÚkwargsÚmappingÚ	__class__s       €ÚµC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/pipelines/object_detection.pyr   Ú ObjectDetectionPipeline.__init__8   sH   ø€ Ü‰Ò˜$Ð) &Ò)ä˜$ Ô)Ü:×?Ò?ÓAˆØ�‰ÔCÔDØ×Ñ˜gÕ&ó    c                 óL   • 0 nSU;   a  US   US'   0 nSU;   a  US   US'   U0 U4$ )NÚtimeoutÚ	threshold© )r   r!   Úpreprocess_paramsÚpostprocess_kwargss       r$   Ú_sanitize_parametersÚ,ObjectDetectionPipeline._sanitize_parameters@   sL   € ØÐØ˜ÓØ+1°)Ñ+<Ð˜iÑ(ØÐØ˜&Ó Ø.4°[Ñ.AÐ˜{Ñ+Ø  "Ð&8Ð8Ð8r&   ÚimagezImage.Imager    r!   Úreturnc                 ó   • g ©Nr*   ©r   r/   r    r!   s       r$   Ú__call__Ú ObjectDetectionPipeline.__call__I   s   € Ømpr&   c                 ó   • g r2   r*   r3   s       r$   r4   r5   L   s   € ð &)r&   c                 ód   >• SU;   a  SU;  a  UR                  S5      US'   [        TU ]  " U0 UD6$ )a±  
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   r5   Q   s=   ø€ ð@ �vÓ (°&Ó"8Ø%Ÿz™z¨(Ó3ˆF�8ÑÜ‰wÒ Ð0¨Ñ0Ð0r&   c                 ó  • [        XS9n[        R                  " UR                  UR                  //5      nU R                  U/SS9nUR                  U R                  5      nU R                  b  U R                  US   US   SS9nX4S'   U$ )N)r(   Úpt)r8   Úreturn_tensorsÚwordsÚboxes)Útextr?   r=   Útarget_size)	r   ÚtorchÚ	IntTensorÚheightÚwidthÚimage_processorÚtoÚdtypeÚ	tokenizer)r   r/   r(   rA   r9   s        r$   Ú
preprocessÚ"ObjectDetectionPipeline.preprocessu   s‡   € Ü˜5Ñ2ˆÜ—o’o¨¯©°e·k±kÐ'BÐ&CÓDˆØ×%Ñ%¨e¨WÀTÐ%ÐJˆØ—‘˜4Ÿ:™:Ó&ˆØ�>‰>Ñ%Ø—^‘^¨°©ÀÀwÁÐ`d�^ÐeˆFØ +ˆ}ÑØˆr&   c                 ó    • UR                  S5      nU R                  " S0 UD6nUR                  SU0UE5      nU R                  b  US   US'   U$ )NrA   Úbboxr*   )r:   Úmodelr#   rI   )r   Úmodel_inputsrA   ÚoutputsÚmodel_outputss        r$   Ú_forwardÚ ObjectDetectionPipeline._forward   s\   € Ø"×&Ñ& }Ó5ˆØ—*’*Ñ,˜|Ñ,ˆØ×)Ñ)¨=¸+Ð*QÈÐ*QÓRˆØ�>‰>Ñ%Ø$0°Ñ$8ˆM˜&Ñ!ØÐr&   c           	      óH  ^ ^^• US   nT R                   Gb  US   R                  5       u  mmUU U4S jnUS   R                  S5      R                  SS9R	                  SS9u  pVUR                  5        Vs/ s H&  nT R
                  R                  R                  U   PM(     nnUS   R                  S5       V	s/ s H
  o”" U	5      PM     n
n	/ SQn[        UR                  5       XŠ5       Vs/ s H!  oÌS   U:”  d  M  [        [        X¼5      5      PM#     nnU$ T R                  R                  XU5      nUS   nUS	   nUS
   nUS   n
UR                  5       US	'   U Vs/ s H4  nT R
                  R                  R                  UR                  5          PM6     snUS
'   U
 Vs/ s H  nT R                  U5      PM     snUS'   / SQn[        US	   US
   US   5       Vs/ s H  n[        [        X¼5      5      PM     nnU$ s  snf s  sn	f s  snf s  snf s  snf s  snf )NrA   r   c           
      óž   >• TR                  [        R                  " TU S   -  S-  TU S   -  S-  TU S   -  S-  TU S   -  S-  /5      5      $ )Nr   iè  r   r   é   )Ú_get_bounding_boxrB   ÚTensor)rM   rD   r   rE   s    €€€r$   ÚunnormalizeÚ8ObjectDetectionPipeline.postprocess.<locals>.unnormalizeŽ   si   ø€ Ø×-Ñ-Ü—L’Là" T¨!¡W™_¨tÑ3Ø# d¨1¡gÑ-°Ñ4Ø" T¨!¡W™_¨tÑ3Ø# d¨1¡gÑ-°Ñ4ð	óó	ð 	r&   Úlogitséÿÿÿÿ)ÚdimrM   )ÚscoreÚlabelÚboxÚscoresÚlabelsr?   )rI   ÚtolistÚsqueezeÚsoftmaxÚmaxrN   ÚconfigÚid2labelÚzipÚdictrF   Úpost_process_object_detectionÚitemrW   )r   rQ   r)   rA   rY   ra   ÚclassesÚ
predictionrb   rM   r?   ÚkeysÚvalsÚ
annotationÚraw_annotationsÚraw_annotationr_   r`   rD   rE   s   `                 @@r$   ÚpostprocessÚ#ObjectDetectionPipeline.postprocess‡   s.  ú€ Ø# MÑ2ˆØ�>‰>Ò%ð (¨™N×1Ñ1Ó3‰MˆF�E÷
ð ,¨HÑ5×=Ñ=¸aÓ@×HÑHÈRÐHÐP×TÑTÐY[ÐTÐ\‰OˆFØOVÏ~É~ÔO_Ó`ÒO_À�d—j‘j×'Ñ'×0Ñ0°Ô<ÑO_ˆFÐ`Ø3@ÀÑ3H×3PÑ3PÐQRÔ3SÓTÒ3S¨4�[ Ö&Ñ3SˆEÐTÚ,ˆDÜ<?ÀÇÁÃÐQWÔ<_ÓwÒ<_°DÐhiÑcjÐmvÑcvÓ/œ$œs 4›Ö/Ñ<_ˆJÐwð( Ðð# #×2Ñ2×PÑPÐQ^ÐkvÓwˆOØ,¨QÑ/ˆNØ# HÑ-ˆFØ# HÑ-ˆFØ" 7Ñ+ˆEà'-§}¡}£ˆN˜8Ñ$Ù^dÓ'eÒ^dÐUZ¨¯
©
×(9Ñ(9×(BÑ(BÀ5Ç:Á:Ã<Ô(PÑ^dÑ'eˆN˜8Ñ$ÙNSÓ&TÊeÀs t×'=Ñ'=¸cÖ'BÉeÑ&TˆN˜7Ñ#ò -ˆDô   ¨xÑ 8¸.ÈÑ:RÐTbÐcjÑTkÔlóâl�Dô ”S˜“_Ö%Ùlð ð ð
 Ðùò/ aùÚTùâwùò (fùÚ&Tùòs*   Á7-HÂ<HÃ/HÃ?HÅ";HÆ'HÇ$Hr`   ztorch.Tensorc                 óV   • UR                  5       R                  5       u  p#pEUUUUS.nU$ )zõ
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)Úintrc   )r   r`   rw   rx   ry   rz   rM   s          r$   rW   Ú)ObjectDetectionPipeline._get_bounding_box´   s7   € ð "%§¡£×!1Ñ!1Ó!3Ñˆ�DàØØØñ	
ˆð ˆr&   r*   r2   )g      à?)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r-   r   r   Ústrr   Úlistrj   r4   rJ   rR   rt   r{   rW   Ú__static_attributes__Ú__classcell__)r#   s   @r$   r   r      s!  ø† ñð0 €OØ ÐØ#ÐØ€Oõ'ò9ð Øp˜e C¨Ð$6Ñ7ÐpÀÐpÈsÐpÐW[Ð\`ÐadÐfiÐaiÑ\jÑWkÓpó Øpàð)Ø˜#‘Y  mÑ!4Ñ4ð)Ø=@ð)ØLOð)à	ˆd�4˜˜S˜‘>Ñ"Ñ	#ó)ó ð)ð"1¨4°°S¸#°X±Ñ+?À$ÀtÈDÐQTÐVYÐQYÉNÑG[ÑB\Ñ+\÷ "1ôHòô+ðZ ^ð ¸¸SÀ#¸X¹÷ ò r&   r   )Útypingr   r   r   r   Úutilsr   r	   r
   r   r   Úbaser   r   Úimage_utilsr   rB   Úmodels.auto.modeling_autor   r   ÚPILr   Ú
get_loggerr}   Úloggerr   r*   r&   r$   Ú<module>r’      sw   ðß 6Ó 6ç kÕ kß 4ñ ×ÑÝ(ñ ×ÑÛ÷ö
 Ýà	×	Ò	˜HÓ	%€ñ Ñ,ÀÑFÓGôk˜hó kó Hñkr&   