o
    ߥi!                     @   s   d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ ddlmZ dd	lmZmZ ejee
jd
G dd deZdd Zdd Zdd ZdS )    N)Dict)tqdm)MetricswarpListImage)default_group   )Metric)METRICS
MetricKeys)	group_keymodule_namec                   @   sL   e Zd ZdZdd ZdedefddZdd	 ZdddZdd Z	dd Z
dS )VideoStabilizationMetricz-The metric for video summarization task.
    c                 C   s   g | _ g | _d S Ninputsoutputsself r   a/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/metrics/video_stabilization_metric.py__init__   s   
z!VideoStabilizationMetric.__init__r   r   c                 C   s4   t t|}| j|d  | j|d d  d S )Nvideoinputr   )video_mergerwarpprocessr   appendr   )r   r   r   outr   r   r   add   s   zVideoStabilizationMetric.addc           	      C   s   g }g }g }t | j| jD ],\}}t||\}}}|dkr5|dkr5|dkr5|| || || qtd qtjt|t	| tj
t|t	| tjt|t	| iS )Nr   z.Removed one error item when computing metrics.)zipr   r   metricsr   printr   CROPPING_RATIOsumlenDISTORTION_VALUESTABILITY_SCORE)	r   CRDVSSoutputr   cropping_ratiodistortion_valuestability_scorer   r   r   evaluate$   s   


z!VideoStabilizationMetric.evaluateotherc                 C   s    | j |j  | j|j d S r   )r   extendr   )r   r/   r   r   r   merge8   s   zVideoStabilizationMetric.mergec                 C   s   | j | jfS r   r   r   r   r   r   __getstate__<   s   z%VideoStabilizationMetric.__getstate__c                 C   s   |\| _ | _d S r   r   )r   stater   r   r   __setstate__?   s   z%VideoStabilizationMetric.__setstate__N)r/   r   )__name__
__module____qualname____doc__r   r   r   r.   r1   r2   r4   r   r   r   r   r      s    
r   c                 C   s   | d dddddddf }| d dddddddf }| d dddddddf }| d dddddddf }|| }|| }t | d ||| d | d }|| d	 | d | d | d
 dS )z video stabilization postprocess

    Args:
        inputs:  input data

    Return:
        dict of results:  a dict containing outputs of model.
    origin_motionNr   r   smooth_path
ori_imageswidthheightfpsbase_crop_width)r*   r>   r<   r=   r?   r   )r   x_pathsy_pathssx_pathssy_pathsnew_x_motion_meshesnew_y_motion_meshes
out_imagesr   r   r   r   C   s     	   
r   c                    s   | d   tj  fddt jd D  tjddj}t	j
d }| d }| d	 }| d
 }t	||| d ||f}t D ]-\}}t|| d }	t|	| | }
||
|
 |	|	 f }t	|||f}|| q@|  d|iS )Nr*   c                    s   g | ]
}t  | d qS ))r      r   )np	transpose).0idxrF   r   r   
<listcomp>a   s    z video_merger.<locals>.<listcomp>r   z.mp4)suffixmp4vr<   r=   r?   r>   i   r   )numpyastyperH   uint8rangeshapetempfileNamedTemporaryFilenamecv2VideoWriter_fourccVideoWriter	enumerateintresizewriterelease)r   output_video_pathfourccwhr?   video_writerrK   framehorizontal_bordervertical_border	new_framer   rL   r   r   _   s,   


r   c           -         sp  t  }t  }d}d}d}g }g }td}	g }
t | }t |}| }| }g }g }|rQ|rQ| \}}| \}}|rM|rM|| || |rQ|s3d}t	d| 
dd  d	  ttt|D ]*}|| }|| }||d \ }||d \}|j||d
d}g }|D ]\}}|j||j k r|| qt||krt fdd|D ddd
}tfdd|D ddd
}t j||t j|d\}}nd}|st|d d
 |d d
  }tj|dd
dd
f \} }t| d d d } | d | d  }!|d|  ||! |d t|k r||d  }"||"d \}#|j||#d
d}g }|D ]\}}|j||j k rM|| q;t||krtfdd|D ddd
}tfdd|D ddd
}t j||t j|d\}}|
t|	| t|	|}	qh|rdS g }$g }%|
D ]+}&t|&d d
 |&d d
  }'t|&d |&d d tj }(|$|' |%|( qtj|$})tj|%}*t|)d
 })t|*d
 }*t|)d})t|*d}*|)d t|)d
  })|*d t|*d
  }*t|)d d t|) }+t|*d d t|* },t t!|dgt"t ||+|, d
 fS )N
   gffffff?g      @   Fzprocessing /:rG   )kc                       g | ]} |j  jqS r   queryIdxptrJ   m)
keyPoints1r   r   rM          zmetrics.<locals>.<listcomp>r   c                    ro   r   trainIdxrr   rs   keyPoints1or   r   rM      rv   )methodransacReprojThresholdT)r   r   )r   r   r   c                    ro   r   rp   rs   ry   r   r   rM      rv   c                    ro   r   rw   rs   )keyPoints2or   r   rM      rv   )rl   rl   rl   )r   rG   )r   rG   )r   r         )#rX   	BFMatcherSIFT_createrH   eyeVideoCaptureisOpenedreadr   r!   splitr   rS   r$   detectAndComputeknnMatchdistancefloat32reshapefindHomographyRANSACsqrtlinalgeigsortmatmularctan2pifftabsdeleter#   minmeanabsolute)-
original_vpred_vbfsiftMIN_MATCH_COUNTratiothreshCR_seqDV_seqPtP_seqvc_ovc_prval_orval_pimgs1imgs1oimg1img1ois_got_bad_itemidescriptors1descriptors1omatchesgoodMatchesrt   nsourcePointsdestinationPointsM_scaleRecoveredrb   r(   img2odescriptors2oP_seq_tP_seq_rMptransRecoveredthetaRecoveredfft_tfft_rSS_tSS_rr   )ru   rz   r}   r   r    z   s   







 





r    )ossysrU   typingr   rX   rP   rH   r   modelscope.metainfor   8modelscope.models.cv.video_stabilization.utils.WarpUtilsr   modelscope.utils.registryr   baser	   builderr
   r   register_modulevideo_stabilization_metricr   r   r   r    r   r   r   r   <module>   s&   ,