
    h                         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y)    )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              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    f/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/transformers/pipelines/keypoint_matching.py_is_valid_imagez-validate_image_pairs.<locals>._is_valid_image+   s    E"<j&<<    r	   c              3   .   K   | ]  } |        y wN .0r!   r#   s     r"   	<genexpr>z'validate_image_pairs.<locals>.<genexpr>0   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>5   s     CuOE*Cr+   N)r   r   lenall)r)   
image_pairr#   s     r"   r*   z'validate_image_pairs.<locals>.<genexpr>2   sN      
  z8, DJ1$DC
CCD
s   A A)r   r   r.   r/   
ValueError)r   error_messager#   s     @r"   validate_image_pairsr3   $   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   	frameworkr1   )selfargskwargs	__class__s      r"   r:   z!KeypointMatchingPipeline.__init__F   s<    $)&)$)>>T!abb "r$   c                 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                      y r&   r'   r<   rF   rB   r>   s       r"   __call__z!KeypointMatchingPipeline.__call__U   s    adr$   c                      y r&   r'   rH   s       r"   rI   z!KeypointMatchingPipeline.__call__X   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!rB   r   r   )r1   r3   r9   rI   r.   )r<   rF   rB   r>   formatted_inputsoutputsr?   s         r"   rI   z!KeypointMatchingPipeline.__call__[   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)rA   )r   return_tensors)model_inputstarget_sizes)r   image_processorr;   totorch_dtypesize)r<   r   rA   r!   rP   rQ   preprocess_outputss          r"   
preprocessz#KeypointMatchingPipeline.preprocess   s|    BHI*UG4II++6$..+Y#t'7'78067u

77.:LY!! J 8s   A3A8c                 F    |d   } | j                   di |}||d   gd}|S )NrP   rQ   )model_outputsrQ   r'   )model)r<   rV   rP   rY   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 )NrY   rQ   )rQ   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)rR   post_process_keypoint_matchingzipr   itemappendr   sorted)
r<   r[   rB   rY   rQ   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$
 d1gllnQ?Dd1gllnQ?DudTY^YcYcYefg	h [.BDQr$   )NN)g        r&   )__name__
__module____qualname____doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr:   rE   r   r   floatr   listr   rI   r   rW   r\   rn   __classcell__)r?   s   @r"   r5   r5   <   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$   r5   N)typingr   r   r   r   typing_extensionsr   r   image_utilsr
   utilsr   r   baser   PILr   r   r    r   __annotations__rw   r   r   r3   r5   r'   r$   r"   <module>r      s    3 2 1 & :  (  mS&8 9:	9 :Zu5!9:'hafgh$ $(92E)F $0mx mr$   