
    ^j                         d dl mZ ddlmZmZ ddlmZ ddlmZ ddl	m
Z
  e       rd dlZerd	d
lmZ  G d de
      Z G d de      ZddgZy)    )TYPE_CHECKING   )
TensorTypeis_torch_available)requires   )SuperGlueImageProcessorPil)SuperGlueImageProcessorN   )$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)	EfficientLoFTRImageProcessor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 )
  
        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   RMake 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     /var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/efficientloftr/modular_efficientloftr.py	<genexpr>zNEfficientLoFTRImageProcessor.post_process_keypoint_matching.<locals>.<genexpr>)        I[3{#q(I   TEach 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_sizesr1   resultskeypoints_pairr)   scoresvalid_matchesmatched_keypoints0matched_keypoints1r(   s                 r   post_process_keypoint_matchingz;EfficientLoFTRImageProcessor.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/   TensorrB    rC   r   r   r      sN    
 	979 !4;.9 	9
 
d3$%	&9rC   r   c                   \    e Zd Z ed      	 ddddeee   z  dedeee	df      fd	       Z
y
)EfficientLoFTRImageProcessorPil)r/   )backendsr   r   r   r   r   ztorch.Tensorc                 (   ddl }|j                  j                  d   t        |      k7  rt	        d      t        d |D              st	        d      t        |t              r) |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                  |j                        }g }t!        ||j                  |j"                        D ]U  \  }}	}
 |j$                  |
|kD  |	d	kD        }|d   |d      }|d   |d      }|
d   |d      }|j'                  |||d
       W |S )r   r   Nr   c              3   8   K   | ]  }t        |      d k(    ywr   r   r   s     r   r   zQEfficientLoFTRImageProcessorPil.post_process_keypoint_matching.<locals>.<genexpr>i   r   r    r!   r"   r   r   r$   r%   )r/   r)   r*   r   r+   r,   r-   r.   r0   r#   r1   r2   r3   r4   r5   r6   r7   r(   r8   r9   )r:   r   r   r   r/   r;   r1   r<   r=   r)   r>   r?   r@   rA   r(   s                  r   rB   z>EfficientLoFTRImageProcessorPil.post_process_keypoint_matchingN   s   . 	??  #s<'88qrrILIIsttlD)+u||LAWAWX!!!$)\-?-?-Ba-G j   ,%%++-	 0 5 5b 9 A A"aA NN	LL-	/29goowOfOf/g 	+NGV-E--fy.@'B,OM!/!2=3C!D!/!2=3C!D$Qia(89ONN"4"4'6	  rC   NrD   )rE   rF   rG   r   r   r.   rH   rI   rJ   rK   rB   rM   rC   r   rO   rO   M   s\    z"
 	;7; !4;.; 	;
 
d3&'	(; #;rC   rO   )typingr   utilsr   r   utils.import_utilsr   (superglue.image_processing_pil_supergluer	   $superglue.image_processing_supergluer
   r/   modeling_efficientloftrr   r   rO   __all__rM   rC   r   <module>rZ      sN      3 * Q J M:#: :z=&@ =@ *+L
MrC   