
    Z j2                     L   S r SSKJr  SSK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  \(       a  SSKJr  \" 5       (       a  SSKJrJr  SSK J!r"  S r#S\4S jr$  SS jr%    SS jr& " S S\SS9r'\ " S S\5      5       r(S/r)g)z$Image processor class for SuperGlue.    )TYPE_CHECKINGN   )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_vision_available   )SuperGlueKeypointMatchingOutput)Image	ImageDraw)
functionalc                     [        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    ډ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/superglue/image_processing_superglue.py_is_valid_imager#   2   sF     ub."79=="HbSQVQ\Q\M]abMb    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"   r#   8validate_and_format_image_pairs.<locals>._is_valid_imageA   sG    E" 
5!fnU&;y}}&LfQTUZU`U`QaefQf	
r$      c              3   4   >#    U  H  nT" U5      v   M     g 7fN .0r!   r#   s     r"   	<genexpr>2validate_and_format_image_pairs.<locals>.<genexpr>H   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)r)   c              3   4   >#    U  H  nT" U5      v   M     g 7fr+   r,   r-   s     r"   r/   <validate_and_format_image_pairs.<locals>.<genexpr>.<genexpr>M   s     C
uOE**
r1   N)
isinstancelistr   all)r.   
image_pairr#   s     r"   r/   r0   J   sO      
 %
 z4( DJ1$DC
CCD %s   AA)r5   r6   r   r7   
ValueError)r%   error_messager8   r!   r#   s       @r"   validate_and_format_image_pairsr;   8   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.Nr)   )ndimr   torchr7   r    s    r"   is_grayscaler?   T   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)r?   tvFrgb_to_grayscaler    s    r"   convert_to_grayscalerD   _   s'     E1==r$   c                   $    \ rS rSr% Sr\\S'   Srg)SuperGlueImageProcessorKwargsr   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_grayscaler,   N)__name__
__module____qualname____firstlineno____doc__bool__annotations____static_attributes__r,   r$   r"   rF   rF   r   s    
 r$   rF   F)totalc                     ^  \ 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&$ )&SuperGlueImageProcessor{   i  i  )heightwidthFTgp?Nkwargsc                 &   > [         TU ]  " S0 UD6  g )Nr,   )super__init__)selfrW   	__class__s     r"   rZ    SuperGlueImageProcessor.__init__   s    "6"r$   r%   returnc                 &   > [         TU ]  " U40 UD6$ r+   )rY   
preprocess)r[   r%   rW   r\   s      r"   r`   "SuperGlueImageProcessor.preprocess   s    w!&3F33r$   c                 :    U R                  U5      n[        U5      $ r+   )fetch_imagesr;   )r[   r%   rW   s      r"   _prepare_images_structure1SuperGlueImageProcessor._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_tensorsrH   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   r)   )dimpixel_values)datatensor_type)r   itemsresizer   rescalerD   ranger   r>   stackr   )r[   r%   rg   rh   ri   rj   rk   rl   rm   rH   rW   grouped_imagesgrouped_images_indexprocessed_images_groupedr   stacked_imagesresized_imagesprocessed_imagesiimage_pairspairstacked_pairss                         r"   _preprocess#SuperGlueImageProcessor._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 [`SuperGlueKeypointMatchingOutput`] into lists of keypoints, scores and descriptors
with coordinates absolute to the original image sizes.
Args:
    outputs ([`SuperGlueKeypointMatchingOutput`]):
        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)r)   N)r   )r.   target_sizes     r"   r/   ISuperGlueImageProcessor.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   r)   N)
keypoints0
keypoints1matching_scores)maskr   r   r9   r7   r5   r6   r>   tensorr   	keypointscloneflipreshapetoint32zipmatchesr   append)r[   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_matching6SuperGlueImageProcessor.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   r)   Nr   r   )dtyper   r   r   )fillrV   black)r   )r;   r   rv   r   r   r   r>   zerosmaxuint8
from_numpyr   	fromarraynumpyr   Drawunbind
_get_colorlineellipser   )r[   r%   r   r!   r~   r   r   r8   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_matching3SuperGlueImageProcessor.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   "SuperGlueImageProcessor._get_color=  s1    q5y!"eQwr$   r,   )T)g        )'rI   rJ   rK   rL   rF   valid_kwargsr   BILINEARri   rh   default_to_squarerg   rj   rk   do_normalizerH   r   rZ   r   r	   r   r`   rd   r6   rN   r   floatstrr   r   tupledictr>   Tensorr   r   r   rP   __classcell__)r\   s   @r"   rS   rS   {   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$   rS   )r!   rf   )r!   rf   r^   rf   )*rM   typingr   r>   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr	   r
   r   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   modeling_supergluer   r   r   r   torchvision.transforms.v2r   rB   r#   r;   r?   rD   rF   rS   __all__r,   r$   r"   <module>r      s    +    ; 2 E	 	 	 5  C$ 7$J $8>>>&L  F0 F FR %
%r$   