o
    ϯi                      @  s   d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZ d dlZd dlZd dlmZ ddlmZ ddlmZ eeZG dd	 d	Zejjjdd
ddddfd ddZdS )!    )annotationsN)AnyCallableListOptionalTupleTypeUnion)	g_pathmgr   )ClipSampler)LabeledVideoDatasetc                   @  s|   e Zd ZdZg dZddddZe	d#d$ddZe	d%d&ddZ	d'd(ddZ	dd Z
ede
Z
d)dd Zd*d!d"ZdS )+Hmdb51LabeledVideoPathsa  
    Pre-processor for Hmbd51 dataset mentioned here -
        https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/

    This dataset consists of classwise folds with each class consisting of 3
        folds (splits).

    The videos directory is of the format,
        video_dir_path/class_x/<somevideo_name>.avi
        ...
        video_dir_path/class_y/<somevideo_name>.avi

    The splits/fold directory is of the format,
        folds_dir_path/class_x_test_split_1.txt
        folds_dir_path/class_x_test_split_2.txt
        folds_dir_path/class_x_test_split_3.txt
        ...
        folds_dir_path/class_y_test_split_1.txt
        folds_dir_path/class_y_test_split_2.txt
        folds_dir_path/class_y_test_split_3.txt

    And each text file in the splits directory class_x_test_split_<1 or 2 or 3>.txt
        <a video as in video_dir_path/class_x> <0 or 1 or 2>
        where 0,1,2 corresponds to unused, train split respectively.

    Each video has name of format
        <some_name>_<tag1>_<tag2>_<tag_3>_<tag4>_<tag5>_<some_id>.avi
    For more details on tags -
        https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/
    )r         r   r   r   )traintestunusedr   	data_pathstrsplit_idint
split_typereturnc                   sz   t |}| st| dS t|| jvr"t| d| j dS dtt|  t fdd| D }| 	||S )a6  
        Factory function that creates Hmdb51LabeledVideoPaths object form a splits/folds
        directory.

        Args:
            data_path (str): The path to the splits/folds directory of HMDB51.
            split_id (int): Fold id to be loaded. Belongs to [1,2,3]
            split_type (str): Split/Fold type to be loaded. It belongs to one of the
                following,
                - "train"
                - "test"
                - "unused" (This is a small set of videos that are neither
                of part of test or train fold.)
        z! not found or is not a directory.z! not found in allowed split id's ._test_splitc                 3  s0    | ]}|  r|jd kr |jv r|V  qdS ).txtN)is_filesuffixstem).0ffile_name_format L/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/data/hmdb51.py	<genexpr>S   s    z3Hmdb51LabeledVideoPaths.from_dir.<locals>.<genexpr>)
pathlibPathis_dirRuntimeErrorr   _allowed_splitsr   sortediterdir	from_csvs)clsr   r   r   
file_pathsr$   r"   r%   from_dir8   s   

z Hmdb51LabeledVideoPaths.from_dirr0   List[Union[pathlib.Path, str]]c           
   	   C  s6  g }|D ]}t |}t|sJ | d|jdkr!d|jv s*td|   S d}||jddd }t	|dA}|
  D ]2}|dd	}t|d	 | j| ksZqFtj||d
 }|d
 ddd }	||||	df qFW d   n1 sw   Y  qt|d
ksJ d| d| |S )a  
        Factory function that creates Hmdb51LabeledVideoPaths object form a list of
        split files of .txt type

        Args:
            file_paths (List[Union[pathlib.Path, str]]) : The path to the splits/folds
                    directory of HMDB51.
            split_type (str): Split/Fold type to be loaded.
                - "train"
                - "test"
                - "unused"
        z not found.r   r   zIvalid file: _Nrr   r   i)label	meta_tagszFailed to load dataset from r   )r'   r(   r
   existsr   r   r*   joinsplitopenread
splitlinesrsplitr   _split_type_dictospathappendlen)
r/   r0   r   video_paths_and_label	file_pathaction_namer!   
path_label
line_splitr8   r$   r$   r%   r.   [   s2   

z!Hmdb51LabeledVideoPaths.from_csvs paths_and_labels List[Tuple[str, Optional[dict]]]Nonec                 C  s   || _ || _dS )z
        Args:
            paths_and_labels [(str, int)]: a list of tuples containing the video
                path and integer label.
        N)_paths_and_labels_path_prefix)selfrK   path_prefixr$   r$   r%   __init__   s   
z Hmdb51LabeledVideoPaths.__init__c                 C  s
   || _ d S )N)rO   )rP   prefixr$   r$   r%   rQ      s   
z#Hmdb51LabeledVideoPaths.path_prefixNindexTuple[str, dict]c                 C  s"   | j | \}}tj| j||fS )z
        Args:
            index (int): the path and label index.

        Returns:
            The path and label tuple for the given index.
        )rN   rA   rB   r:   rO   )rP   rT   rB   r7   r$   r$   r%   __getitem__   s   z#Hmdb51LabeledVideoPaths.__getitem__c                 C  s
   t | jS )zQ
        Returns:
            The number of video paths and label pairs.
        )rD   rN   )rP   r$   r$   r%   __len__   s   
zHmdb51LabeledVideoPaths.__len__)r   r   )r   r   r   r   r   r   r   r   )r   )r0   r2   r   r   r   r   )rJ   )rK   rL   r   rM   )rT   r   r   rU   )r   r   )__name__
__module____qualname____doc__r+   r@   classmethodr1   r.   rR   rQ   propertyrV   rW   r$   r$   r$   r%   r      s     "+

r   rJ   r   Tpyavr   pathlib.Pathclip_samplerr   video_samplerType[torch.utils.data.Sampler]	transformOptional[Callable[[dict], Any]]video_path_prefixr   r   r   r   decoderr   r   c	                 C  s:   t jd tj| ||d}	||	_t|	|||||d}
|
S )a  
    A helper function to create ``LabeledVideoDataset`` object for HMDB51 dataset

    Args:
        data_path (pathlib.Path): Path to the data. The path type defines how the data
            should be read:

            * For a file path, the file is read and each line is parsed into a
              video path and label.
            * For a directory, the directory structure defines the classes
              (i.e. each subdirectory is a class).

        clip_sampler (ClipSampler): Defines how clips should be sampled from each
            video. See the clip sampling documentation for more information.

        video_sampler (Type[torch.utils.data.Sampler]): Sampler for the internal
            video container. This defines the order videos are decoded and,
            if necessary, the distributed split.

        transform (Callable): This callable is evaluated on the clip output before
            the clip is returned. It can be used for user defined preprocessing and
            augmentations to the clips. See the ``LabeledVideoDataset`` class for
            clip output format.

        video_path_prefix (str): Path to root directory with the videos that are
            loaded in LabeledVideoDataset. All the video paths before loading
            are prefixed with this path.

        split_id (int): Fold id to be loaded. Options are 1, 2 or 3

        split_type (str): Split/Fold type to be loaded. Options are ("train", "test" or
            "unused")

        decoder (str): Defines which backend should be used to decode videos.
    zPYTORCHVIDEO.dataset.Hmdb51)r   r   )decode_audiorf   )torch_C_log_api_usage_oncer   r1   rQ   r   )r   r`   ra   rc   re   r   r   rg   rf   labeled_video_pathsdatasetr$   r$   r%   Hmdb51   s   /	rm   )r   r_   r`   r   ra   rb   rc   rd   re   r   r   r   r   r   rf   r   r   r   )
__future__r   loggingrA   r'   typingr   r   r   r   r   r   r	   rh   torch.utils.dataiopath.common.file_ior
   clip_samplingr   labeled_video_datasetr   	getLoggerrX   loggerr   utilsdataRandomSamplerrm   r$   r$   r$   r%   <module>   s*   $
 