o
    oiR-                     @   s   d dl mZmZmZmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZ ddgZdee	 d	e	fd
dZG dd dZG dd deZG dd dZdS )    )ListOptionalTupleUnioncastN)Size)Tensor)transform_points	KeypointsKeypoints3D	keypointsreturnc                 C      t NNotImplementedError)r    r   M/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/keypoints.py_merge_keypoint_list   s   r   c                	   @   s  e Zd ZdZd2deeee f deddfddZd	ee	e
ef dd fd
dZd	ee	e
ef dd dd fddZedeee
df ef fddZedefddZedejfddZedejfddZ	d3deeedf ee f deed f dedd fddZdedd fdd Zdedd fd!d"Zd3d#ededd fd$d%Zd#edd fd&d'Zededd fd(d)Zd3d*edeeee f fd+d,Zd4d-d.Zd/ejdd fd0d1ZdS )5r
   z2D Keypoints containing Nx2 or BxNx2 points.

    Args:
        keypoints: Raw tensor or a list of Tensors with the Nx2 coordinates
        raise_if_not_floating_point: will raise if the Tensor isn't float

    Tr   raise_if_not_floating_pointr   Nc                 C      d | _ t|trt|\}| _ t|tstdt| d| s0|r,td|j	 |
 }t|jdkr<|d}d|j  krGdkrRn n	|jdd  d	ks[td
|j d|jdkrbdnd| _|| _d S )N&Input keypoints is not a Tensor. Got: .+Coordinates must be in floating point. Got r   )   r      r   )r   z1Keypoints shape must be (N, 2) or (B, N, 2). Got FT_N
isinstancelistr   r   	TypeErrortypeis_floating_point
ValueErrordtypefloatlenshapereshapendim_is_batched_dataselfr   r   r   r   r   __init__*      


,
zKeypoints.__init__keyc                 C      t | | j| d}|S NFr"   r,   r.   r1   new_objr   r   r   __getitem__E      zKeypoints.__getitem__valuec                 C      |j | j |< | S r   r,   r.   r1   r9   r   r   r   __setitem__I      zKeypoints.__setitem__.c                 C      | j jS r   datar(   r.   r   r   r   r(   M      zKeypoints.shapec                 C      | j S r   r;   rB   r   r   r   rA   Q      zKeypoints.datac                 C   r?   )zReturns keypoints device.)r,   devicerB   r   r   r   rF   U      zKeypoints.devicec                 C   r?   )zReturns keypoints dtype.)r,   r%   rB   r   r   r   r%   Z   rG   zKeypoints.dtypeFindicesvaluesinplacec                 C   sV   |r| j }n| j  }t|tr|||j n||| |r"| S |  }||_ |S r   )r,   cloner   r
   
index_put_rA   )r.   rH   rI   rJ   r,   objr   r   r   	index_put_   s   

zKeypoints.index_putpadding_sizec                 C   sn   t |jdkr|ddkstd|j d| jd  |dddf 7  < | jd	  |ddd
f 7  < | S zSPad a bounding keypoints.

        Args:
            padding_size: (B, 4)

        r         z%Expected padding_size as (B, 4). Got r   ).r   .N).rQ   r   r'   r(   sizeRuntimeErrorr,   r.   rO   r   r   r   padv   
   zKeypoints.padc                 C   sn   t |jdkr|ddkstd|j d| jd  |dddf 8  < | jd	  |ddd
f 8  < | S rP   rS   rV   r   r   r   unpad   rX   zKeypoints.unpadMc                 C   sb   d|j   krdkrn n	|jdd dkrtd|j dt|| j}|r,|| _| S t|dS )	&  Apply a transformation matrix to the 2D keypoints.

        Args:
            M: The transformation matrix to be applied, shape of :math:`(3, 3)` or :math:`(B, 3, 3)`.
            inplace: do transform in-place and return self.

        Returns:
            The transformed keypoints.

        r   r   N)r   r   zAThe transformation matrix shape must be (3, 3) or (B, 3, 3). Got r   F)r*   r(   r$   r	   r,   r
   )r.   rZ   rJ   transformed_boxesr   r   r   transform_keypoints   s   ,
zKeypoints.transform_keypointsc                 C      | j |ddS z9Inplace version of :func:`Keypoints.transform_keypoints`.TrJ   r^   r.   rZ   r   r   r   transform_keypoints_      zKeypoints.transform_keypoints_c                 C      | |S r   r   clsr   r   r   r   from_tensor   rC   zKeypoints.from_tensoras_padded_sequencec                 C      |rt | jS )a  Cast :class:`Keypoints` to a tensor.

        ``mode`` controls which 2D keypoints format should be use to represent keypoints in the tensor.

        Args:
            as_padded_sequence: whether to keep the pads for a list of keypoints. This parameter is only valid
                if the keypoints are from a keypoint list.

        Returns:
            Keypoints tensor :math:`(B, N, 2)`

        r   r,   r.   rj   r   r   r   	to_tensor      zKeypoints.to_tensorc                 C      t | j dS r3   )r
   r,   rK   rB   r   r   r   rK         zKeypoints.cloner%   c                 C   s   | j || _ | S r   )r,   r"   )r.   r%   r   r   r   r"      s   zKeypoints.typeTF)r   r
   )__name__
__module____qualname____doc__r   r   r   boolr/   sliceintr7   r=   propertyr   r   r(   rA   torchrF   r%   rN   rW   rY   r^   rd   classmethodri   rn   rK   r"   r   r   r   r   r
   !   s@    $  

 
c                       sz   e Zd ZU eed< eddeeee f de	dd fddZ
def fdd	Zddede	dd f fddZdddZ  ZS )VideoKeypointstemporal_channel_sizeTboxesvalidate_boxesr   c                 C   sj   t |tfs| dks|jd dkrtd|d}| ||d|d d|d}||_|S )NrR   r   r   zOInput box type is not yet supported. Please input an `BxTxNx2` tensor directly.rQ   r   r   )r   r    dimr(   r$   rT   viewr   )rh   r   r   r   outr   r   r   ri      s   &
(zVideoKeypoints.from_tensorc                    s8   t  jdd}tt|}|jd| jg|jdd  R  S )NF)rj   r   rQ   )superrn   r   r   r   r   r(   r.   r   	__class__r   r   rn      s   
 zVideoKeypoints.to_tensorFrZ   rJ   c                    s0   t  j||d}|r| S t|jd}| j|_|S )Nra   F)r   r^   r~   rA   r   )r.   rZ   rJ   r   r   r   r   r^      s   z"VideoKeypoints.transform_keypointsc                 C   s   t | j d}| j|_|S r3   )r~   r,   rK   r   r   r   r   r   rK      s   zVideoKeypoints.clonerr   rs   )r   r~   )rt   ru   rv   rz   __annotations__r}   r   r   r   rx   ri   rn   r^   rK   __classcell__r   r   r   r   r~      s   
 &r~   c                   @   s"  e Zd ZdZd&deeee f deddfddZd	ee	e
ef dd fd
dZd	ee	e
ef dd dd fddZedefddZedefddZdedd fddZdedd fddZd'dededd fddZdedd fddZededd fdd Zd'd!edeeee f fd"d#Zd(d$d%ZdS ))r   z3D Keypoints containing Nx3 or BxNx3 points.

    Args:
        keypoints: Raw tensor or a list of Tensors with the Nx3 coordinates
        raise_if_not_floating_point: will raise if the Tensor isn't float

    Tr   r   r   Nc                 C   r   )Nr   r   r   r   )r   r   r   r   r   )r   z1Keypoints shape must be (N, 3) or (B, N, 3). Got FTr   r-   r   r   r   r/      r0   zKeypoints3D.__init__r1   c                 C   r2   r3   r4   r5   r   r   r   r7     r8   zKeypoints3D.__getitem__r9   c                 C   r:   r   r;   r<   r   r   r   r=     r>   zKeypoints3D.__setitem__c                 C   r?   r   r@   rB   r   r   r   r(     rC   zKeypoints3D.shapec                 C   rD   r   r;   rB   r   r   r   rA     rE   zKeypoints3D.datarO   c                 C   r   zSPad a bounding keypoints.

        Args:
            padding_size: (B, 6)

        r   rV   r   r   r   rW        zKeypoints3D.padc                 C   r   r   r   rV   r   r   r   rY   &  r   zKeypoints3D.unpadFrZ   rJ   c                 C   r   )r[   r   )r.   rZ   rJ   r   r   r   r^   /  s   zKeypoints3D.transform_keypointsc                 C   r_   r`   rb   rc   r   r   r   rd   <  re   z Keypoints3D.transform_keypoints_c                 C   rf   r   r   rg   r   r   r   ri   @  rC   zKeypoints3D.from_tensorrj   c                 C   rk   )a  Cast :class:`Keypoints` to a tensor.

        ``mode`` controls which 2D keypoints format should be use to represent keypoints in the tensor.

        Args:
            as_padded_sequence: whether to keep the pads for a list of keypoints. This parameter is only valid
                if the keypoints are from a keypoint list.

        Returns:
            Keypoints tensor :math:`(B, N, 3)`

        rl   rm   r   r   r   rn   D  ro   zKeypoints3D.to_tensorc                 C   rp   r3   )r   r,   rK   rB   r   r   r   rK   U  rq   zKeypoints3D.clonerr   rs   )r   r   )rt   ru   rv   rw   r   r   r   rx   r/   ry   rz   r7   r=   r{   r   r(   rA   rW   rY   r^   rd   r}   ri   rn   rK   r   r   r   r   r      s"    $ 		 )typingr   r   r   r   r   r|   r   kornia.corer   kornia.geometryr	   __all__r   r
   r~   r   r   r   r   r   <module>   s    &#