
    bi                     2   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dS )    )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<   dS )KeypointxyN)__name__
__module____qualname__float__annotations__     c/root/projects/butler/venv/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                   B    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                 Z     t                      j        |i | t          | d           d S )Nvision)super__init__r   )selfargskwargs	__class__s      r   r>   z!KeypointMatchingPipeline.__init__O   s5    $)&)))$)))))r   Nc                 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                     d S r-   r   r?   rJ   rE   rA   s       r   __call__z!KeypointMatchingPipeline.__call__\   s    adadr   c                     d S r-   r   rL   s       r   rM   z!KeypointMatchingPipeline.__call___   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!rE   r   r   )r6   r8   r=   rM   r3   )r?   rJ   rE   rA   formatted_inputsoutputsrB   s         r   rM   z!KeypointMatchingPipeline.__call__b   so    N >efff/77"%''"#3SSySFSS  A%%1:r   c                     fd|D             }|                      |d          }|                    | j                  }d |D             }||d}|S )Nc                 2    g | ]}t          |           S ))rD   r   )r/   r*   rD   s     r   
<listcomp>z7KeypointMatchingPipeline.preprocess.<locals>.<listcomp>   s&    III*UG444IIIr   pt)r$   return_tensorsc                     g | ]	}|j         
S r   )size)r/   r*   s     r   rT   z7KeypointMatchingPipeline.preprocess.<locals>.<listcomp>   s    777u
777r   )model_inputstarget_sizes)image_processortodtype)r?   r$   rD   rY   rZ   preprocess_outputss     `   r   
preprocessz#KeypointMatchingPipeline.preprocess   sn    IIII&III++6$+OO#tz2277777.:LYY!!r   c                 H    |d         } | j         di |}||d         gd}|S )NrY   rZ   )model_outputsrZ   r   )model)r?   r^   rY   ra   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 )Nra   rZ   )rZ   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)r[   post_process_keypoint_matchingzipr   itemappendr    sorted)
r?   rc   rE   ra   rZ   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)rI   r-   )r   r   r   __doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr>   rH   r   r   r   r   listr    rM   r_   rd   rv   __classcell__)rB   s   @r   r:   r:   E   s         O #O* * * * *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
 - -Y)+- - 	-
 
etD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 3 3 3 3 3 3 3 3 3 3 & & & & & & & & & & & & : : : : : : : :        )((((((  mS&8 9:	9 : : :    y   
    I   $ $(92E)F $ $ $ $0k k k k kx k k k k kr   