o
    ei                     @   sF   d dl Z ddlmZ ddlmZ ddlmZ G dd	 d	eZd	gZdS )
    N   )
TensorType   )SuperGlueImageProcessorFast   )$EfficientLoFTRKeypointMatchingOutputc                   @   s@   e Zd Z	d
dddeee B dedeeee	j
f  fddZd	S ) EfficientLoFTRImageProcessorFast        outputsr   target_sizes	thresholdreturnc                 C   s&  |j jd t|krtdtdd |D stdt|tr*tj||j j	d}n|jd dks8|jd dkr<td|}|j
 }||d	d	ddd }|tj}g }t||j |jD ]0\}}}	t|	|k|d	k}
|d |
d  }|d |
d  }|	d |
d  }||||d
 q`|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                 s   s    | ]	}t |d kV  qdS )r   N)len).0target_size r   w/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/efficientloftr/modular_efficientloftr.py	<genexpr>!   s    zREfficientLoFTRImageProcessorFast.post_process_keypoint_matching.<locals>.<genexpr>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   r   r   r   post_process_keypoint_matching	   s6   

z?EfficientLoFTRImageProcessorFast.post_process_keypoint_matchingN)r	   )__name__
__module____qualname__r   r   tuplefloatdictstrr   Tensorr2   r   r   r   r   r      s    
r   )	r   utilsr   )superglue.image_processing_superglue_fastr   modeling_efficientloftrr   r   __all__r   r   r   r   <module>   s    
=