o
    oi                     @  s   d dl mZ d dlmZ d dlZd dlmZmZ d dlm	Z	m
Z
mZ ddlmZmZ z
d dlZd dlZW n eyA   dZdZY nw dgZG d	d dZdS )
    )annotations)ClassVarN)DeviceTensor)KORNIA_CHECKKORNIA_CHECK_SAME_DEVICESKORNIA_CHECK_SHAPE   )download_onnx_from_urlnormalize_keypointsOnnxLightGluec                   @  sX   e Zd ZU dZdddddZded< d	d
gZded< ddddZd ddZd ddZ	dS )!r   a  Wrapper for loading LightGlue-ONNX models and running inference via ONNXRuntime.

    LightGlue :cite:`LightGlue2023` performs fast descriptor-based deep keypoint matching.
    This module requires `onnxruntime` to be installed.

    If you have trained your own LightGlue model, see https://github.com/fabio-sim/LightGlue-ONNX
    for how to export the model to ONNX and optimize it.

    Args:
        weights: Pretrained weights, or a path to your own exported ONNX model. Available pretrained weights
          are ``'disk'``, ``'superpoint'``, ``'disk_fp16'``, and ``'superpoint_fp16'``. `Note that FP16 requires CUDA.`
          Defaults to ``'disk_fp16'`` if ``device`` is CUDA, and ``'disk'`` if CPU.
        device: Device to run inference on.

    z^https://github.com/fabio-sim/LightGlue-ONNX/releases/download/v1.0.0/disk_lightglue_fused.onnxzdhttps://github.com/fabio-sim/LightGlue-ONNX/releases/download/v1.0.0/superpoint_lightglue_fused.onnxzchttps://github.com/fabio-sim/LightGlue-ONNX/releases/download/v1.0.0/disk_lightglue_fused_fp16.onnxzihttps://github.com/fabio-sim/LightGlue-ONNX/releases/download/v1.0.0/superpoint_lightglue_fused_fp16.onnx)disk
superpoint	disk_fp16superpoint_fp16zClassVar[dict[str, str]]
MODEL_URLSimage0image1zClassVar[list[str]]required_data_keysNcpuweights
str | Nonedevicer   returnNonec                 C  s   t td ud t td ud t|}|| _|jdkrdg}n|jdkr)ddg}ntd| |d u r=|jdkr;dnd	}|| jv rbd
|v rNt |jdkd | j| }|jdkr^|dd}t	|}tj
||d| _d S )Nzonnxruntime is not installed.znumpy is not installed.r   CPUExecutionProvidercudaCUDAExecutionProviderzUnsupported device r   r   fp16zFP16 requires CUDA.z.onnxz	_cpu.onnx)	providers)r   ortnptorchr   type
ValueErrorr   replacer
   InferenceSessionsession)selfr   r   r   url r*   [/home/ubuntu/.local/lib/python3.10/site-packages/kornia/feature/lightglue_onnx/lightglue.py__init__A   s&   






zOnnxLightGlue.__init__datadict[str, dict[str, Tensor]]dict[str, Tensor]c                 C  s
   |  |S )N)forward)r(   r-   r*   r*   r+   __call__^   s   
zOnnxLightGlue.__call__c              	   C  s  | j D ]}t||v d| d q|d |d }}|d  |d  }}|d  |d  }}t||||gd t|jj| jjkd ttj|j  koa|j  koa|j  koa|jkn  d t	|g d	 t	|g d
 t	|g d t	|g d t|j
d |j
d kd t|j
d |j
d kd t|j
d |j
d kd |d|d}	}
|	dur|	n|d j
dd ddd }	|
dur|
n|d j
dd ddd }
t||	d}t||
d}tt|dk ot|dk d tt|dk ot|dk d ||||d}ddg}| j }| D ]\}}|j|| jjdtjt|j
| d q'|D ]}|j|| jjdd qB| j| | \}}t| | jt| | jd}|S )aV  Match keypoints and descriptors between two images.

        The output contains the matches (the indices of the matching keypoint pairs between the first and second image)
        and the corresponding confidence scores.
        Only a batch size of 1 is supported.

        Args:
            data: Dictionary containing both images and the keypoints and descriptors thereof.

        Returns:
            Dictionary containing the matches and scores.

        ``data`` (``dict``):
            ``image0`` (``dict``):
                ``keypoints`` (`float32`): :math:`(1, M, 2)`

                ``descriptors`` (`float32`): :math:`(1, M, D)`

                ``image``: :math:`(1, C, H, W)` or ``image_size``: :math:`(1, 2)`

            ``image1`` (``dict``):
                ``keypoints`` (`float32`): :math:`(1, N, 2)`

                ``descriptors`` (`float32`): :math:`(1, N, D)`

                ``image``: :math:`(1, C, H, W)` or ``image_size``: :math:`(1, 2)`

        ``output`` (``dict``):
            ``matches`` (`int64`): :math:`(S, 2)`

            ``scores`` (`float32`): :math:`(S)`

        zMissing key z in datar   r   	keypointsdescriptorszWrong devicezWrong dtype)1M2)r4   Nr6   )r4   r5   D)r4   r7   r8   r	   z8Number of keypoints does not match number of descriptors   z$Descriptors' dimensions do not match
image_sizeNimage)size )kpts0kpts1desc0desc1matches0mscores0r   )device_type	device_idelement_typeshape
buffer_ptr)rF   rG   )matchesscores)r   r   
contiguousr   r   r#   r"   float32dtyper   rI   getr   allitemr'   
io_bindingitems
bind_inputr!   tupledata_ptrbind_outputrun_with_iobindingget_outputsfrom_dlpacknumpyto)r(   r-   keydata0data1kpts0_kpts1_rB   rC   size0size1r@   rA   lightglue_inputslightglue_outputsbindingnametensorrK   mscoresoutputsr*   r*   r+   r0   a   sT   
#<((*,

	zOnnxLightGlue.forward)Nr   )r   r   r   r   r   r   )r-   r.   r   r/   )
__name__
__module____qualname____doc__r   __annotations__r   r,   r1   r0   r*   r*   r*   r+   r   '   s   
 
)
__future__r   typingr   r"   kornia.corer   r   kornia.core.checkr   r   r   utilsr
   r   r\   r!   onnxruntimer    ImportError__all__r   r*   r*   r*   r+   <module>   s   