o
    ôjS  ã                   @   sˆ   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	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 G d	d
„ d
eƒZdS )é    N)ÚPath)ÚAnyÚCallableÚDictÚListÚOptionalÚTupleÚUnion)ÚTensoré   )Úfind_classesÚmake_dataset)Ú
VideoClips)ÚVisionDatasetc                !       s  e Zd ZdZdZdddœZdZdZ									
	
	
	
	d)dee	e
f de	dededee dededee deee	ef  dededededede	ddf ‡ fdd„Zedee	ef fdd„ƒZd ee	 d!e	dededee f
d"d#„Zdefd$d%„Zd&edeeeef fd'd(„Z‡  ZS )*ÚHMDB51aº  
    `HMDB51 <https://serre-lab.clps.brown.edu/resource/hmdb-a-large-human-motion-database/>`_
    dataset.

    HMDB51 is an action recognition video dataset.
    This dataset consider every video as a collection of video clips of fixed size, specified
    by ``frames_per_clip``, where the step in frames between each clip is given by
    ``step_between_clips``.

    To give an example, for 2 videos with 10 and 15 frames respectively, if ``frames_per_clip=5``
    and ``step_between_clips=5``, the dataset size will be (2 + 3) = 5, where the first two
    elements will come from video 1, and the next three elements from video 2.
    Note that we drop clips which do not have exactly ``frames_per_clip`` elements, so not all
    frames in a video might be present.

    Internally, it uses a VideoClips object to handle clip creation.

    Args:
        root (str or ``pathlib.Path``): Root directory of the HMDB51 Dataset.
        annotation_path (str): Path to the folder containing the split files.
        frames_per_clip (int): Number of frames in a clip.
        step_between_clips (int): Number of frames between each clip.
        fold (int, optional): Which fold to use. Should be between 1 and 3.
        train (bool, optional): If ``True``, creates a dataset from the train split,
            otherwise from the ``test`` split.
        transform (callable, optional): A function/transform that takes in a TxHxWxC video
            and returns a transformed version.
        output_format (str, optional): The format of the output video tensors (before transforms).
            Can be either "THWC" (default) or "TCHW".

    Returns:
        tuple: A 3-tuple with the following entries:

            - video (Tensor[T, H, W, C] or Tensor[T, C, H, W]): The `T` video frames
            - audio(Tensor[K, L]): the audio frames, where `K` is the number of channels
              and `L` is the number of points
            - label (int): class of the video clip
    zJhttps://serre-lab.clps.brown.edu/wp-content/uploads/2013/10/hmdb51_org.rarzQhttps://serre-lab.clps.brown.edu/wp-content/uploads/2013/10/test_train_splits.rarZ 15e67781e70dcfbdce2d7dbb9b3344b5)ÚurlÚmd5r   é   NTr   ÚTHWCÚrootÚannotation_pathÚframes_per_clipÚstep_between_clipsÚ
frame_rateÚfoldÚtrainÚ	transformÚ_precomputed_metadataÚnum_workersÚ_video_widthÚ_video_heightÚ_video_min_dimensionÚ_audio_samplesÚoutput_formatÚreturnc                    s°   t ƒ  |¡ |dvrtd|› �ƒ‚d}t| jƒ\| _}t| j||ƒ| _dd„ | jD ƒ}t|||||	|
|||||d�}|| _	|| _
|| _|  ||||¡| _| | j¡| _|| _d S )N)r   r   é   z$fold should be between 1 and 3, got )Zavic                 S   s   g | ]\}}|‘qS © r&   )Ú.0ÚpathÚ_r&   r&   úl/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/hmdb51.pyÚ
<listcomp>[   s    z#HMDB51.__init__.<locals>.<listcomp>)r   r   r    r!   r"   r#   )ÚsuperÚ__init__Ú
ValueErrorr   r   Úclassesr   Úsamplesr   Úfull_video_clipsr   r   Ú_select_foldÚindicesÚsubsetÚvideo_clipsr   )Úselfr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Ú
extensionsÚclass_to_idxZvideo_pathsr5   ©Ú	__class__r&   r*   r-   =   s<   ýõ
zHMDB51.__init__c                 C   s   | j jS ©N)r1   Úmetadata©r6   r&   r&   r*   r<   s   s   zHMDB51.metadataÚ
video_listÚannotations_dirc              	   C   sØ   |r| j n| j}d|› d�}tj ||¡}t |¡}tƒ }	|D ]2}
t|
ƒ�}| ¡ }W d   ƒ n1 s4w   Y  |D ]}| 	¡ \}}t
|ƒ}||krP|	 |¡ q;qg }t|ƒD ]\}}tj |¡|	v ri| |¡ qX|S )Nz*test_splitz.txt)Ú	TRAIN_TAGÚTEST_TAGÚosr(   ÚjoinÚglobÚsetÚopenÚ	readlinesÚsplitÚintÚaddÚ	enumerateÚbasenameÚappend)r6   r>   r?   r   r   Z
target_tagZsplit_pattern_nameZsplit_pattern_pathZannotation_pathsZselected_filesÚfilepathÚfidÚlinesÚlineZvideo_filenameZ
tag_stringÚtagr3   Zvideo_indexZ
video_pathr&   r&   r*   r2   w   s,   


ÿ
€ü
€zHMDB51._select_foldc                 C   s
   | j  ¡ S r;   )r5   Z	num_clipsr=   r&   r&   r*   Ú__len__�   s   
zHMDB51.__len__Úidxc                 C   sJ   | j  |¡\}}}}| j| }| j| \}}| jd ur |  |¡}|||fS r;   )r5   Zget_clipr3   r0   r   )r6   rT   ÚvideoÚaudior)   Z	video_idxZsample_indexÚclass_indexr&   r&   r*   Ú__getitem__�   s   



zHMDB51.__getitem__)r   Nr   TNNr   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zdata_urlÚsplitsr@   rA   r	   Ústrr   rI   r   Úboolr   r   r   r-   Úpropertyr<   r   r2   rS   r   r
   rX   Ú__classcell__r&   r&   r9   r*   r      st    'þð
þýüûúùø	÷
öõôóòñðï6&$r   )rD   rB   Úpathlibr   Útypingr   r   r   r   r   r   r	   Útorchr
   Úfolderr   r   Zvideo_utilsr   Úvisionr   r   r&   r&   r&   r*   Ú<module>   s    $