o
    ôj–(  ã                   @   sê   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dl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mZmZmZ ddlmZ ddlmZ de de de ddfdd„Z!G dd„ deƒZ"dS )é    N)Úpartial)ÚPool)Úpath)ÚPath)ÚAnyÚCallableÚDictÚOptionalÚTupleÚUnion)ÚTensoré   )Úfind_classesÚmake_dataset)Úcheck_integrityÚdownload_and_extract_archiveÚdownload_urlÚverify_str_arg)Ú
VideoClips)ÚVisionDatasetÚtarpathÚ	videopathÚlineÚreturnc                 C   s   t || |ƒ d S ©N)r   )r   r   r   © r   ún/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/kinetics.pyÚ_dl_wrap   s   r   c                )       s4  e Zd ZdZddddœZddddœZ			
															d5deeef de	dedede
e	 de	de
e deedf dede	de	de
eeef  de	de	d e	d!e	d"e	d#ed$ed%df(‡ fd&d'„Zd6d(d)„Zd6d*d+„Zd6d,d-„Zed%eeef fd.d/„ƒZd%e	fd0d1„Zd2e	d%eeee	f fd3d4„Z‡  ZS )7ÚKineticsu†  `Generic Kinetics <https://www.deepmind.com/open-source/kinetics>`_
    dataset.

    Kinetics-400/600/700 are action recognition video datasets.
    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.

    Args:
        root (str or ``pathlib.Path``): Root directory of the Kinetics Dataset.
            Directory should be structured as follows:
            .. code::

                root/
                â”œâ”€â”€ split
                â”‚   â”œâ”€â”€  class1
                â”‚   â”‚   â”œâ”€â”€  vid1.mp4
                â”‚   â”‚   â”œâ”€â”€  vid2.mp4
                â”‚   â”‚   â”œâ”€â”€  vid3.mp4
                â”‚   â”‚   â”œâ”€â”€  ...
                â”‚   â”œâ”€â”€  class2
                â”‚   â”‚   â”œâ”€â”€   vidx.mp4
                â”‚   â”‚    â””â”€â”€ ...

            Note: split is appended automatically using the split argument.
        frames_per_clip (int): number of frames in a clip
        num_classes (int): select between Kinetics-400 (default), Kinetics-600, and Kinetics-700
        split (str): split of the dataset to consider; supports ``"train"`` (default) ``"val"`` ``"test"``
        frame_rate (float): If omitted, interpolate different frame rate for each clip.
        step_between_clips (int): number of frames between each clip
        transform (callable, optional): A function/transform that takes in a TxHxWxC video
            and returns a transformed version.
        download (bool): Download the official version of the dataset to root folder.
        num_workers (int): Use multiple workers for VideoClips creation
        num_download_workers (int): Use multiprocessing in order to speed up download.
        output_format (str, optional): The format of the output video tensors (before transforms).
            Can be either "THWC" or "TCHW" (default).
            Note that in most other utils and datasets, the default is actually "THWC".

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

            - video (Tensor[T, C, H, W] or Tensor[T, H, W, C]): the `T` video frames in torch.uint8 tensor
            - audio(Tensor[K, L]): the audio frames, where `K` is the number of channels
              and `L` is the number of points in torch.float tensor
            - label (int): class of the video clip

    Raises:
        RuntimeError: If ``download is True`` and the video archives are already extracted.
    zChttps://s3.amazonaws.com/kinetics/400/{split}/k400_{split}_path.txtzChttps://s3.amazonaws.com/kinetics/600/{split}/k600_{split}_path.txtzMhttps://s3.amazonaws.com/kinetics/700_2020/{split}/k700_2020_{split}_path.txt©Ú400Z600Z700z=https://s3.amazonaws.com/kinetics/400/annotations/{split}.csvz=https://s3.amazonaws.com/kinetics/600/annotations/{split}.csvzBhttps://s3.amazonaws.com/kinetics/700_2020/annotations/{split}.csvr    ÚtrainNr   ©ÚaviZmp4Fr   ÚTCHWÚrootÚframes_per_clipÚnum_classesÚsplitÚ
frame_rateÚstep_between_clipsÚ	transformÚ
extensions.ÚdownloadÚnum_download_workersÚnum_workersÚ_precomputed_metadataÚ_video_widthÚ_video_heightÚ_video_min_dimensionÚ_audio_samplesÚ_audio_channelsÚ_legacyÚoutput_formatr   c                    sò   t |dg d¢d�| _|| _|
| _|| _|| _|r+tdƒ || _d| _d}|	r*t	dƒ‚nt
 ||¡| _t |dg d	¢d�| _|	rB|  ¡  tƒ  | j¡ t| jƒ\| _}t| j||d d
�| _dd„ | jD ƒ}t||||||||||||d�| _|| _d S )Nr'   r   )ÚargÚvalid_valueszUsing legacy structureÚunknownÚTHWCz2Cannot download the videos using legacy_structure.r(   )r!   ÚvalÚtest)Úis_valid_filec                 S   s   g | ]}|d  ‘qS )r   r   )Ú.0Úxr   r   r   Ú
<listcomp>�   s    z%Kinetics.__init__.<locals>.<listcomp>)r/   r1   r2   r3   r4   r5   r7   )r   r'   r,   r.   r%   r6   ÚprintÚsplit_folderr(   Ú
ValueErrorr   ÚjoinÚdownload_and_process_videosÚsuperÚ__init__r   Úclassesr   Úsamplesr   Úvideo_clipsr+   )Úselfr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   Úclass_to_idxÚ
video_list©Ú	__class__r   r   rH   \   sH   ÿô
zKinetics.__init__c                 C   sb   t   ¡ }|  ¡  t   ¡ }td|| d ƒ |  ¡  t   ¡ }td|| d ƒ td|| d ƒ dS )zEDownloads all the videos to the _root_ folder in the expected format.z%Elapsed time for downloading in mins é<   z$Elapsed time for processing in mins zElapsed time overall in mins N)ÚtimeÚ_download_videosrB   Ú_make_ds_structure)rL   ÚticZtocZtoc2r   r   r   rF   ž   s   z$Kinetics.download_and_process_videosc           
      C   s   t  | j¡rtd| j› d�ƒ‚t  | jd¡}t  | jd¡}| j| j j| j	d�}t  |t  
|¡¡}t|ƒs:t||ƒ t|ƒ�}dd„ | ¡  ¡ D ƒ}W d  ƒ n1 sTw   Y  | jd	krl|D ]	}t||| jƒ q`dS tt|| jƒ}t| jƒ}	|	 ||¡ dS )
a  download tarballs containing the video to "tars" folder and extract them into the _split_ folder where
        split is one of the official dataset splits.

        Raises:
            RuntimeError: if download folder exists, break to prevent downloading entire dataset again.
        zThe directory z[ already exists. If you want to re-download or re-extract the images, delete the directory.ZtarsÚfiles©r(   c                 S   s   g | ]
}t jj|d d�‘qS )z/,:)Úsafe)ÚurllibÚparseÚquote)r?   r   r   r   r   rA   ½   s    z-Kinetics._download_videos.<locals>.<listcomp>Nr   )r   ÚexistsrC   ÚRuntimeErrorrE   r%   Ú	_TAR_URLSr'   Úformatr(   Úbasenamer   r   ÚopenÚreadÚ
splitlinesr.   r   r   r   r   Úmap)
rL   Ztar_pathZfile_list_pathZ	split_urlZsplit_url_filepathÚfileZlist_video_urlsr   ÚpartZpoolprocr   r   r   rS   ©   s(   ÿ

ÿ
ÿ
zKinetics._download_videosc           
   
   C   s0  t  | jd¡}tt  || j› d�¡ƒs!t| j| j j| jd�|ƒ t  || j› d�¡}d}t	|ƒ�]}t
 |¡}|D ]L}|j|d t|d ƒt|d ƒd�}|d	  d
d¡ dd¡ dd¡ dd¡}tjt  | j|¡dd� t  | j|¡}	t  |	¡r…t |	t  | j||¡¡ q9W d  ƒ dS 1 s‘w   Y  dS )uü   move videos from
        split_folder/
            â”œâ”€â”€ clip1.avi
            â”œâ”€â”€ clip2.avi

        to the correct format as described below:
        split_folder/
            â”œâ”€â”€ class1
            â”‚   â”œâ”€â”€ clip1.avi

        Úannotationsz.csvrW   z{ytid}_{start:06}_{end:06}.mp4Ú
youtube_idZ
time_startZtime_end)ZytidÚstartÚendÚlabelú Ú_ú'Ú ú(ú)T)Úexist_okN)r   rE   r%   r   r(   r   Ú_ANNOTATION_URLSr'   r_   ra   ÚcsvÚ
DictReaderÚintÚreplaceÚosÚmakedirsrC   Úisfile)
rL   Úannotation_pathrg   Zfile_fmtstrZcsvfileÚreaderÚrowÚfrk   Zdownloaded_filer   r   r   rT   Ç   s0   



ý(
þ€ö"þzKinetics._make_ds_structurec                 C   s   | j jS r   )rK   Úmetadata©rL   r   r   r   r   ê   s   zKinetics.metadatac                 C   s
   | j  ¡ S r   )rK   Ú	num_clipsr€   r   r   r   Ú__len__î   s   
zKinetics.__len__Úidxc                 C   s@   | j  |¡\}}}}| j| d }| jd ur|  |¡}|||fS )Nr   )rK   Úget_cliprJ   r+   )rL   rƒ   ÚvideoÚaudioÚinfoÚ	video_idxrk   r   r   r   Ú__getitem__ñ   s
   


zKinetics.__getitem__)r    r!   Nr   Nr"   Fr   r   Nr   r   r   r   r   Fr$   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r^   rs   r   Ústrr   rv   r	   r   r
   Úboolr   r   rH   rF   rS   rT   Úpropertyr   r‚   r   r‰   Ú__classcell__r   r   rO   r   r      s–    :ýý
ì
þýüûúùø
	÷
öõôóòñðïîíìë
B

#$r   )#rt   rx   rR   rY   Ú	functoolsr   Úmultiprocessingr   r   Úpathlibr   Útypingr   r   r   r	   r
   r   Útorchr   Úfolderr   r   Úutilsr   r   r   r   Úvideo_utilsr   Úvisionr   rŽ   r   r   r   r   r   r   Ú<module>   s      