
    h,                         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y)    )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<   y)KeypointxyN)__name__
__module____qualname__float__annotations__     g/var/www/html/aiagenthome/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%;&   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
uOE*
r0   N)r'   r   lenall)r.   
image_pairr*   s     r   r/   z'validate_image_pairs.<locals>.<genexpr>;   sP      
 %
 z8, 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8O 
 %	
 
 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   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                 p    t        |   |i | t        | d       | j                  dk7  rt	        d      y )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   s<    $)&)$)>>T!abb "r   c                 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                      y r,   r   rA   rK   rG   rC   s       r   __call__z!KeypointMatchingPipeline.__call__^   s    adr   c                      y r,   r   rM   s       r   rN   z!KeypointMatchingPipeline.__call__a   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!rG   r   r   )r6   r8   r>   rN   r3   )rA   rK   rG   rC   formatted_inputsoutputsrD   s         r   rN   z!KeypointMatchingPipeline.__call__d   sY    N >eff/7'"#3SySFS A%1:r   c                     |D cg c]  }t        ||       }}| j                  || j                        }|j                  | j                        }|D cg c]  }|j
                   }}||d}|S c c}w c c}w )N)rF   )r#   return_tensors)model_inputstarget_sizes)r   image_processorr@   todtypesize)rA   r#   rF   r)   rU   rV   preprocess_outputss          r   
preprocessz#KeypointMatchingPipeline.preprocess   s|    BHI&*UG4&I++6$..+Y#tzz2067u

7.:LY!! J 8s   A3A8c                 F    |d   } | j                   di |}||d   gd}|S )NrU   rV   )model_outputsrV   r   )model)rA   r[   rU   r^   forward_outputss        r   _forwardz!KeypointMatchingPipeline._forward   s9    ).9"

2\2,9L^_mLnKo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^   rV   )rV   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)rW   post_process_keypoint_matchingzipr   itemappendr   sorted)
rA   r`   rG   r^   rV   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"
D$
 d1gllnQ?Dd1gllnQ?DudTY^YcYcYefg"
 [.BDQr   )NN)g        r,   )r   r   r   __doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr?   rJ   r   r   r   r   listr   rN   r   r\   ra   rs   __classcell__)rD   s   @r   r:   r:   E   s     O #Oc9 dydUdCdTXY^T_d dptIp5pRUpZ^_cdi_jZkp 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 & :  (  mS&8 9:	9 :y 
I $ $(92E)F $0mx mr   