
     `i,                     2   U d dl mZ d dl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<    G d de          Z G d de          Zdedeee                  fdZ G d de          ZdS )    )Sequence)Any	TypedDictUnion)	TypeAlias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<   dS )KeypointxyN)__name__
__module____qualname__float__annotations__     |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/pipelines/keypoint_matching.pyr   r   "   s"         HHHHHHHHr   r   c                   .    e Zd ZU eed<   eed<   eed<   dS )Matchkeypoint_image_0keypoint_image_1scoreN)r   r   r   r   r   r   r   r   r   r    r    '   s1         LLLLLr   r    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    r   _is_valid_imagez-validate_image_pairs.<locals>._is_valid_image4   s    E""<j&<&<<r   r	   c              3   .   K   | ]} |          V  d S Nr   .0r*   r+   s     r   	<genexpr>z'validate_image_pairs.<locals>.<genexpr>9   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   r0   z1validate_image_pairs.<locals>.<genexpr>.<genexpr>>   s-      CCuOOE**CCCCCCr   N)r(   r   lenall)r/   
image_pairr+   s     r   r0   z'validate_image_pairs.<locals>.<genexpr>;   s}       
 
  z8,, DJ1$DCCCC
CCCCC
 
 
 
 
 
r   )r(   r   r3   r4   
ValueError)r$   error_messager+   s     @r   validate_image_pairsr8   -   s    M= = = &(## 	v;;!#Q#Q#Q#Q&#Q#Q#Q Q Q8O 
 
 
 
 %	
 
 
 
 
 	 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   	frameworkr6   )selfargskwargs	__class__s      r   r?   z!KeypointMatchingPipeline.__init__O   sS    $)&)))$)))>T!!abbb "!r   Nc                 0    i }|||d<   i }|||d<   |i |fS )Ntimeout	thresholdr   )rA   rG   rF   preprocess_paramspostprocess_paramss        r   _sanitize_parametersz-KeypointMatchingPipeline._sanitize_parametersU   s>    +2i( .7{+ "&888r           inputsrG   rC   r%   c                     d S r-   r   rA   rL   rG   rC   s       r   __call__z!KeypointMatchingPipeline.__call__^   s    adadr   c                     d S r-   r   rN   s       r   rO   z!KeypointMatchingPipeline.__call__a   s    mpmpr   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!rG   r   r   )r6   r8   r>   rO   r3   )rA   rL   rG   rC   formatted_inputsoutputsrD   s         r   rO   z!KeypointMatchingPipeline.__call__d   so    N >efff/77"%''"#3SSySFSS  A%%1:r   c                     fd|D             }|                      || j                  }|                    | j                  }d |D             }||d}|S )Nc                 2    g | ]}t          |           S ))rF   r   )r/   r*   rF   s     r   
<listcomp>z7KeypointMatchingPipeline.preprocess.<locals>.<listcomp>   s&    III*UG444IIIr   )r$   return_tensorsc                     g | ]	}|j         
S r   )size)r/   r*   s     r   rV   z7KeypointMatchingPipeline.preprocess.<locals>.<listcomp>   s    777u
777r   )model_inputstarget_sizes)image_processorr@   todtype)rA   r$   rF   rZ   r[   preprocess_outputss     `   r   
preprocessz#KeypointMatchingPipeline.preprocess   sp    IIII&III++6$.+YY#tz2277777.:LYY!!r   c                 H    |d         } | j         di |}||d         gd}|S )NrZ   r[   )model_outputsr[   r   )model)rA   r_   rZ   rb   forward_outputss        r   _forwardz!KeypointMatchingPipeline._forward   s@    ).9"
22\22,9L^_mLnKo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 )Nrb   r[   )r[   rG   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)r\   post_process_keypoint_matchingzipr   itemappendr    sorted)
rA   rd   rG   rb   r[   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$
 d1gllnnQ???Dd1gllnnQ???DudTY^YcYcYeYefffgggg[.B.BDQQQr   )NN)rK   r-   )r   r   r   __doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr?   rJ   r   r   r   r   listr    rO   r   r`   re   rw   __classcell__)rD   s   @r   r:   r:   E   s         O #Oc c c c c9 9 9 9 ddydUdCdTXY^T_ddd XdpptIp5pRUpZ^_cdi_jZkppp Xp
 - -d9oy01- - 	-
 
tE{De--	.- - - - - -^" " " "   T%[        r   r:   N)collections.abcr   typingr   r   r   typing_extensionsr   r   image_utilsr
   utilsr   r   baser   PILr   r   r)   r   r   r   r    r8   r:   r   r   r   <module>r      s   % $ $ $ $ $ $ ( ( ( ( ( ( ( ( ( ( 1 1 1 1 1 1 1 1 & & & & & & : : : : : : : :        )((((((  mS&8 9:	9 : : :    y   
    I   $ $(92E)F $ $ $ $0m m m m mx m m m m mr   