o
    *j3                     @   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   Zis_availabledeviceloggerinfo)selfr   kwargs	__class__r   u/var/www/html/Deteccion_Ine/venv/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 )NZvideo_input_pathoutput_pathz
output.mp4   i      zmatting start using TZdownsample_ratior      zmatting process done)cv2ZVideoCapturegetZCAP_PROP_FPSreadshapemaxfpsr   tor   r   r   r   Zno_gradr   datar   numpyZ	transposeappendreleaser   MASKSOUTPUT_VIDEO)r   r!   Zforward_paramsZ
video_pathZout_pathZvideo_inputr+   successframehwscalemasksZrecZframe_tensorZ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#   )sequencer+   )r+   Zaudio)r'   r   r1   r2   repeatZastypenpZuint8r/   r   r+   Zwrite_videofile)r   Zinputsr   r;   r8   r"   Z	frame_lstr9   comZvideoresultr   r   r   postprocessR   s   

z%VideoHumanMattingPipeline.postprocess)__name__
__module____qualname__strr   r	   r   r   r   r:   rC   __classcell__r   r   r   r   r      s    

 r   )!Zos.pathpathZosptypingr   r   r&   r.   r@   r   Zmoviepy.editorr   r   Zmodelscope.metainfor   Z(modelscope.models.cv.video_human_mattingr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r   Zregister_moduleZvideo_human_mattingr   r   r   r   r   <module>   s$   