o
    ॵi3                     @   s   d dl mZ d dlmZmZ d dlZd dlZd dl	Z	d dl
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 d d	lmZmZ d d
lmZ e ZejejejdG dd deZ dS )    N)AnyDict)ImageSequenceClipVideoFileClip)	Pipelines)
preprocess)
OutputKeys)InputPipeline)	PIPELINES)	ModelFileTasks)
get_logger)module_namec                       sh   e Zd Zdef fddZdefddZdeeef deeef fdd	Z	deeef fd
dZ
  ZS )VideoHumanMattingPipelinemodelc                    s<   t  jdd|i| tj rd| _nd| _td dS )a   Video Human Matting Pipeline.
        use `model` to create a video human matting pipeline for prediction

        Example:

        >>> from modelscope.pipelines import pipeline
        >>> from modelscope.outputs import OutputKeys
        >>> from modelscope.utils.constant import Tasks
        >>> video_matting = pipeline(Tasks.video_human_matting, model='damo/cv_effnetv2_video-human-matting')
        >>> result_status = video_matting({
        'video_input_path':'https://modelscope.oss-cn-beijing.aliyuncs.com/test/videos/video_matting_test.mp4',
        'output_path':'matting_out.mp4'})
        >>> masks = result_status[OutputKeys.MASKS]
        r   cudacpuzload model doneN )super__init__torchr   is_availabledeviceloggerinfo)selfr   kwargs	__class__r   h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/video_human_matting_pipeline.pyr      s
   
z"VideoHumanMattingPipeline.__init__returnc                 C   s   |S )Nr   )r   inputr   r   r    r   /   s   z$VideoHumanMattingPipeline.preprocessr"   c                 K   sL  |d }d|v r|d }nd}t |}|t j}| \}}|jd d \}	}
dt|	|
 }|| _g }d gd }| j	| j
| _td| j
  t ? 	 |d u rUn1t|}| jj|	| j
g|R d	|i^}}|d
 j  ddd
}|| | \}}qPW d    n1 sw   Y  td |  tj|tj|iS )Nvideo_input_pathoutput_pathz
output.mp4   i      zmatting start using Tdownsample_ratior      zmatting process done)cv2VideoCapturegetCAP_PROP_FPSreadshapemaxfpsr   tor   r   r   r   no_gradr   datar   numpy	transposeappendreleaser   MASKSOUTPUT_VIDEO)r   r"   forward_params
video_pathout_pathvideo_inputr0   successframehwscalemasksrecframe_tensorphamaskr   r   r    forward2   sF   








z!VideoHumanMattingPipeline.forwardc                 K   s   | dd}|tj }|tj }g }|D ]}|d ddtj}|| qt	|| j
d}	|	j|| j
dd ~tj|r?d n|tj|i}
|
S )NrenderF      r%   )sequencer0   )r0   audio)r+   r   r8   r9   repeatastypenpuint8r6   r   r0   write_videofile)r   inputsr   rI   rC   r$   	frame_lstrG   comvideoresultr   r   r    postprocessR   s   

z%VideoHumanMattingPipeline.postprocess)__name__
__module____qualname__strr   r	   r   r   r   rH   rX   __classcell__r   r   r   r    r      s    

 r   )!os.pathpathosptypingr   r   r)   r4   rP   r   moviepy.editorr   r   modelscope.metainfor   (modelscope.models.cv.video_human_mattingr   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   register_modulevideo_human_mattingr   r   r   r   r    <module>   s$   