Ë
    ÜÓ^jº  ã                   óè   — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ dd	lmZmZ dd
lmZmZ erddlmZ ddlmZ  G d„ ded¬«      Zdddefd„Zdd„Ze G d„ de«      «       ZdgZy)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                   ó   — e Zd ZU dZeed<   y)ÚSuperPointImageProcessorKwargszÀ
    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/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/superpoint/image_processing_superpoint.pyr   r   "   s   … ñð
 Ôr   r   F)ÚtotalÚimageútorch.TensorÚreturnc           	      ó$  — | j                   dk  s#| j                  | j                   dk(  rdnd   dk(  ryt        j                  | dddd…dd…f   | dddd…dd…f   k(  «      xr. t        j                  | dddd…dd…f   | dddd…dd…f   k(  «      S )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�   € à‡z�z�A‚~˜Ÿ™¨%¯*©*¸ª/¡Q¸qÑAÀQÒFØÜ�9‰9�U˜3 ¢1¢a˜<Ñ(¨E°#°qº!ºQ°,Ñ,?Ñ?Ó@ò ÄUÇYÁYØˆc�1’ašˆlÑ˜u S¨!ªQ² \Ñ2Ñ2óFð r   c                 óJ   — t        | «      r| S t        j                  | d¬«      S )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_grayscaler.   4   s$   € ô �EÔØˆÜ×Ñ ¸1Ô=Ð=r   c                   óä   ‡ — e Zd ZeZej                  ZdddœZdZ	dZ
dZdZdZdZdee   fˆ fd	„Z	 dd
ed   dededddedededz  deez  dz  dedefd„Zdddeee   z  deeedf      fd„Zˆ xZS )ÚSuperPointImageProcessorià  i€  )ÚheightÚwidthFTgp?NÚkwargsc                 ó$   •— t        ‰| �  di |¤Ž y )Nr   )ÚsuperÚ__init__)Úselfr3   Ú	__class__s     €r   r6   z!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
                 ó  — t        ||¬«      \  }}i }|j                  «       D ]A  \  }}|	rt        |«      }|r| j                  |||¬«      }|r| j	                  ||«      }|||<   ŒC t        ||«      }t        d|i|¬«      S )N)r?   )r;   r<   Úpixel_values)ÚdataÚtensor_type)r   Úitemsr.   ÚresizeÚrescaler   r   )r7   r9   r:   r;   r<   r=   r>   r?   r@   r   r3   Úgrouped_imagesÚgrouped_images_indexÚprocessed_images_groupedr%   Ústacked_imagesÚprocessed_imagess                    r   Ú_preprocessz$SuperPointImageProcessor._preprocessT   s£   € ô 0EÀVÐ^nÔ/oÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ó%;ò 
	=Ñ!ˆE�>áÜ!5°nÓ!E�áØ!%§¡¨^À$ÐQY Ó!Z�áØ!%§¡¨n¸nÓ!M�Ø.<Ð$ UÒ+ð
	=ô *Ð*BÐDXÓYÐÜ .Ð2BÐ!CÐQ_Ô`Ð`r   Úoutputsr   Útarget_sizesc                 ó¬  — t        |j                  «      t        |«      k7  rt        d«      ‚t        |t        «      r,t        j                  ||j                  j                  ¬«      }n|j                  d   dk7  rt        d«      ‚|}t        j                  |dg«      }|j                  |dd…df   z  }|j                  t
        j                  «      }g }t        |j                  ||j                  |j                  «      D ]O  \  }}}}	t        j                   |«      j#                  d«      }
||
   }||
   }|	|
   }	|j%                  |||	dœ«       ŒQ |S )a»  
        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&   ÚtensorrQ   r%   ÚfliprR   ÚtoÚint32ÚziprS   rT   ÚnonzeroÚsqueezeÚappend)r7   rN   rO   Úimage_sizesÚmasked_keypointsÚresultsÚ
image_maskrR   rS   rT   Úindicess              r   Úpost_process_keypoint_detectionz8SuperPointImageProcessor.post_process_keypoint_detectionr   sH  € ô$ ˆw�|‰|Ó¤ LÓ 1Ò1ÜÐqÓrÐrä�l¤DÔ)ÜŸ,™, |¸G¿L¹L×<OÑ<OÔP‰Kà×!Ñ! !Ñ$¨Ò)Ü Øjóð ð 'ˆKô —j‘j ¨q¨cÓ2ˆØ"×,Ñ,¨{º1¸d¸7Ñ/CÑCÐð ,×.Ñ.¬u¯{©{Ó;ÐàˆÜ:=Ø�L‰LÐ*¨G¯N©N¸G×<OÑ<Oó;
ò 	cÑ6ˆJ˜	 6¨;ô —m‘m JÓ/×7Ñ7¸Ó:ˆGØ! 'Ñ*ˆIØ˜G‘_ˆFØ% gÑ.ˆKØ�N‰N¨¸fÐU`ÑaÕbð	cð ˆr   )F)r   r   r   r   Úvalid_kwargsr	   ÚBILINEARr<   r;   Údefault_to_squarer:   r=   r>   Údo_normalizer   r   r6   rY   r   r
   ÚfloatÚstrr   r   rM   ÚtupleÚdictrg   Ú__classcell__)r8   s   @r   r0   r0   E   s  ø„ à1€LØ!×*Ñ*€HØ CÑ(€DØÐØ€IØ€JØ€NØ€LØ€Lð# Ð(FÑ!Gõ #ð #ñaà�^Ñ$ðað ðað ð	að
 Lðað ðað ðað  ™+ðað ˜jÑ(¨4Ñ/ðað ðað 
óað</Ø<ð/ØLVÐY]Ð^cÑYdÑLdð/à	ˆd�3˜Ð&Ñ'Ñ	(÷/r   r0   )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)   r.   r0   Ú__all__r   r   r   ú<module>r{      s}   ðñ ,å  ã å ;Ý 2ß Eß 7ß 4ß /ñ ÝHå 7ô \¸õ ð˜ð ¨4ó ó>ð" ô[Ð1ó [ó ð[ð| &Ð
&�r   