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‡  ZS )ÚRawframeDataseta  Rawframe dataset for action recognition.

    The dataset loads raw frames and apply specified transforms to return a
    dict containing the frame tensors and other information.

    The ann_file is a text file with multiple lines, and each line indicates
    the directory to frames of a video, total frames of the video and
    the label of a video, which are split with a whitespace.
    Example of a annotation file:

    .. code-block:: txt

        some/directory-1 163 1
        some/directory-2 122 1
        some/directory-3 258 2
        some/directory-4 234 2
        some/directory-5 295 3
        some/directory-6 121 3

    Example of a multi-class annotation file:


    .. code-block:: txt

        some/directory-1 163 1 3 5
        some/directory-2 122 1 2
        some/directory-3 258 2
        some/directory-4 234 2 4 6 8
        some/directory-5 295 3
        some/directory-6 121 3

    Example of a with_offset annotation file (clips from long videos), each
    line indicates the directory to frames of a video, the index of the start
    frame, total frames of the video clip and the label of a video clip, which
    are split with a whitespace.


    .. code-block:: txt

        some/directory-1 12 163 3
        some/directory-2 213 122 4
        some/directory-3 100 258 5
        some/directory-4 98 234 2
        some/directory-5 0 295 3
        some/directory-6 50 121 3


    Args:
        ann_file (str): Path to the annotation file.
        pipeline (list[dict | callable]): A sequence of data transforms.
        data_prefix (str | None): Path to a directory where videos are held.
            Default: None.
        test_mode (bool): Store True when building test or validation dataset.
            Default: False.
        filename_tmpl (str): Template for each filename.
            Default: 'img_{:05}.jpg'.
        with_offset (bool): Determines whether the offset information is in
            ann_file. Default: False.
        multi_class (bool): Determines whether it is a multi-class
            recognition dataset. Default: False.
        num_classes (int | None): Number of classes in the dataset.
            Default: None.
        modality (str): Modality of data. Support 'RGB', 'Flow'.
            Default: 'RGB'.
        sample_by_class (bool): Sampling by class, should be set `True` when
            performing inter-class data balancing. Only compatible with
            `multi_class == False`. Only applies for training. Default: False.
        power (float | None): We support sampling data with the probability
            proportional to the power of its label frequency (freq ^ power)
            when sampling data. `power == 1` indicates uniformly sampling all
            data; `power == 0` indicates uniformly sampling all classes.
            Default: None.
    NFúimg_{:05}.jpgr   ÚRGBc                    s0   || _ || _tƒ j|||||||	|
||d�
 d S )N)Úsample_by_classÚpower)Úfilename_tmplÚwith_offsetÚsuperÚ__init__)ÚselfÚann_fileÚpipelineÚdata_prefixÚ	test_moder
   r   Úmulti_classÚnum_classesÚstart_indexÚmodalityr   r	   ©Ú	__class__© ú8/root/disk_3tb/HAC/mmaction/datasets/rawframe_dataset.pyr   W   s   
özRawframeDataset.__init__c           	      C   sP  | j  d¡r
|  ¡ S g }t| j dƒ�Œ}|D ]€}| ¡  ¡ }i }d}|| }| jdur1t | j|¡}||d< |d7 }| j	rSt
|| ƒ|d< t
||d  ƒ|d< |d	7 }nt
|| ƒ|d< |d7 }d
d„ ||d… D ƒ}|ssJ d|› �ƒ‚| jr‚| jdus}J ‚||d< nt|ƒdksŠJ ‚|d |d< | |¡ qW d  ƒ |S 1 s¡w   Y  |S )z.Load annotation file to get video information.z.jsonÚrr   NÚ	frame_dirr   ÚoffsetÚtotal_framesé   c                 S   s   g | ]}t |ƒ‘qS r   )Úint)Ú.0Úxr   r   r   Ú
<listcomp>Œ   s    z4RawframeDataset.load_annotations.<locals>.<listcomp>zmissing label in line: Úlabel)r   ÚendswithÚload_json_annotationsÚopenÚstripÚsplitr   ÚospÚjoinr   r    r   r   ÚlenÚappend)	r   Úvideo_infosÚfinÚlineÚ
line_splitÚ
video_infoÚidxr   r$   r   r   r   Úload_annotationsr   s@   


ä
ÿáz RawframeDataset.load_annotationsc                 C   ó‚   | j r| j| }t tj |¡¡}nt | j| ¡}| j|d< | j	|d< | j
|d< | jr<t | j¡}d||d < ||d< |  |¡S )z0Prepare the frames for training given the index.r
   r   r   ç      ð?r$   ©r   Úvideo_infos_by_classÚcopyÚdeepcopyÚnpÚrandomÚchoicer.   r
   r   r   r   ÚtorchÚzerosr   r   ©r   r3   ÚsamplesÚresultsÚonehotr   r   r   Úprepare_train_frames˜   ó   




z$RawframeDataset.prepare_train_framesc                 C   r5   )z/Prepare the frames for testing given the index.r
   r   r   r6   r$   r7   r@   r   r   r   Úprepare_test_frames¬   rE   z#RawframeDataset.prepare_test_frames)
NFr   FFNr   r   FN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r4   rD   rF   Ú__classcell__r   r   r   r   r      s     Mô&r   )r9   Úos.pathÚpathr*   Únumpyr;   r>   Úbaser   Úregistryr   Úregister_moduler   r   r   r   r   Ú<module>   s    