o
    *Îj  ã                   @   s^   d dl Zd dlZd dlm  mZ d dlmZ ddlm	Z	 ddl
mZmZ dd„ Zd	d
„ ZdS )é    N)Útqdmé   )Úcfgé   )ÚHomoCalcÚHomoProjc                 C   sþ  | j }t t tjdtjjtjj|dtjdtjj	tjj|d¡d¡ 
ddd¡ d¡ ¡ }|t ||gd¡ }g }t|jd ƒD ]O}	t|d ||	 ƒ}
t t tjdtjj|dtjdtjj	|d¡d¡ 
ddd¡ ¡ }t|
| ¡  dd¡ 
dd¡ƒ 
dd¡}| | ¡ j|jŽ  
ddd¡¡ q@t |d¡}|dd…dd…dd…df tjj d d |dd…dd…dd…df< |dd…dd…dd…df tjj	 d d |dd…dd…dd…df< | 
dd	dd¡}tj|||fd
dd}| 
ddd	d¡}tj| |dd}|S )z¯
    @param frame current frame [N, 1, H, W]
    @param x_motion [N, 1, G_H, G_W]
    @param y_motion [N, 1, G_H, G_W]

    @return mesh warping according to given motion
    r   )Údevicer   r   éÿÿÿÿNg       @g      ð?é   ZbilinearT)ÚmodeÚalign_corners)r   )r   ÚtorchÚstackZmeshgridZaranger   ZMODELZWIDTHZPIXELSZHEIGHTZpermuteZ	unsqueezeÚfloatÚcatÚrangeÚshaper   r   Ú
contiguousÚviewÚappendÚFZinterpolateZgrid_sample)ÚframeÚx_motionÚy_motionÚ	cap_widthÚ
cap_heightZtarget_deviceZ	src_gridsZ	des_gridsZ
projectionÚiZhomoZ	origin_kpZprojected_kpZgenerated_frame© r   úy/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/video_stabilization/utils/WarpUtils.pyÚmesh_warp_frame   sp   	ÿÿýúúþüüþþÿ
ÿDÿÿÿýr   c                 C   sz   t  | d¡}t  t  |d¡d¡}t  t  |d¡d¡}t | t j¡¡}t | t j¡¡}t | t j¡¡}t|||||ƒS )zt
    @param images List(image [1, 1, H, W])
    @param x_motion [G_H, G_W, N]
    @param y_motion [G_H, G_W, N]
    r   )r   r   r   r   )	ÚnpZconcatenateZexpand_dimsZ	transposer   Z
from_numpyZastypeZfloat32r   )Zimagesr   r   r   r   Úframesr   r   r   ÚwarpListImageE   s   r"   )Únumpyr    r   Ztorch.nn.functionalÚnnZ
functionalr   r   Z
DUT.configr   ZProjectionUtilsr   r   r   r"   r   r   r   r   Ú<module>   s   8