§
    Þ˜ºh  ã                   ó0  — U d dl mZmZmZmZ d dlmZ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<    edeedœ¦  «        Z edeeedœ¦  «        Zdedeee                  fd„Z G d„ de¦  «        ZdS )é    )ÚAnyÚSequenceÚ	TypedDictÚUnion)Ú	TypeAliasÚoverloadé   )Úis_pil_image)Úis_vision_availableÚrequires_backendsé   )ÚPipeline)ÚImage©Ú
load_imagezImage.ImageÚ	ImagePairÚKeypoint©ÚxÚyÚMatch©Úkeypoint_image_0Úkeypoint_image_1ÚscoreÚimagesÚreturnc                 óò   ‡— d}d„ Št          | t          ¦  «        rNt          | ¦  «        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                 óJ   — t          | ¦  «        pt          | t          ¦  «        S )z"images is a PIL Image or a string.)r
   Ú
isinstanceÚstr)Úimages    úp/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/transformers/pipelines/keypoint_matching.pyÚ_is_valid_imagez-validate_image_pairs.<locals>._is_valid_image+   s   € å˜EÑ"Ô"Ð<¥j°½Ñ&<Ô&<Ð<ó    r	   c              3   ó.   •K  — | ]} ‰|¦  «        V — Œd S ©N© ©Ú.0r"   r$   s     €r#   ú	<genexpr>z'validate_image_pairs.<locals>.<genexpr>0   s-   øè è € Ð#QÐ#QÀ _ _°UÑ%;Ô%;Ð#QÐ#QÐ#QÐ#QÐ#QÐ#Qr%   c              3   óž   •K  — | ]G}t          |t          ¦  «        o-t          |¦  «        d k    ot          ˆfd„|D ¦   «         ¦  «        V — ŒHdS )r	   c              3   ó.   •K  — | ]} ‰|¦  «        V — Œd S r'   r(   r)   s     €r#   r+   z1validate_image_pairs.<locals>.<genexpr>.<genexpr>5   s-   øè è € ÐCÐC¨u�O�O EÑ*Ô*ÐCÐCÐCÐCÐCÐCr%   N)r    r   ÚlenÚall)r*   Ú
image_pairr$   s     €r#   r+   z'validate_image_pairs.<locals>.<genexpr>2   s}   øè è € ð 
ð 
ð õ �z¥8Ñ,Ô,ð DÝ�J‘” 1Ò$ðDåÐCÐCÐCÐC¸
ÐCÑCÔCÑCÔCð
ð 
ð 
ð 
ð 
ð 
r%   )r    r   r.   r/   Ú
ValueError)r   Úerror_messager$   s     @r#   Úvalidate_image_pairsr3   $   s¶   ø€ ð€Mð=ð =ð =õ �&�(Ñ#Ô#ð 	Ýˆv‰;Œ;˜!ÒÐ¥Ð#QÐ#QÐ#QÐ#QÈ&Ð#QÑ#QÔ#QÑ QÔ QÐØ�8ˆOÝð 
ð 
ð 
ð 
ð %ð	
ñ 
ô 
ñ 
ô 
ð 	ð ˆMÝ
�]Ñ
#Ô
#Ð#r%   c                   óV  ‡ — 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         ef         d	ed
edeee         eee                  f         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          ¦   «         j        |i |¤Ž t          | d¦  «         | j        dk    rt	          d¦  «        ‚d S )NÚvisionÚptzBKeypoint matching pipeline only supports PyTorch (framework='pt').)ÚsuperÚ__init__r   Ú	frameworkr1   )ÚselfÚargsÚkwargsÚ	__class__s      €r#   r:   z!KeypointMatchingPipeline.__init__F   sS   ø€ Ø�‰ŒÔ˜$Ð) &Ð)Ð)Ð)Ý˜$ Ñ)Ô)Ð)ØŒ>˜TÒ!Ð!ÝÐaÑbÔbÐbð "Ð!r%   Nc                 ó0   — i }|�||d<   i }|�||d<   |i |fS )NÚtimeoutÚ	thresholdr(   )r<   rB   rA   Úpreprocess_paramsÚpostprocess_paramss        r#   Ú_sanitize_parametersz-KeypointMatchingPipeline._sanitize_parametersL   s>   € ØÐØÐØ+2Ð˜iÑ(ØÐØÐ Ø.7Ð˜{Ñ+Ø  "Ð&8Ð8Ð8r%   ç        ÚinputsrB   r>   r   c                 ó   — d S r'   r(   ©r<   rG   rB   r>   s       r#   Ú__call__z!KeypointMatchingPipeline.__call__U   s   € ØadÐadr%   c                 ó   — d S r'   r(   rI   s       r#   rJ   z!KeypointMatchingPipeline.__call__X   s   € ØmpÐmpr%   c                 ó¶   •— |€t          d¦  «        ‚t          |¦  «        } t          ¦   «         j        |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.
        NzFCannot call the keypoint-matching pipeline without an inputs argument!rB   r   r   )r1   r3   r9   rJ   r.   )r<   rG   rB   r>   Úformatted_inputsÚoutputsr?   s         €r#   rJ   z!KeypointMatchingPipeline.__call__[   so   ø€ ðN ˆ>ÝÐeÑfÔfÐfÝ/°Ñ7Ô7ÐØ"•%‘'”'Ô"Ð#3ÐSÐS¸yÐSÈFÐSÐSˆÝÐÑ Ô  AÒ%Ð%Ø˜1”:ÐØˆr%   c                 ó²   ‡— ˆfd„|D ¦   «         }|                       || j        ¬¦  «        }|                     | j        ¦  «        }d„ |D ¦   «         }||dœ}|S )Nc                 ó2   •— g | ]}t          |‰¬ ¦  «        ‘ŒS ))rA   r   )r*   r"   rA   s     €r#   ú
<listcomp>z7KeypointMatchingPipeline.preprocess.<locals>.<listcomp>‹   s&   ø€ ÐIÐIÐI¸•*˜U¨GÐ4Ñ4Ô4ÐIÐIÐIr%   )r   Úreturn_tensorsc                 ó   — g | ]	}|j         ‘Œ
S r(   )Úsize)r*   r"   s     r#   rQ   z7KeypointMatchingPipeline.preprocess.<locals>.<listcomp>Ž   s   € Ð7Ð7Ð7 u˜œ
Ð7Ð7Ð7r%   )Úmodel_inputsÚtarget_sizes)Úimage_processorr;   ÚtoÚtorch_dtype)r<   r   rA   rU   rV   Úpreprocess_outputss     `   r#   Ú
preprocessz#KeypointMatchingPipeline.preprocessŠ   sq   ø€ ØIÐIÐIÐIÀ&ÐIÑIÔIˆØ×+Ò+°6È$Ì.Ð+ÑYÔYˆØ#—’ tÔ'7Ñ8Ô8ˆØ7Ð7°Ð7Ñ7Ô7ˆØ.:ÈLÐYÐYÐØ!Ð!r%   c                 óH   — |d         } | j         di |¤Ž}||d         gdœ}|S )NrU   rV   )Úmodel_outputsrV   r(   )Úmodel)r<   rZ   rU   r]   Úforward_outputss        r#   Ú_forwardz!KeypointMatchingPipeline._forward’   s@   € Ø)¨.Ô9ˆØ"˜œ
Ð2Ð2 \Ð2Ð2ˆØ,9ÐL^Ð_mÔLnÐKoÐpÐpˆØÐr%   c           	      ó`  — |d         }|d         }| j                              |||¬¦  «        }|d         }g }t          |d         |d         |d         ¦  «        D ]¿\  }}}	t          |d                              ¦   «         |d                              ¦   «         ¬	¦  «        }t          |d                              ¦   «         |d                              ¦   «         ¬	¦  «        }|                     t          |||	                     ¦   «         ¬
¦  «        ¦  «         ŒÀt          |d„ d¬¦  «        }|S )Nr]   rV   )rV   rB   r   Ú
keypoints0Ú
keypoints1Úmatching_scoresr   r   r   c                 ó   — | d         S )Nr   r(   )r   s    r#   ú<lambda>z6KeypointMatchingPipeline.postprocess.<locals>.<lambda>¨   s
   € ¸¸'¼
€ r%   T)ÚkeyÚreverse)rW   Úpost_process_keypoint_matchingÚzipr   ÚitemÚappendr   Úsorted)
r<   r_   rB   r]   rV   Úpostprocess_outputsÚpair_resultÚkp_0Úkp_1r   s
             r#   Úpostprocessz$KeypointMatchingPipeline.postprocess˜   s0  € Ø'¨Ô8ˆØ& ~Ô6ˆØ"Ô2×QÒQØ¨À	ð Rñ 
ô 
Ðð 2°!Ô4ÐØˆÝ!$Ø Ô-Ø Ô-ØÐ 1Ô2ñ"
ô "
ð 	hð 	hÑˆD�$˜õ
 ˜d 1œgŸlšl™nœn°°Q´·²±´Ð?Ñ?Ô?ˆDÝ˜d 1œgŸlšl™nœn°°Q´·²±´Ð?Ñ?Ô?ˆDØ×Ò�u°dÈTÐY^×YcÒYcÑYeÔYeÐfÑfÔfÑgÔgÐgÐgÝ˜[Ð.BÐ.BÈDÐQÑQÔQˆØÐr%   )NN)rF   r'   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr:   rE   r   r   Úfloatr   Úlistr   rJ   r   r[   r`   rr   Ú__classcell__)r?   s   @r#   r5   r5   <   s£  ø€ € € € € ðð ð €OØ ÐØ#ÐØ€Oðcð cð cð cð cð9ð 9ð 9ð 9ð ØdÐd˜yÐd°UÐdÈCÐdÐTXÐY^ÔT_ÐdÐdÐdñ „XØdàØpÐp˜t IœÐp¸5ÐpÐRUÐpÐZ^Ð_cÐdiÔ_jÔZkÐpÐpÐpñ „XØpð
 ð-ð -à�d˜9”o yÐ0Ô1ð-ð ð-ð ð	-ð
 
ˆt�EŒ{˜D  e¤Ô-Ð-Ô	.ð-ð -ð -ð -ð -ð -ð^"ð "ð "ð "ðð ð ðð ¸TÀ%¼[ð ð ð ð ð ð ð ð r%   r5   N)Útypingr   r   r   r   Útyping_extensionsr   r   Úimage_utilsr
   Úutilsr   r   Úbaser   ÚPILr   r   r!   r   Ú__annotations__r{   r   r   r3   r5   r(   r%   r#   ú<module>r…      sy  ðð 3Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2à 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1à &Ð &Ð &Ð &Ð &Ð &Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø Ð Ð Ð Ð Ð ð ÐÑÔð )ØÐÐÐÐÐà(Ð(Ð(Ð(Ð(Ð(ð    m°SÐ&8Ô 9Ô:€	ˆ9Ð :Ð :Ñ :àˆ9�Z u°5Ð!9Ð!9Ñ:Ô:€Øˆ	�'°ÈhÐafÐgÐgÑhÔh€ð$ ð $¨°(¸9Ô2EÔ)Fð $ð $ð $ð $ð0mð mð mð mð m˜xñ mô mð mð mð mr%   