
    i                         d dl mZ 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d      Z G d de      Z G d de
      ZddgZy	)    )TYPE_CHECKING   )ImagesKwargs)
TensorTypeis_torch_available)requires   )SuperGlueImageProcessorPil)SuperGlueImageProcessorN   )$EfficientLoFTRKeypointMatchingOutputc                       e Zd ZU dZeed<   y)"EfficientLoFTRImageProcessorKwargsz
    do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`):
        Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method.
    do_grayscaleN)__name__
__module____qualname____doc__bool__annotations__     /var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/efficientloftr/modular_efficientloftr.pyr   r      s    
 r   r   F)totalc                   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     r   	<genexpr>zNEfficientLoFTRImageProcessor.post_process_keypoint_matching.<locals>.<genexpr>3        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tensorr0   	keypointscloneflipreshapetoint32zipr5   logical_andappend)selfr   r   r   image_pair_sizesr>   resultskeypoints_pairr6   scoresvalid_matchesmatched_keypoints0matched_keypoints1r5   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	  r   Ng        )r   r   r   r   r;   tuplefloatdictstrr<   TensorrO   r   r   r   r   r      sN    
 	979 !4;.9 	9
 
d3$%	&9r   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>s   r,   r-   r.   r/   r   r	   r1   r2   )r<   r6   r7   r'   r8   r9   r:   r;   r=   r0   r>   r?   r@   rA   rB   rC   rD   r5   rE   rF   )rG   r   r   r   r<   rH   r>   rI   rJ   r6   rK   rL   rM   rN   r5   s                  r   rO   z>EfficientLoFTRImageProcessorPil.post_process_keypoint_matchingX   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	  r   NrP   )r   r   r   r   r   r;   rQ   rR   rS   rT   rO   r   r   r   rW   rW   W   s\    z"
 	;7; !4;.; 	;
 
d3&'	(; #;r   rW   )typingr   processing_utilsr   utilsr   r   utils.import_utilsr   (superglue.image_processing_pil_supergluer
   $superglue.image_processing_supergluer   r<   modeling_efficientloftrr   r   r   rW   __all__r   r   r   <module>rc      s_      , 3 * Q J MU :#: :z=&@ =@ *+L
Mr   