
    qi                     L    d dl Z ddlmZ ddlmZ ddlmZ  G d d	e      Zd	gZy)
    N   )
TensorType   )SuperGlueImageProcessorFast   )$EfficientLoFTRKeypointMatchingOutputc                   X    e Zd Z	 ddddeee   z  dedeeee	j                  f      fdZy)	 EfficientLoFTRImageProcessorFastoutputsr   target_sizes	thresholdreturnc                 4   |j                   j                  d   t        |      k7  rt        d      t	        d |D              st        d      t        |t              r,t        j                  ||j                   j                        }n1|j                  d   dk7  s|j                  d   dk7  rt        d      |}|j                  j                         }||j                  d      j                  dddd      z  }|j                  t        j                        }g }t!        ||j                   |j"                        D ]X  \  }}}	t        j$                  |	|kD  |dkD        }
|d   |
d      }|d   |
d      }|	d   |
d      }|j'                  |||d	       Z |S )
a  
        Converts the raw output of [`EfficientLoFTRKeypointMatchingOutput`] into lists of keypoints, scores and descriptors
        with coordinates absolute to the original image sizes.
        Args:
            outputs ([`EfficientLoFTRKeypointMatchingOutput`]):
                Raw outputs of the model.
            target_sizes (`torch.Tensor` or `List[Tuple[Tuple[int, int]]]`, *optional*):
                Tensor of shape `(batch_size, 2, 2)` or list of tuples of tuples (`Tuple[int, int]`) containing the
                target size `(height, width)` of each image in the batch. This must be the original image size (before
                any processing).
            threshold (`float`, *optional*, defaults to 0.0):
                Threshold to filter out the matches with low scores.
        Returns:
            `List[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image
            of the pair, the matching scores and the matching indices.
        r   zRMake sure that you pass in as many target sizes as the batch dimension of the maskc              3   8   K   | ]  }t        |      d k(    yw)r   N)len).0target_sizes     k/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/efficientloftr/modular_efficientloftr.py	<genexpr>zREfficientLoFTRImageProcessorFast.post_process_keypoint_matching.<locals>.<genexpr>!   s     I[3{#q(Is   zTEach element of target_sizes must contain the size (h, w) of each image of the batch)devicer   r   )
keypoints0
keypoints1matching_scores)matchesshaper   
ValueErrorall
isinstancelisttorchtensorr   	keypointscloneflipreshapetoint32zipr   logical_andappend)selfr   r   r   image_pair_sizesr#   resultskeypoints_pairr   scoresvalid_matchesmatched_keypoints0matched_keypoints1r   s                 r   post_process_keypoint_matchingz?EfficientLoFTRImageProcessorFast.post_process_keypoint_matching	   s   , ??  #s<'88qrrILIIsttlD)$||LAWAWX!!!$)\-?-?-Ba-G j   ,%%++-	 0 5 5b 9 A A"aA NN	LL-	/29goowOfOf/g 	+NGV!--fy.@'B,OM!/!2=3C!D!/!2=3C!D$Qia(89ONN"4"4'6	      N)g        )__name__
__module____qualname__r   r    tuplefloatdictstrr!   Tensorr4    r5   r   r
   r
      sN    
 	979 !4;.9 	9
 
d3$%	&9r5   r
   )	r!   utilsr   )superglue.image_processing_superglue_fastr   modeling_efficientloftrr   r
   __all__r>   r5   r   <module>rC      s)      S I:'B :z .
.r5   