Ë
    Hêñi�  ã                   ó   — U d dl mZ d dlmZmZmZmZ d dlmZ ddl	m
Z
 ddlmZmZ ddlmZ  e«       rd d	lmZ dd
l	mZ eedef      Zeed<    G d„ de«      Z G d„ de«      Zdedeee      fd„Z G d„ de«      Zy)é    )ÚSequence)ÚAnyÚ	TypeAliasÚ	TypedDictÚUnion)Úoverloadé   )Úis_pil_image)Úis_vision_availableÚrequires_backendsé   )ÚPipeline)ÚImage)Ú
load_imagezImage.ImageÚ	ImagePairc                   ó"   — e Zd ZU eed<   eed<   y)ÚKeypointÚxÚyN)Ú__name__Ú
__module__Ú__qualname__ÚfloatÚ__annotations__© ó    új/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/transformers/pipelines/keypoint_matching.pyr   r   "   s   … ØƒHØ„Hr   r   c                   ó,   — e Zd ZU eed<   eed<   eed<   y)ÚMatchÚkeypoint_image_0Úkeypoint_image_1ÚscoreN)r   r   r   r   r   r   r   r   r   r   r   '   s   … ØÓØÓØ„Lr   r   ÚimagesÚreturnc                 óº   ‡— d}d„ Št        | t        «      r;t        | «      dk(  rt        ˆfd„| D «       «      r| gS t        ˆfd„| D «       «      r| S t	        |«      ‚)N)z-Input images must be a one of the following :z - A pair of images.z - A list of pairs of images.c                 ó<   — t        | «      xs t        | t        «      S )z"images is a PIL Image or a string.)r
   Ú
isinstanceÚstr)Úimages    r   Ú_is_valid_imagez-validate_image_pairs.<locals>._is_valid_image4   s   € ä˜EÓ"Ò<¤j°¼Ó&<Ð<r   r	   c              3   ó.   •K  — | ]  } ‰|«      –— Œ y ­w©Nr   ©Ú.0r)   r*   s     €r   ú	<genexpr>z'validate_image_pairs.<locals>.<genexpr>9   s   øè ø€ Ò#QÀ¡_°U×%;Ñ#Qùó   ƒc              3   óŠ   •K  — | ]:  }t        |t        «      xr$ t        |«      d k(  xr t        ˆfd„|D «       «      –— Œ< y­w)r	   c              3   ó.   •K  — | ]  } ‰|«      –— Œ y ­wr,   r   r-   s     €r   r/   z1validate_image_pairs.<locals>.<genexpr>.<genexpr>>   s   øè ø€ ÒC¨u‘O E×*ÑCùr0   N)r'   r   ÚlenÚall)r.   Ú
image_pairr*   s     €r   r/   z'validate_image_pairs.<locals>.<genexpr>;   sN   øè ø€ ò 
ð ô �z¤8Ó,ò DÜ�J“ 1Ñ$òDäÓC¸
ÔCÓCóDñ
ùs   ƒA A)r'   r   r3   r4   Ú
ValueError)r#   Úerror_messager*   s     @r   Úvalidate_image_pairsr8   -   sd   ø€ ð€Mò=ô �&œ(Ô#Üˆv‹;˜!Ò¤Ó#QÈ&Ô#QÔ QØ�8ˆOÜó 
ð %ô	
ô 
ð ˆMÜ
�]Ó
#Ð#r   c                   ó   ‡ — e Zd ZdZdZdZdZdZˆ fd„Zdd„Z	e
ddededed	ee   fd
„«       Ze
ddee   deded	eee      fd„«       Z	 ddee   ez  deded	ee   eee      z  fˆ fd„Zdd„Zd„ Zdd	ee   fd„Zˆ xZS )ÚKeypointMatchingPipelinez‚
    Keypoint matching pipeline using any `AutoModelForKeypointMatching`. This pipeline matches keypoints between two images.
    FTc                 ó<   •— t        ‰| �  |i |¤Ž t        | d«       y )NÚvision)ÚsuperÚ__init__r   )ÚselfÚargsÚkwargsÚ	__class__s      €r   r>   z!KeypointMatchingPipeline.__init__O   s   ø€ Ü‰Ñ˜$Ð) &Ò)Ü˜$ Õ)r   c                 ó0   — i }|�||d<   i }|�||d<   |i |fS )NÚtimeoutÚ	thresholdr   )r?   rE   rD   Úpreprocess_paramsÚpostprocess_paramss        r   Ú_sanitize_parametersz-KeypointMatchingPipeline._sanitize_parametersS   s>   € ØÐØÐØ+2Ð˜iÑ(ØÐØÐ Ø.7Ð˜{Ñ+Ø  "Ð&8Ð8Ð8r   ÚinputsrE   rA   r$   c                  ó   — y r,   r   ©r?   rI   rE   rA   s       r   Ú__call__z!KeypointMatchingPipeline.__call__\   s   € Øadr   c                  ó   — y r,   r   rK   s       r   rL   z!KeypointMatchingPipeline.__call___   s   € Ømpr   c                 ó‚   •— |€t        d«      ‚t        |«      }t        ‰| �  |fd|i|¤Ž}t	        |«      dk(  r|d   S |S )aø  
        Find matches between keypoints in two images.

        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 pair of images or a batch of image pairs, 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.

            threshold (`float`, *optional*, defaults to 0.0):
                The threshold to use for keypoint matching. Keypoints matched with a lower matching score will be filtered out.
                A value of 0 means that all matched keypoints will be returned.

            kwargs:
                `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:
            Union[list[Match], list[list[Match]]]:
                A list of matches or a list if a single image pair is provided, or of lists of matches if a batch
                of image pairs is provided. Each match is a dictionary containing the following keys:

                - **keypoint_image_0** (`Keypoint`): The keypoint in the first image (x, y coordinates).
                - **keypoint_image_1** (`Keypoint`): The keypoint in the second image (x, y coordinates).
                - **score** (`float`): The matching score between the two keypoints.
        zFCannot call the keypoint-matching pipeline without an inputs argument!rE   r   r   )r6   r8   r=   rL   r3   )r?   rI   rE   rA   Úformatted_inputsÚoutputsrB   s         €r   rL   z!KeypointMatchingPipeline.__call__b   sY   ø€ ðN ˆ>ÜÐeÓfÐfÜ/°Ó7ÐÜ‘'Ñ"Ð#3ÑS¸yÐSÈFÑSˆÜÐÓ  AÒ%Ø˜1‘:ÐØˆr   c                 óæ   — |D �cg c]  }t        ||¬«      ‘Œ }}| j                  |d¬«      }|j                  | j                  «      }|D �cg c]  }|j                  ‘Œ }}||dœ}|S c c}w c c}w )N)rD   Úpt)r#   Úreturn_tensors)Úmodel_inputsÚtarget_sizes)r   Úimage_processorÚtoÚdtypeÚsize)r?   r#   rD   r)   rT   rU   Úpreprocess_outputss          r   Ú
preprocessz#KeypointMatchingPipeline.preprocess‘   sv   € ØBHÖI¸”*˜U¨GÖ4ÐIˆÐIØ×+Ñ+°6È$Ð+ÓOˆØ#—‘ t§z¡zÓ2ˆØ06Ö7 u˜Ÿ
›
Ð7ˆÐ7Ø.:ÈLÑYÐØ!Ð!ùò Jùò 8s   …A)ÁA.c                 óF   — |d   } | j                   di |¤Ž}||d   gdœ}|S )NrT   rU   )Úmodel_outputsrU   r   )Úmodel)r?   rZ   rT   r]   Úforward_outputss        r   Ú_forwardz!KeypointMatchingPipeline._forward™   s9   € Ø)¨.Ñ9ˆØ"˜Ÿ
™
Ñ2 \Ñ2ˆØ,9ÐL^Ð_mÑLnÐKoÑpˆØÐr   c           	      óÒ  — |d   }|d   }| j                   j                  |||¬«      }|d   }g }t        |d   |d   |d   «      D ]�  \  }}}	t        |d   j	                  «       |d   j	                  «       ¬	«      }t        |d   j	                  «       |d   j	                  «       ¬	«      }|j                  t        |||	j	                  «       ¬
«      «       Œ‘ t        |d„ d¬«      }|S )Nr]   rU   )rU   rE   r   Ú
keypoints0Ú
keypoints1Úmatching_scoresr   )r   r   )r    r!   r"   c                 ó   — | d   S )Nr"   r   )r   s    r   ú<lambda>z6KeypointMatchingPipeline.postprocess.<locals>.<lambda>¯   s
   € ¸¸'¹
€ r   T)ÚkeyÚreverse)rV   Úpost_process_keypoint_matchingÚzipr   ÚitemÚappendr   Úsorted)
r?   r_   rE   r]   rU   Úpostprocess_outputsÚpair_resultÚkp_0Úkp_1r"   s
             r   Úpostprocessz$KeypointMatchingPipeline.postprocessŸ   sü   € Ø'¨Ñ8ˆØ& ~Ñ6ˆØ"×2Ñ2×QÑQØ¨À	ð Ró 
Ðð 2°!Ñ4ÐØˆÜ!$Ø Ñ-Ø Ñ-ØÐ 1Ñ2ó"
ò 	hÑˆD�$˜ô
 ˜d 1™gŸl™l›n°°Q±·±³Ô?ˆDÜ˜d 1™gŸl™l›n°°Q±·±³Ô?ˆDØ×Ñœu°dÈTÐY^×YcÑYcÓYeÔfÕgð	hô ˜[Ñ.BÈDÔQˆØÐr   )NN)g        r,   )r   r   r   Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr>   rH   r   r   r   r   Úlistr   rL   r[   r`   rr   Ú__classcell__)rB   s   @r   r:   r:   E   sï   ø„ ñð €OØ ÐØ#ÐØ€Oô*ó9ð Ùd˜yÐd°UÐdÈCÐdÐTXÐY^ÑT_Òdó ØdàÙp˜t I™Ðp¸5ÐpÐRUÐpÐZ^Ð_cÐdiÑ_jÑZkÒpó Øpð
 ñ-à�Y‘ )Ñ+ð-ð ð-ð ð	-ð
 
ˆe‰�t˜D ™KÑ(Ñ	(õ-ó^"òñ¸TÀ%¹[÷ r   r:   N)Úcollections.abcr   Útypingr   r   r   r   Útyping_extensionsr   Úimage_utilsr
   Úutilsr   r   Úbaser   ÚPILr   r   r(   r   r   r   r   r8   r:   r   r   r   ú<module>r�      sˆ   ðö %ß 3Ó 3å &å &ß :Ý ñ ÔÝå(ð    m°SÐ&8Ñ 9Ñ:€	ˆ9Ó :ôˆyô ô
ˆIô ð$ ð $¨°(¸9Ñ2EÑ)Fó $ô0k˜xõ kr   