
    Z j1                     H   S SK Jr  S SKJrJr  S SKJr  SSKJ	r	  SSK
Jr  SSKJrJr  SSKJrJrJrJrJrJrJrJr  SS	KJrJr  SS
KJrJrJr  \" 5       (       a  S SKr\(       a  SSK J!r!   " S S\SS9r"S r#S\4S jr$  SS jr%    SS jr&\ " S S\	5      5       r'S/r(g)    )TYPE_CHECKING)Image	ImageDraw)
functional   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)
ImageInput	ImageTypePILImageResamplingSizeDictget_image_typeis_pil_imageis_valid_imageto_numpy_array)ImagesKwargsUnpack)
TensorTypeauto_docstringis_torch_availableN   )$EfficientLoFTRKeypointMatchingOutputc                   $    \ rS rSr% Sr\\S'   Srg)"EfficientLoFTRImageProcessorKwargs$   z
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_grayscale N)__name__
__module____qualname____firstlineno____doc__bool__annotations____static_attributes__r       ړ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/efficientloftr/image_processing_efficientloftr.pyr   r   $   s    
 r(   r   F)totalc                     [        U 5      =(       dM    [        U 5      =(       a;    [        U 5      [        R                  :g  =(       a    [        U R                  5      S:H  $ )Nr   r   r   r   r   PILlenshapeimages    r)   _is_valid_imager2   -   sF     ub."79=="HbSQVQ\Q\M]abMbr(   imagesc                   ^ SnS m[        U [        5      (       ab  [        U 5      S:X  a  [        U4S jU  5       5      (       a  U $ [        U4S jU  5       5      (       a  U  VVs/ s H  o"  H  o3PM     M     snn$ [	        U5      es  snnf )N)z-Input images must be a one of the following :z - A pair of PIL images.z - A pair of 3D arrays.z! - A list of pairs of PIL images.z  - A list of pairs of 3D arrays.c                     [        U 5      =(       dM    [        U 5      =(       a;    [        U 5      [        R                  :g  =(       a    [        U R                  5      S:H  $ )z$images is a PIL Image or a 3D array.r   r,   r0   s    r)   r2   8validate_and_format_image_pairs.<locals>._is_valid_image<   sG    E" 
5!fnU&;y}}&LfQTUZU`U`QaefQf	
r(      c              3   4   >#    U  H  nT" U5      v   M     g 7fNr   .0r1   r2   s     r)   	<genexpr>2validate_and_format_image_pairs.<locals>.<genexpr>C   s     #Q&_U%;%;&   c              3      >#    U  HF  n[        U[        5      =(       a*    [        U5      S :H  =(       a    [        U4S jU 5       5      v   MH     g7f)r7   c              3   4   >#    U  H  nT" U5      v   M     g 7fr9   r   r:   s     r)   r<   <validate_and_format_image_pairs.<locals>.<genexpr>.<genexpr>H   s     C
uOE**
r>   N)
isinstancelistr.   all)r;   
image_pairr2   s     r)   r<   r=   E   sO      
 %
 z4( DJ1$DC
CCD %s   AA)rB   rC   r.   rD   
ValueError)r3   error_messagerE   r1   r2   s       @r)   validate_and_format_image_pairsrH   3   s    M
 &$v;!#Q&#Q Q QM 
 %	
 
 
 -3KFj
uE
EFKK
]
## Ls   &B	c           	      8   U R                   S:  d%  U R                  U R                   S:X  a  SOS   S:X  a  g[        R                  " U SSSS2SS24   U SSSS2SS24   :H  5      =(       a/    [        R                  " U SSSS2SS24   U SSSS2SS24   :H  5      $ )zAChecks if an image is grayscale (all RGB channels are identical).r   r   r   T.Nr7   )ndimr/   torchrD   r0   s    r)   is_grayscalerL   O   s     zzA~%**/QqAQF99U31a<(E#q!Q,,??@ UYYc1aluS!Q\22F r(   c                 P    [        U 5      (       a  U $ [        R                  " U SS9$ )a  
Converts an image to grayscale format using the NTSC formula. Only support torch.Tensor.

This function is supposed to return a 1-channel image, but it returns a 3-channel image with the same value in each
channel, because of an issue that is discussed in :
https://github.com/huggingface/transformers/pull/25786#issuecomment-1730176446

Args:
    image (torch.Tensor):
        The image to convert.
r   )num_output_channels)rL   tvFrgb_to_grayscaler0   s    r)   convert_to_grayscalerQ   Z   s'     E1==r(   c                     ^  \ rS rSr\r\R                  rSSS.r	Sr
SrSrSrSrSrS	\\   4U 4S
 jjr\S\S	\\   S\4U 4S jj5       rS\S\4S jr S$S\S   S\S\SSS\S\S\S-  S\\-  S-  S\S\4S jjr S%SSS\\\   -  S\S\\\\ RB                  4      4S jjr"S\\\\ RB                  4      S\S    4S! jr#S" r$S#r%U =r&$ )&EfficientLoFTRImageProcessorm   i  i  )heightwidthFTgp?Nkwargsc                 &   > [         TU ]  " S0 UD6  g )Nr   )super__init__)selfrW   	__class__s     r)   rZ   %EfficientLoFTRImageProcessor.__init__y   s    "6"r(   r3   returnc                 &   > [         TU ]  " U40 UD6$ r9   )rY   
preprocess)r[   r3   rW   r\   s      r)   r`   'EfficientLoFTRImageProcessor.preprocess|   s    w!&3F33r(   c                 :    U R                  U5      n[        U5      $ r9   )fetch_imagesrH   )r[   r3   rW   s      r)   _prepare_images_structure6EfficientLoFTRImageProcessor._prepare_images_structure   s     ""6*.v66r(   torch.Tensor	do_resizesizeresamplez7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordisable_groupingreturn_tensorsr   c
                 (   [        XS9u  p0 nUR                  5        H   u  pU(       a  U R                  XUS9nXU'   M"     [        X5      n[        UUS9u  p0 nUR                  5        H3  u  pU(       a  U R	                  X5      nU	(       a  [        U5      nXU'   M5     [        X5      n[        S[        U5      S5       Vs/ s H  nUUUS-    PM     nnU Vs/ s H  n[        R                  " USS9PM     nn[        SU0US9$ s  snf s  snf )N)rl   )rh   ri   r   r7   )dimpixel_values)datatensor_type)r
   itemsresizer   rescalerQ   ranger.   rK   stackr	   )r[   r3   rg   rh   ri   rj   rk   rl   rm   r   rW   grouped_imagesgrouped_images_indexprocessed_images_groupedr/   stacked_imagesresized_imagesprocessed_imagesiimage_pairspairstacked_pairss                         r)   _preprocess(EfficientLoFTRImageProcessor._preprocess   s0    0EV/o,#% %3%9%9%;!E!%^QY!Z.<U+ &< ((@W/D^fv/w,#% %3%9%9%;!E!%n!M!5n!E.<U+ &< **BY =B!SIYEZ\]<^_<^q'AE2<^_ ?JJkdTq1kJ .-!@n]] ` Ks   D
Doutputsr   target_sizes	thresholdc                 F   UR                   R                  S   [        U5      :w  a  [        S5      e[	        S U 5       5      (       d  [        S5      e[        U[        5      (       a)  [        R                  " X!R                   R                  S9nO3UR                  S   S:w  d  UR                  S   S:w  a  [        S5      eUnUR                  R                  5       nXTR                  S5      R                  SSSS5      -  nUR                  [        R                  5      n/ n[!        XQR                   UR"                  5       HX  u  pxn	[        R$                  " X:  US:  5      n
US   U
S      nUS   U
S      nU	S   U
S      nUR'                  UUUS	.5        MZ     U$ )
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   >   #    U  H  n[        U5      S :H  v   M     g7f)r7   N)r.   )r;   target_sizes     r)   r<   NEfficientLoFTRImageProcessor.post_process_keypoint_matching.<locals>.<genexpr>   s     IL[3{#q(Ls   zTEach element of target_sizes must contain the size (h, w) of each image of the batch)devicer   r7   )
keypoints0
keypoints1matching_scores)matchesr/   r.   rF   rD   rB   rC   rK   tensorr   	keypointscloneflipreshapetoint32zipr   logical_andappend)r[   r   r   r   image_pair_sizesr   resultskeypoints_pairr   scoresvalid_matchesmatched_keypoints0matched_keypoints1r   s                 r)   post_process_keypoint_matching;EfficientLoFTRImageProcessor.post_process_keypoint_matching   s   , ??  #s<'88qrrILIIIsttlD))$||LAWAWX!!!$)\-?-?-Ba-G j   ,%%++-	 5 5b 9 A A"aA NN	LL-	/29oowOfOf/g+NV!--f.@'B,OM!/!2=3C!D!/!2=3C!D$Qia(89ONN"4"4'6 0h  r(   keypoint_matching_outputzImage.Imagec           	      2   [        U5      nU Vs/ s H  n[        U5      PM     nn[        S[        U5      S5       Vs/ s H	  oAXDS-    PM     nn/ n[	        XR5       GH  u  pxUS   R
                  SS u  pUS   R
                  SS u  p[        R                  " [        X5      X-   S4[        R                  S9n[        R                  " US   5      USU	2SU
24'   [        R                  " US   5      USU2U
S24'   [        R                  " UR                  5       5      n[        R                  " U5      nUS   R!                  S5      u  nnUS   R!                  S5      u  nn[	        UUUUUS	   5       Hx  u  nnnnnU R#                  U5      nUR%                  UUUU
-   U4USS
9  UR'                  US-
  US-
  US-   US-   4SS9  UR'                  UU
-   S-
  US-
  UU
-   S-   US-   4SS9  Mz     UR)                  U5        GM     U$ s  snf s  snf )a`  
Plots the image pairs side by side with the detected keypoints as well as the matching between them.

Args:
    images:
        Image pairs to plot. Same as `EfficientLoFTRImageProcessor.preprocess`. Expects either a list of 2
        images or a list of list of 2 images list with pixel values ranging from 0 to 255.
    keypoint_matching_output (List[Dict[str, torch.Tensor]]]):
        A post processed keypoint matching output

Returns:
    `List[PIL.Image.Image]`: A list of PIL images, each containing the image pairs side by side with the detected
    keypoints as well as the matching between them.
r   r7   Nr   r   )dtyper   r   r   )fillrV   black)r   )rH   r   rv   r.   r   r/   rK   zerosmaxuint8
from_numpyr   	fromarraynumpyr   Drawunbind
_get_colorlineellipser   )r[   r3   r   r1   r~   r   r   rE   pair_outputheight0width0height1width1
plot_imageplot_image_pildrawkeypoints0_xkeypoints0_ykeypoints1_xkeypoints1_ykeypoint0_xkeypoint0_ykeypoint1_xkeypoint1_ymatching_scorecolors                             r)   visualize_keypoint_matching8EfficientLoFTRImageProcessor.visualize_keypoint_matching   sS   ( 185;<VE.'V<273v;2JK2JQaa%(2JK'*;'Q#J(m11"15OG(m11"15OGc'&;V_a%PX]XcXcdJ,1,<,<Z],KJxx&(),1,<,<Z],KJxx()"__Z-=-=-?@N>>.1D)4\)B)I)I!)L&L,)4\)B)I)I!)L&L,VYlL,TeHfWR[+{N 7		 +{V/C[Q  
 kAo{QaQ\_`Q`ahop 6)A-{Qf@TWX@XZehiZij   W NN>*7 (R8 A =Ks
   HHc                 L    [        SSU-
  -  5      n[        SU-  5      nSnX#U4$ )zMaps a score to a color.   r   r   )int)r[   scorergbs        r)   r   'EfficientLoFTRImageProcessor._get_color&  s1    q5y!"eQwr(   r   )T)g        )'r    r!   r"   r#   r   valid_kwargsr   BILINEARri   rh   default_to_squarerg   rj   rk   do_normalizer   r   rZ   r   r   r	   r`   rd   rC   r%   r   floatstrr   r   tupledictrK   Tensorr   r   r   r'   __classcell__)r\   s   @r)   rS   rS   m   s   5L!**HC(DIJNLL#(J!K # 4 4v>`7a 4fr 4 477 
	7& ")^^$)^ )^ 	)^
 L)^ )^ )^ +)^ j(4/)^ )^ 
)^^ 	979 !4;.9 	9
 
d3$%	&9v5 #'tC,='>"?5 
m		5n r(   rS   )r1   rf   )r1   rf   r^   rf   ))typingr   r-   r   r   torchvision.transforms.v2r   rO   image_processing_backendsr   image_processing_utilsr	   image_transformsr
   r   image_utilsr   r   r   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   rK   modeling_efficientloftrr   r   r2   rH   rL   rQ   rS   __all__r   r(   r)   <module>r      s    !   7 ; 2 E	 	 	 5 C C MU $J $8>>>& }#5 } }@ *
*r(   