o
    oi                     @   st   d dl mZmZmZmZmZ d dlm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 G dd de
Zd	S )
    )AnyDictOptionalTupleUnion)random_generator)GeometricAugmentationBase2D)Resample)Tensor	as_tensor)get_perspective_transformwarp_perspectivec                       s  e Zd ZdZdejjdddddfdeee	f dee
eef deded	e	d
ede
ddf fddZdedee
ef dee
ef defddZ	ddedee
ef dee
ef dee def
ddZ		ddedee
ef dee deeeef  def
ddZ  ZS )RandomPerspectivea<  Apply a random perspective transformation to an image tensor with a given probability.

    .. image:: _static/img/RandomPerspective.png

    Args:
        distortion_scale: the degree of distortion, ranged from 0 to 1.
        resample: the interpolation method to use.
        same_on_batch: apply the same transformation across the batch. Default: False.
        align_corners: interpolation flag.
        p: probability of the image being perspectively transformed.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it
                 to the batch form (False).
        sampling_method: ``'basic'`` | ``'area_preserving'``. Default: ``'basic'``
            If ``'basic'``, samples by translating the image corners randomly inwards.
            If ``'area_preserving'``, samples by randomly translating the image corners in any direction.
            Preserves area on average. See https://arxiv.org/abs/2104.03308 for further details.

    Shape:
        - Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`, Optional: :math:`(B, 3, 3)`
        - Output: :math:`(B, C, H, W)`

    .. note::
        This function internally uses :func:`kornia.geometry.transform.warp_pespective`.

    Examples:
        >>> rng = torch.manual_seed(0)
        >>> inputs= torch.tensor([[[[1., 0., 0.],
        ...                         [0., 1., 0.],
        ...                         [0., 0., 1.]]]])
        >>> aug = RandomPerspective(0.5, p=0.5)
        >>> out = aug(inputs)
        >>> out
        tensor([[[[0.0000, 0.2289, 0.0000],
                  [0.0000, 0.4800, 0.0000],
                  [0.0000, 0.0000, 0.0000]]]])
        >>> aug.inverse(out)
        tensor([[[[0.0500, 0.0961, 0.0000],
                  [0.2011, 0.3144, 0.0000],
                  [0.0031, 0.0130, 0.0053]]]])

    To apply the exact augmenation again, you may take the advantage of the previous parameter state:
        >>> input = torch.randn(1, 3, 32, 32)
        >>> aug = RandomPerspective(0.5, p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    g      ?Fbasicdistortion_scaleresamplesame_on_batchalign_cornerspkeepdimsampling_methodreturnNc                    s8   t  j|||d tj||d| _|t|d| _d S )N)r   r   r   )r   )r   r   )super__init__rgPerspectiveGenerator_param_generatorr	   getflags)selfr   r   r   r   r   r   r   	__class__ a/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/geometric/perspective.pyr   L   s   
zRandomPerspective.__init__inputparamsr   c                 C   s   t |d ||d |S )Nstart_points
end_points)r   to)r   r$   r%   r   r"   r"   r#   compute_transformation[   s   z(RandomPerspective.compute_transformation	transformc                 C   sP   |j \}}}}t|tstdt| dt||||f|d j |d dS )N*Expected the `transform` be a Tensor. Got .r   r   )moder   )shape
isinstancer
   	TypeErrortyper   namelower)r   r$   r%   r   r*   _heightwidthr"   r"   r#   apply_transform^   s   
z!RandomPerspective.apply_transformsizec                 C   s@   t |tstdt| d| j|| jt||j|jd|dS )Nr+   r,   )devicedtype)r%   r*   r   )	r/   r
   r0   r1   r7   _paramsr   r9   r:   )r   r$   r   r*   r8   r"   r"   r#   inverse_transformi   s   
z#RandomPerspective.inverse_transform)N)NN)__name__
__module____qualname____doc__r	   BILINEARr2   r   r
   floatstrintboolr   r   r   r)   r   r7   r   r<   __classcell__r"   r"   r    r#   r      sh    2
	*



r   N)typingr   r   r   r   r   kornia.augmentationr   r   &kornia.augmentation._2d.geometric.baser   kornia.constantsr	   kornia.corer
   r   kornia.geometry.transformr   r   r   r"   r"   r"   r#   <module>   s   