
    Z j                         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  SS	KJrJr  SS
KJrJr  \(       a  SSKJr  SSKJr   " S S\SS9rSSS\4S jrSS jr\ " S S\5      5       rS/rg)z%Image processor class for SuperPoint.    )TYPE_CHECKINGN   )TorchvisionBackend)BatchFeature)group_images_by_shapereorder_images)PILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstring   )#SuperPointKeypointDescriptionOutput)
functionalc                   $    \ rS rSr% Sr\\S'   Srg)SuperPointImageProcessorKwargs"   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/superpoint/image_processing_superpoint.pyr   r   "   s    
 r   r   F)totalimagetorch.Tensorreturnc           	      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.N   )ndimshapetorchallr"   s    r    is_grayscaler,   +   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_grayscaler1   4   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   S\S\SSS\S\S\S-  S\\-  S-  S\S\4S jjrSSS\\\   -  S\\\S4      4S jrSrU =r$ )SuperPointImageProcessorE   i  i  )heightwidthFTgp?Nkwargsc                 &   > [         TU ]  " S0 UD6  g )Nr   )super__init__)selfr7   	__class__s     r    r:   !SuperPointImageProcessor.__init__Q   s    "6"r   imagesr#   	do_resizesizeresamplez7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordisable_groupingreturn_tensorsr   r$   c
                    [        XS9u  p0 nUR                  5        HJ  u  pU	(       a  [        U5      nU(       a  U R                  XUS9nU(       a  U R	                  X5      nXU'   ML     [        X5      n[        SU0US9$ )N)rD   )r@   rA   pixel_values)datatensor_type)r   itemsr1   resizerescaler   r   )r;   r>   r?   r@   rA   rB   rC   rD   rE   r   r7   grouped_imagesgrouped_images_indexprocessed_images_groupedr(   stacked_imagesprocessed_imagess                    r    _preprocess$SuperPointImageProcessor._preprocessT   s     0EV/o,#% %3%9%9%;!E!5n!E!%^QY!Z!%n!M.<U+ &< **BY.2B!CQ_``r   outputsr   target_sizesc                    [        UR                  5      [        U5      :w  a  [        S5      e[        U[        5      (       a)  [
        R                  " X!R                  R                  S9nO UR                  S   S:w  a  [        S5      eUn[
        R                  " US/5      nUR                  USS2S4   -  nUR                  [
        R                  5      n/ n[        UR                  XAR                  UR                  5       HK  u  pgp[
        R                   " U5      R#                  S5      n
Xz   nX   nX   n	UR%                  XxU	S.5        MM     U$ )aS  
Converts the raw output of [`SuperPointForKeypointDetection`] into lists of keypoints, scores and descriptors
with coordinates absolute to the original image sizes.

Args:
    outputs ([`SuperPointKeypointDescriptionOutput`]):
        Raw outputs of the model containing keypoints in a relative (x, y) format, with scores and descriptors.
    target_sizes (`torch.Tensor` or `list[tuple[int, int]]`):
        Tensor of shape `(batch_size, 2)` or list 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).
Returns:
    `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in absolute format according
    to target_sizes, scores and descriptors for an image in the batch as predicted by the model.
zRMake sure that you pass in as many target sizes as the batch dimension of the mask)devicer   r&   zTEach element of target_sizes must contain the size (h, w) of each image of the batchN)	keypointsscoresdescriptors)lenmask
ValueError
isinstancelistr)   tensorrW   r(   fliprX   toint32ziprY   rZ   nonzerosqueezeappend)r;   rT   rU   image_sizesmasked_keypointsresults
image_maskrX   rY   rZ   indicess              r    post_process_keypoint_detection8SuperPointImageProcessor.post_process_keypoint_detectionr   s4   $ w||L 11qrrlD)),,|LL<O<OPK!!!$) j  'K jjqc2",,{1d7/CC ,..u{{;:=LL*NNG<O<O;
6J6 mmJ/77:G!*I_F%.KNNU`ab;
 r   r   )F)r   r   r   r   r   valid_kwargsr	   BILINEARrA   r@   default_to_squarer?   rB   rC   do_normalizer   r   r:   r_   r   r
   floatstrr   r   rR   tupledictrm   r   __classcell__)r<   s   @r    r3   r3   E   s   1L!**HC(DIJNLL#(F!G # #a^$a a 	a
 La a a +a j(4/a a 
a</</LVY]^cYdLd/	d3&'	(/ /r   r3   )r"   r#   r$   r#   )r   typingr   r)   image_processing_backendsr   image_processing_utilsr   image_transformsr   r   image_utilsr	   r
   processing_utilsr   r   utilsr   r   modeling_superpointr   torchvision.transforms.v2r   r/   r   r   r,   r1   r3   __all__r   r   r    <module>r      s}    ,    ; 2 E 7 4 / H 7\  4 >" [1 [ [| &
&r   