o
    _{j  ã                   @   sX   d dl Z d dlmZ d dlZd dlZddlmZ ddlm	Z	 e	 
¡ G dd„ deƒƒZdS )é    Né   )ÚBaseDataset)ÚDATASETSc                       sN   e Zd ZdZ		d‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	‡  Z
S )ÚRawVideoDatasetaä  RawVideo dataset for action recognition, used in the Project OmniSource.

    The dataset loads clips of raw videos and apply specified transforms to
    return a dict containing the frame tensors and other information. Not that
    for this dataset, `multi_class` should be False.

    The ann_file is a text file with multiple lines, and each line indicates
    a sample video with the filepath (without suffix), label, number of clips
    and index of positive clips (starting from 0), which are split with a
    whitespace. Raw videos should be first trimmed into 10 second clips,
    organized in the following format:

    .. code-block:: txt

        some/path/D32_1gwq35E/part_0.mp4
        some/path/D32_1gwq35E/part_1.mp4
        ......
        some/path/D32_1gwq35E/part_n.mp4

    Example of a annotation file:

    .. code-block:: txt

        some/path/D32_1gwq35E 66 10 0 1 2
        some/path/-G-5CJ0JkKY 254 5 3 4
        some/path/T4h1bvOd9DA 33 1 0
        some/path/4uZ27ivBl00 341 2 0 1
        some/path/0LfESFkfBSw 186 234 7 9 11
        some/path/-YIsNpBEx6c 169 100 9 10 11

    The first line indicates that the raw video `some/path/D32_1gwq35E` has
    action label `66`, consists of 10 clips (from `part_0.mp4` to
    `part_9.mp4`). The 1st, 2nd and 3rd clips are positive clips.


    Args:
        ann_file (str): Path to the annotation file.
        pipeline (list[dict | callable]): A sequence of data transforms.
        sampling_strategy (str): The strategy to sample clips from raw videos.
            Choices are 'random' or 'positive'. Default: 'positive'.
        clipname_tmpl (str): The template of clip name in the raw video.
            Default: 'part_{}.mp4'.
        **kwargs: Keyword arguments for ``BaseDataset``.
    úpart_{}.mp4Úpositivec                    sX   t ƒ j||fddi|¤Ž | jdu sJ ‚|| _|| _| jdkr*dd„ | jD ƒ| _d S d S )NÚstart_indexr   Fr   c                 S   s   g | ]
}t |d  ƒr|‘qS )Úpositive_clip_inds)Úlen)Ú.0Úx© r   ú8/root/disk_3tb/HAC/mmaction/datasets/rawvideo_dataset.pyÚ
<listcomp>G   s
    ÿÿz,RawVideoDataset.__init__.<locals>.<listcomp>)ÚsuperÚ__init__Úmulti_classÚsampling_strategyÚclipname_tmplÚvideo_infos)ÚselfÚann_fileÚpipeliner   r   Úkwargs©Ú	__class__r   r   r   :   s   
ÿÿzRawVideoDataset.__init__c           	   
   C   sÄ   | j  d¡r
|  ¡ S g }t| j dƒ�F}|D ]:}| ¡  ¡ }|d }t|d ƒ}t|d ƒ}dd„ |dd	… D ƒ}| jd	urDt 	| j|¡}| 
t||||d
�¡ qW d	  ƒ |S 1 s[w   Y  |S )z.Load annotation file to get video information.z.jsonÚrr   r   é   c                 S   s   g | ]}t |ƒ‘qS r   )Úint)r   Úindr   r   r   r   X   s    z4RawVideoDataset.load_annotations.<locals>.<listcomp>é   N)Ú	video_dirÚlabelÚ	num_clipsr	   )r   ÚendswithÚload_json_annotationsÚopenÚstripÚsplitr   Údata_prefixÚospÚjoinÚappendÚdict)	r   r   ÚfinÚlineÚ
line_splitr!   r"   r#   r	   r   r   r   Úload_annotationsL   s2   
üÿ÷
ÿðz RawVideoDataset.load_annotationsc                 C   sZ   t  | j¡}t|ƒ}d}t|ƒD ]}| jdur*|| | }t | j|¡}||| |< q|S )z3Load json annotation file to get video information.r!   N)ÚmmcvÚloadr   r
   Úranger)   r*   r+   )r   r   Ú
num_videosÚpath_keyÚiÚ
path_valuer   r   r   r%   e   s   
€z%RawVideoDataset.load_json_annotationsc                 C   sŒ   | j dv sJ ‚| j dkr|d sJ ‚t |d ¡}n
t d|d d ¡}| j |¡}| jd  ¡ r:t |d |¡}n|d | }||d< |S )	z=Sample a clip from the raw video given the sampling strategy.)r   Úrandomr   r	   r   r#   r   r!   Úfilename)	r   r9   ÚchoiceÚrandintr   ÚformatÚisalphar*   r+   )r   Úresultsr   Zclipnamer:   r   r   r   Úsample_clipq   s   
zRawVideoDataset.sample_clipc                 C   ó8   t  | j| ¡}|  |¡}| j|d< | j|d< |  |¡S )z0Prepare the frames for training given the index.Úmodalityr   ©ÚcopyÚdeepcopyr   r@   rB   r   r   ©r   Úidxr?   r   r   r   Úprepare_train_frames„   ó
   



z$RawVideoDataset.prepare_train_framesc                 C   rA   )z/Prepare the frames for testing given the index.rB   r   rC   rF   r   r   r   Úprepare_test_framesŒ   rI   z#RawVideoDataset.prepare_test_frames)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r1   r%   r@   rH   rJ   Ú__classcell__r   r   r   r   r      s    0ür   )rD   Úos.pathÚpathr*   r9   r2   Úbaser   Úregistryr   Úregister_moduler   r   r   r   r   Ú<module>   s    