o
    _{jË  ã                   @   s¤   d dl Z d dlmZ d dlm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 d dlZd dlZ				dd
d„Z				ddd„ZdS )é    N)Ú
itemgetter)ÚcollateÚscatter)Úload_checkpoint)Ú
OutputHook)ÚCompose)Úbuild_recognizerúcuda:0Fc                 C   sÆ   t | tƒrtj | ¡} nt | tjƒstdt| ƒ› �ƒ‚|r"| jdks)|s;| jdkr;|r-dnd}td|› d| j› d�ƒ‚d	| j	j
_t| j	|  d
¡d�}|d	urUt|||d� | |_| |¡ | ¡  |S )a#  Initialize a recognizer from config file.

    Args:
        config (str | :obj:`mmcv.Config`): Config file path or the config
            object.
        checkpoint (str | None, optional): Checkpoint path/url. If set to None,
            the model will not load any weights. Default: None.
        device (str | :obj:`torch.device`): The desired device of returned
            tensor. Default: 'cuda:0'.
        use_frames (bool): Whether to use rawframes as input. Default:False.

    Returns:
        nn.Module: The constructed recognizer.
    z4config must be a filename or Config object, but got ZRawframeDatasetZVideoDatasetZ	rawframesÚvideozWinput data type should be consist with the dataset type in config, but got input type 'z' and dataset type 'ú'NÚtest_cfg)r   )Úmap_location)Ú
isinstanceÚstrÚmmcvÚConfigÚfromfileÚ	TypeErrorÚtypeZdataset_typeÚRuntimeErrorÚmodelÚbackboneZ
pretrainedr   Úgetr   ÚcfgÚtoÚeval)ÚconfigÚ
checkpointÚdeviceÚ
use_framesÚ
input_typer   © r!   ú-/root/disk_3tb/HAC/mmaction/apis/inference.pyÚinit_recognizer   s0   
ÿÿ
þ
ý

r#   Tc               	   C   s<  t  |¡r|rtd|› d�ƒ‚t  |¡r|std|› d�ƒ‚t|tƒr&|f}|du s3t|ttfƒs3J ‚| j}t	|  
¡ ƒj}t|dƒ�}dd„ |D ƒ}	W d  ƒ n1 sTw   Y  |jjj}
t|
ƒ}
|r”t |d ¡}| tj¡}|jj d	d
¡}|jj dd¡}|jj dd¡}t||d|||d�}n|jj dd¡}t|d|dd�}|
|ƒ}t|gdd�}t	|  
¡ ƒjr¾t||gƒd }g }g }td|d  ¡ d dƒD ]�}i }|d |d< |d dd…||d …dd…dd…dd…dd…f |d< t ¡ �D | j |d d ¡}| j |¡}|   |¡}tj!|dd�}tj"|dd�}| dddi|¤Ž\}}| #¡  $¡ d }| #¡  $¡ }W d  ƒ n	1 �s@w   Y  | %|¡ | %|¡ qÎt&j'|dd�}t&j'|dd�}t&j"|dd�}t&j"|dd�}tt(|	|ƒƒ}t)|t*dƒdd�}|dd… }tt(|	|ƒƒ}t)|t*dƒdd�}|dd… }||||fS )aI  Inference a video with the detector.

    Args:
        model (nn.Module): The loaded recognizer.
        video_path (str): The video file path/url or the rawframes directory
            path. If ``use_frames`` is set to True, it should be rawframes
            directory path. Otherwise, it should be video file path.
        label_path (str): The label file path.
        use_frames (bool): Whether to use rawframes as input. Default:False.
        outputs (list(str) | tuple(str) | str | None) : Names of layers whose
            outputs need to be returned, default: None.
        as_tensor (bool): Same as that in ``OutputHook``. Default: True.

    Returns:
        dict[tuple(str, float)]: Top-5 recognition result dict.
        dict[torch.tensor | np.ndarray]:
            Output feature maps from layers specified in `outputs`.
    r   z+' is a video file, not a rawframe directoryz+' is a rawframe directory, not a video fileNÚrc                 S   s   g | ]}|  ¡ ‘qS r!   )Ústrip)Ú.0Úliner!   r!   r"   Ú
<listcomp>e   s    z(inference_recognizer.<locals>.<listcomp>z.mp4Úfilename_tmplzimg_{:05}.jpgÚmodalityÚRGBÚstart_indexr   éÿÿÿÿ)Ú	frame_dirÚtotal_framesÚlabelr,   r)   r*   )Úfilenamer0   r,   r*   é   )Zsamples_per_gpuÚimgsé   r0   )ÚdimZreturn_lossF)ÚaxisT)ÚkeyÚreverseé   r!   )+ÚospÚisfiler   Úisdirr   r   ÚtupleÚlistr   ÚnextÚ
parametersr   ÚopenÚdataÚtestZpipeliner   Úcv2ÚVideoCapturer   ÚCAP_PROP_FRAME_COUNTÚdictr   Úis_cudar   ÚrangeÚsizeÚtorchÚno_gradr   Úget_featureÚget_predictÚcls_headÚsoftmaxÚmeanÚcpuÚnumpyÚappendÚnpÚstackÚzipÚsortedr   ) r   Z
video_pathZ
label_pathr   ÚoutputsÚ	as_tensorr   r   Úfr0   Ztest_pipelineÚcapr/   r)   r*   r,   rB   Úoutput_listZoutput_list2ÚjÚtmpÚfeatÚpredict1ÚscoresÚfeatureZscores2Zscore_tuplesZscore_sortedZ
top5_labelZscore_tuples2Zscore_sorted2Ztop5_label2r!   r!   r"   Úinference_recognizer;   s�   
ÿ
ÿ
ÿ
ù	ü8

÷

rd   )Nr	   F)FNT)ÚosÚos.pathÚpathr:   Úoperatorr   r   rK   Zmmcv.parallelr   r   Zmmcv.runnerr   Zmmaction.corer   Zmmaction.datasets.pipelinesr   Zmmaction.modelsr   rS   rU   rD   r#   rd   r!   r!   r!   r"   Ú<module>   s(    
ý.û