
    Z j4                     ,   S SK Jr  S SK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  \(       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_CHECKINGN)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   )LightGlueKeypointMatchingOutputc                   $    \ rS rSr% Sr\\S'   Srg)LightGlueImageProcessorKwargs/   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/lightglue/image_processing_lightglue.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_imager1   8   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+   r/   s    r(   r1   8validate_and_format_image_pairs.<locals>._is_valid_imageG   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   .0r0   r1   s     r(   	<genexpr>2validate_and_format_image_pairs.<locals>.<genexpr>N   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)r6   c              3   4   >#    U  H  nT" U5      v   M     g 7fr8   r   r9   s     r(   r;   <validate_and_format_image_pairs.<locals>.<genexpr>.<genexpr>S   s     C
uOE**
r=   N)
isinstancelistr-   all)r:   
image_pairr1   s     r(   r;   r<   P   sO      
 %
 z4( DJ1$DC
CCD %s   AA)rA   rB   r-   rC   
ValueError)r2   error_messagerD   r0   r1   s       @r(   validate_and_format_image_pairsrG   >   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.Nr6   )ndimr.   torchrC   r/   s    r(   is_grayscalerK   Z   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)rK   tvFrgb_to_grayscaler/   s    r(   convert_to_grayscalerP   e   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&$ )&LightGlueImageProcessorx   i  i  )heightwidthFTgp?Nkwargsc                 &   > [         TU ]  " S0 UD6  g )Nr   )super__init__)selfrV   	__class__s     r(   rY    LightGlueImageProcessor.__init__   s    "6"r'   r2   returnc                 &   > [         TU ]  " U40 UD6$ r8   )rX   
preprocess)rZ   r2   rV   r[   s      r(   r_   "LightGlueImageProcessor.preprocess   s    w!&3F33r'   c                 :    U R                  U5      n[        U5      $ r8   )fetch_imagesrG   )rZ   r2   rV   s      r(   _prepare_images_structure1LightGlueImageProcessor._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)rk   )rg   rh   r   r6   )dimpixel_values)datatensor_type)r
   itemsresizer   rescalerP   ranger-   rJ   stackr	   )rZ   r2   rf   rg   rh   ri   rj   rk   rl   r   rV   grouped_imagesgrouped_images_indexprocessed_images_groupedr.   stacked_imagesresized_imagesprocessed_imagesiimage_pairspairstacked_pairss                         r(   _preprocess#LightGlueImageProcessor._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                    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[!        UR                   XQR"                  S	S	2S4   UR$                  S	S	2S4   5       Hq  u  pxpUS   S:  nUS   S:  nUS   U   nUS   U   nX   nX   nUU:  US:  -  XR                  S   :  -  nUU   nXU      nUU   nUR'                  UUUS
.5        Ms     U$ )au  
Converts the raw output of [`LightGlueKeypointMatchingOutput`] into lists of keypoints, scores and descriptors
with coordinates absolute to the original image sizes.
Args:
    outputs ([`LightGlueKeypointMatchingOutput`]):
        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)r6   N)r-   )r:   target_sizes     r(   r;   ILightGlueImageProcessor.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   r6   N)
keypoints0
keypoints1matching_scores)maskr.   r-   rE   rC   rA   rB   rJ   tensorr   	keypointscloneflipreshapetoint32zipmatchesr   append)rZ   r   r   r   image_pair_sizesr   results	mask_pairkeypoints_pairr   scoresmask0mask1r   r   matches0scores0valid_matchesmatched_keypoints0matched_keypoints1r   s                        r(   post_process_keypoint_matching6LightGlueImageProcessor.post_process_keypoint_matching   s   , <<a C$55qrrILIIIsttlD))$||LATATU!!!$)\-?-?-Ba-G j   ,%%++-	 5 5b 9 A A"aA NN	LL-	:=LL)__QT%:G<S<STUWXTX<Y;
6Iw aL1$EaL1$E'*51J'*51J~HmG %y0X]CxRbRbcdReGefM!+M!:!+],C!D%m4ONN"4"4'6#;
2 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   r6   Nr   r   )dtyper   r   r   )fillrU   black)r   )rG   r   ru   r-   r   r.   rJ   zerosmaxuint8
from_numpyr   	fromarraynumpyr   Drawunbind
_get_colorlineellipser   )rZ   r2   r   r0   r}   r~   r   rD   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_matching3LightGlueImageProcessor.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)rZ   scorergbs        r(   r   "LightGlueImageProcessor._get_color:  s1    q5y!"eQwr'   r   )T)g        )'r   r    r!   r"   r   valid_kwargsr   BILINEARrh   rg   default_to_squarerf   ri   rj   do_normalizer   r   rY   r   r   r	   r_   rc   rB   r$   r   floatstrr   r   tupledictrJ   Tensorr   r   r   r&   __classcell__)r[   s   @r(   rR   rR   x   s   0L!**HC(DIJNLL#(E!F # 4 4v>[7\ 4am 4 477 
	7& ")^^$)^ )^ 	)^
 L)^ )^ )^ +)^ j(4/)^ )^ 
)^^ 	B2B !4;.B 	B
 
d3$%	&BH5 #'tC,='>"?5 
m		5n r'   rR   )r0   re   )r0   re   r]   re   )(typingr   rJ   r,   r   r   torchvision.transforms.v2r   rN   image_processing_backendsr   image_processing_utilsr	   image_transformsr
   r   image_utilsr   r   r   r   r   r   r   r   processing_utilsr   r   utilsr   r   modeling_lightgluer   r   r1   rG   rK   rP   rR   __all__r   r'   r(   <module>r      s   ( !    7 ; 2 E	 	 	 5 / CL $J $8>>>& F0 F FR %
%r'   