o
    ôj�  ã                   @   s€   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#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 )$ÚUCF101aÙ  
    `UCF101 <https://www.crcv.ucf.edu/data/UCF101.php>`_ dataset.

    UCF101 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``. The dataset itself can be downloaded from the dataset website;
    annotations that ``annotation_path`` should be pointing to can be downloaded from `here
    <https://www.crcv.ucf.edu/data/UCF101/UCF101TrainTestSplits-RecognitionTask.zip>`_.

    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 UCF101 Dataset.
        annotation_path (str): path to the folder containing the split files;
            see docstring above for download instructions of these files
        frames_per_clip (int): number of frames in a clip.
        step_between_clips (int, optional): 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
    r   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|  krdksn td|› �ƒ‚d}|| _|| _t| jƒ\| _}t| j||d d�| _	dd„ | j	D ƒ}t
|||||	|
|||||d�}|| _|  ||||¡| _| | j¡| _|| _d S )	Nr   é   z$fold should be between 1 and 3, got )Úavi)Úis_valid_filec                 S   s   g | ]}|d  ‘qS )r   © ©Ú.0Úxr%   r%   úl/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/ucf101.pyÚ
<listcomp>R   s    z#UCF101.__init__.<locals>.<listcomp>)r   r   r   r   r   r    )ÚsuperÚ__init__Ú
ValueErrorr   r   r   r   Úclassesr   Úsamplesr   Úfull_video_clipsÚ_select_foldÚindicesÚsubsetÚvideo_clipsr   )Úselfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r    Ú
extensionsÚclass_to_idxÚ
video_listr4   ©Ú	__class__r%   r)   r,   6   s4   õ
zUCF101.__init__c                 C   s   | j jS ©N)r0   Úmetadata©r5   r%   r%   r)   r<   h   s   zUCF101.metadatar8   c           
         s¬   |rdnd}|› d|d›d�}t j ||¡}tƒ ‰ t|ƒ�!}| ¡ }dd„ |D ƒ}‡fdd„|D ƒ}ˆ  |¡ W d   ƒ n1 sAw   Y  ‡ ‡fd	d„ttˆƒƒD ƒ}	|	S )
Nr   ÚtestÚlistÚ02dz.txtc                 S   s   g | ]}|  ¡  d ¡d ‘qS )ú r   )ÚstripÚsplitr&   r%   r%   r)   r*   s   s    z'UCF101._select_fold.<locals>.<listcomp>c                    s(   g | ]}t jjˆ jg| d ¡¢R Ž ‘qS )ú/)ÚosÚpathÚjoinr   rC   r&   r=   r%   r)   r*   t   s   ( c                    s   g | ]
}ˆ| ˆ v r|‘qS r%   r%   )r'   Úi)Úselected_filesr8   r%   r)   r*   v   s    )	rE   rF   rG   ÚsetÚopenÚ	readlinesÚupdateÚrangeÚlen)
r5   r8   r   r   r   ÚnameÚfÚfidÚdatar2   r%   )rI   r5   r8   r)   r1   l   s   
üzUCF101._select_foldc                 C   s
   | j  ¡ S r;   )r4   Ú	num_clipsr=   r%   r%   r)   Ú__len__y   s   
zUCF101.__len__Úidxc                 C   sF   | j  |¡\}}}}| j| j|  d }| jd ur|  |¡}|||fS )Nr   )r4   Úget_clipr/   r2   r   )r5   rV   ÚvideoÚaudioÚinfoÚ	video_idxÚlabelr%   r%   r)   Ú__getitem__|   s
   


zUCF101.__getitem__)r   Nr   TNNr   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   Ústrr   Úintr   Úboolr   r   r   r,   Úpropertyr<   r   r1   rU   r   r
   r]   Ú__classcell__r%   r%   r9   r)   r      sh    .ð
þýüûúùø	÷
öõôóòñðï2&$r   )rE   Úpathlibr   Útypingr   r   r   r   r   r   r	   Útorchr
   Úfolderr   r   Úvideo_utilsr   Úvisionr   r   r%   r%   r%   r)   Ú<module>   s    $