o
    ॵiD
                     @   s   d dl mZmZ d dl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 d dlmZ e Zejejejd	G d
d de
ZdS )    )AnyDictN)	Pipelines)
OutputKeys)InputPipeline)	PIPELINES)Tasks)
get_logger)module_namec                       s   e Zd Zdef fddZdedeeef fddZdeeef deeef fdd	Z	d
eeef deeef fddZ
  ZS )MovieSceneSegmentationPipelinemodelc                    sN   | dd}tj r|dkrd}nd}t jd||d| td dS )zuse `model` to create a movie scene segmentation pipeline for prediction

        Args:
            model: model id on modelscope hub
        devicegpucpu)r   r   zLoad model done!N )poptorchcudais_availablesuper__init__loggerinfo)selfr   kwargs_devicer   	__class__r   m/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/movie_scene_segmentation_pipeline.pyr      s   z'MovieSceneSegmentationPipeline.__init__inputreturnc                 C   s   || _ t|tr| j|\| _| _| _| _n	t	dt
| | j| jd}t  | j|}W d   |S 1 s=w   Y  |S )a   use pyscenedetect to detect shot from the input video, and generate key-frame jpg, anno.ndjson, and shot-frame.txt
            Then use shot-encoder to encoder feat of the detected key-frame

        Args:
            input: path of the input video

        z input should be a str,  but got )shot_timecode_lstshot_idx_lstN)input_video_pth
isinstancestrr   
preprocess	shot2keyfannor"   r#   	TypeErrortyper   no_grad	inference)r   r    resultoutputr   r   r   r'   $   s"   


z)MovieSceneSegmentationPipeline.preprocessc                 K   s   |S )Nr   )r   r    forward_paramsr   r   r   forward>   s   z&MovieSceneSegmentationPipeline.forwardinputsc                 C   sD   | j || jd}| j|\}}}}tj|tj|tj|tj|i}|S )N)r$   featr(   )	r$   r(   r   postprocessr   SHOT_NUMSHOT_META_LIST	SCENE_NUMSCENE_META_LIST)r   r2   data	scene_numscene_meta_lstshot_numshot_meta_lstr.   r   r   r   r4   B   s   z*MovieSceneSegmentationPipeline.postprocess)__name__
__module____qualname__r&   r   r   r   r   r'   r1   r4   __classcell__r   r   r   r   r      s    

*r   )typingr   r   r   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.utils.constantr	   modelscope.utils.loggerr
   r   register_modulemovie_scene_segmentationr   r   r   r   r   <module>   s   