o
    ॵi                     @   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
 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 d d	lmZmZ d d
l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)tqdm)	Pipelines)PGLVideoSummarizationsummary_format)bvlc_googlenet)generate_summaryget_change_points)
OutputKeys)InputPipeline)	PIPELINES)Config)	ModelFile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
dd Z  ZS )VideoSummarizationPipelinemodelc                    s   t  jd	|dd| td|  t|d}t|tj}td|  t	|| _
t | _| jjtj|t| jd | j| j | _t|| _| j| j | _td dS )
z
        use `model` to create a video summarization pipeline for prediction
        Args:
            model: model id on modelscope hub.
        F)r   auto_collatezloading model from zbvlc_googlenet.ptzloading config from )map_locationzload model doneN )super__init__loggerinfoospjoinr   CONFIGURATIONr   	from_filecfgr   googlenet_modelr   load_state_dicttorchloaddevicetoevalr   	pgl_model)selfr   kwargsgooglenet_model_pathconfig_path	__class__r   h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/video_summarization_pipeline.pyr       s    
z#VideoSummarizationPipeline.__init__inputreturnc           
      C   s   t |tstdt| g }g }t|}|tj| _|tj	| _
d}| rL| \}}|s4n|d dkrD|| || |d7 }| s+|}|t||t|d}	|	S )Nz input should be a str,  but got r         )
video_namevideo_framesn_framepicks)
isinstancestr	TypeErrortypecv2VideoCapturegetCAP_PROP_FPSfpsCAP_PROP_FRAME_COUNTframe_countisOpenedreadappendnparray)
r*   r1   framesr8   cap	frame_idxretframer7   resultr   r   r0   
preprocess8   s4   



z%VideoSummarizationPipeline.preprocessc           	      C   sj   g }t |d D ]}| |}|| qt||d \}}| ||d |d |}t|| j}tj|iS )Nr6   r7   r8   )	r   r"   rF   r
   	inferencer   rA   r   OUTPUT)	r*   r1   frame_featuresrM   featchange_pointsn_frame_per_segsummaryoutputr   r   r0   forwardT   s   

z"VideoSummarizationPipeline.forwardinputsc                 C   s   |S )Nr   )r*   rY   r   r   r0   postprocesse   s   z&VideoSummarizationPipeline.postprocessc                 C   s   t t|tj}t|tj}t  7 | t|d}|d }|j	j
dks,| }|d  }t|g|g|g|gd }W d    | S 1 sOw   Y  | S )N)rR   scorescpur   )r$   
from_numpyrG   rH   float32int32no_gradr)   dictr&   r<   r\   squeezenumpytolistr	   )r*   rR   n_framesr8   rT   resultsr[   rV   r   r   r0   rP   h   s"   

		z$VideoSummarizationPipeline.inference)__name__
__module____qualname__r:   r   r   r   r   rO   rX   rZ   rP   __classcell__r   r   r.   r0   r      s    ""r   )'os.pathpathr   typingr   r   r=   rc   rG   r$   r   modelscope.metainfor   (modelscope.models.cv.video_summarizationr   r   3modelscope.models.cv.video_summarization.base_modelr   3modelscope.models.cv.video_summarization.summarizerr	   r
   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.utils.configr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   register_modulevideo_summarizationr   r   r   r   r0   <module>   s*   