U
    ­Ê¹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
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 eeeddœdd„ZG dd„ deƒZdS )é    N)Úpartial)ÚPool)Úpath)ÚAnyÚCallableÚDictÚOptionalÚTuple)Ú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   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/datasets/kinetics.pyÚ_dl_wrap   s    r   c                       sî   e Zd ZdZddddœZddddœZd#eeeeee eee	 e
edf eeeeeeef  eeeeeeeddœ‡ fdd„Zddœdd„Zddœdd„Zddœdd„Zeeeef dœdd„ƒZedœdd„Zee
eeef d œd!d"„Z‡  ZS )$ÚKineticsuz  `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 (string): Root directory of the Kinetics Dataset.
            Directory should be structured as follows:
            .. code::

                root/
                â”œâ”€â”€ split
                â”‚   â”œâ”€â”€  class1
                â”‚   â”‚   â”œâ”€â”€  clip1.mp4
                â”‚   â”‚   â”œâ”€â”€  clip2.mp4
                â”‚   â”‚   â”œâ”€â”€  clip3.mp4
                â”‚   â”‚   â”œâ”€â”€  ...
                â”‚   â”œâ”€â”€  class2
                â”‚   â”‚   â”œâ”€â”€   clipx.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)Ú400Ú600Ú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   ©Z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dddgd�| _|| _|
| _|| _|| _|rXtdƒ || _d| _d}|	r|t	d	ƒ‚n$t
 ||¡| _t |d
dddgd�| _|	rˆ|  ¡  tƒ  | j¡ t| jƒ\| _}t| j||d d�| _dd„ | jD ƒ}t||||||||||||d�| _|| _d S )Nr%   r   r   r   )ÚargZvalid_valueszUsing legacy structureÚunknownZTHWCz2Cannot download the videos using legacy_structure.r&   r    ÚvalÚtest)Zis_valid_filec                 S   s   g | ]}|d  ‘qS )r   r   )Ú.0Úxr   r   r   Ú
<listcomp>Œ   s     z%Kinetics.__init__.<locals>.<listcomp>)r-   r/   r0   r1   r2   r3   r5   )r   r%   r*   r,   r#   r4   Ú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-   r.   r/   r0   r1   r2   r3   r4   r5   Zclass_to_idxZ
video_list©Ú	__class__r   r   rC   [   sF    
ôzKinetics.__init__)r   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_videosr=   Ú_make_ds_structure)rG   ZticZtocZtoc2r   r   r   rA   �   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|ƒstt||ƒ t|ƒ�}dd„ | ¡  ¡ D ƒ}W 5 Q R X | jdkrÂ|D ]}t||| jƒ q¬n$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   r<   ¼   s     z-Kinetics._download_videos.<locals>.<listcomp>r   N)r   Úexistsr>   ÚRuntimeErrorr@   r#   Ú	_TAR_URLSr%   Úformatr&   Úbasenamer   r   ÚopenÚreadÚ
splitlinesr,   r   r   r   r   Úmap)
rG   Ztar_pathZfile_list_pathZ	split_urlZsplit_url_filepathÚfileZlist_video_urlsr   ÚpartZpoolprocr   r   r   rL   ¨   s$    ÿ

 

zKinetics._download_videosc           
   
   C   s  t  | jd¡}tt  || j› d�¡ƒsBt| j| j j| jd�|ƒ t  || j› d�¡}d}t	|ƒ�®}t
 |¡}|D ]˜}|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  |	¡rrt |	t  | j||¡¡ qrW 5 Q R X 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.csvrO   z{ytid}_{start:06}_{end:06}.mp4Z
youtube_idZ
time_startZtime_end)ZytidÚstartÚendÚlabelú Ú_ú'Ú ú(ú)T)Úexist_okN)r   r@   r#   r   r&   r   Ú_ANNOTATION_URLSr%   rW   rY   ÚcsvÚ
DictReaderÚintÚreplaceÚosÚmakedirsr>   Úisfile)
rG   Zannotation_pathr_   Zfile_fmtstrZcsvfileÚreaderÚrowÚfrb   Zdownloaded_filer   r   r   rM   Æ   s*    



ý(
þzKinetics._make_ds_structurec                 C   s   | j jS r   )rF   Úmetadata©rG   r   r   r   ru   é   s    zKinetics.metadatac                 C   s
   | j  ¡ S r   )rF   Z	num_clipsrv   r   r   r   Ú__len__í   s    zKinetics.__len__)Úidxr   c                 C   s@   | j  |¡\}}}}| j| d }| jd k	r6|  |¡}|||fS )Nr   )rF   Zget_cliprE   r)   )rG   rx   ZvideoZaudioÚinfoZ	video_idxrb   r   r   r   Ú__getitem__ð   s
    

zKinetics.__getitem__)r   r    Nr   Nr!   Fr   r   Nr   r   r   r   r   Fr"   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rV   rj   Ústrrm   r   r   r	   Úboolr   r   rC   rA   rL   rM   Úpropertyru   rw   r
   rz   Ú__classcell__r   r   rH   r   r      sn   :ýý
                 ì
ëB#r   ) rk   ro   rK   rQ   Ú	functoolsr   Úmultiprocessingr   r   Útypingr   r   r   r   r	   Ztorchr
   Úfolderr   r   Úutilsr   r   r   r   Zvideo_utilsr   Zvisionr   r   r   r   r   r   r   r   Ú<module>   s   