o
    *j                     @   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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   Zauto_collatezloading model from zbvlc_googlenet.ptzloading config from T)Zmap_locationZweights_onlyzload model doneN )super__init__loggerinfoospjoinr   ZCONFIGURATIONr   	from_filecfgr   googlenet_modelr   Zload_state_dicttorchloaddevicetoevalr   	pgl_model)selfr   kwargsZgooglenet_model_pathZconfig_path	__class__r   u/var/www/html/Deteccion_Ine/venv/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         )Z
video_namevideo_framesn_framepicks)
isinstancestr	TypeErrortypecv2ZVideoCapturegetZCAP_PROP_FPSfpsZCAP_PROP_FRAME_COUNTZframe_countZisOpenedreadappendnparray)
r&   r+   framesr1   capZ	frame_idxretframer0   resultr   r   r*   
preprocess:   s4   



z%VideoSummarizationPipeline.preprocessc           	      C   sj   g }t |d D ]}| |}|| qt||d \}}| ||d |d |}t|| j}tj|iS )Nr/   r0   r1   )	r   r   r:   r
   	inferencer   r8   r   ZOUTPUT)	r&   r+   frame_featuresr@   Zfeatchange_pointsZn_frame_per_segsummaryoutputr   r   r*   forwardV   s   

z"VideoSummarizationPipeline.forwardinputsc                 C   s   |S )Nr   )r&   rI   r   r   r*   postprocessg   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)rD   scorescpur   )r    Z
from_numpyr;   r<   Zfloat32Zint32Zno_gradr%   dictr"   r5   rL   Zsqueezenumpytolistr	   )r&   rD   Zn_framesr1   rE   resultsrK   rF   r   r   r*   rC   j   s"   

		z$VideoSummarizationPipeline.inference)__name__
__module____qualname__r3   r   r   r   r   rB   rH   rJ   rC   __classcell__r   r   r(   r*   r      s    ""r   )'Zos.pathpathr   typingr   r   r6   rN   r;   r    r   Zmodelscope.metainfor   Z(modelscope.models.cv.video_summarizationr   r   Z3modelscope.models.cv.video_summarization.base_modelr   Z3modelscope.models.cv.video_summarization.summarizerr	   r
   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r   Zregister_moduleZvideo_summarizationr   r   r   r   r*   <module>   s*   