
    ڧg(                        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e e
f         dee e
f         de ddfdZ! G 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                 (    t          || |           d S N)r   )r   r   r   s      Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/datasets/kinetics.py_dl_wrapr      s     w	:::::    c            )       v    e Zd ZdZddddZddddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.d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&Zd/d'Zd/d(Zd/d)Zed%eeef         fd*            Zd%e	fd+Zd,e	d%eeee	f         fd-Z xZS )0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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   avi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                    t          |dg d          | _        || _        |
| _        || _        || _        |r1t          d           || _        d| _        d}|	rt          d          n3t          j        ||          | _        t          |dg d	          | _        |	r|                                  t                                          | j                   t          | j                  \  | _        }t#          | j        ||d 
          | _        d | j        D             }t'          ||||||||||||          | _        || _        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                     g | ]
}|d          S )r    ).0xs     r   
<listcomp>z%Kinetics.__init__.<locals>.<listcomp>   s    111qad111r   )r5   r7   r8   r9   r:   r;   r=   )r   r-   r2   r4   r+   r<   printsplit_folderr.   
ValueErrorr   joindownload_and_process_videossuper__init__r   classesr   samplesr   video_clipsr1   )selfr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   class_to_idx
video_list	__class__s                         r   rQ   zKinetics.__init__\   s   0 *+=WlWlWlmmm$$8!	 		c*+++ $D"DJ"M W !UVVVW !%	$ 6 6D'7IaIaIabbbDJ 	/,,...###%1$2C%D%D"l#D$5|Z_cddd11DL111
%!#%'!5)+'
 
 
 #r   c                 L   t          j                     }|                                  t          j                     }t          d||z
  dz             |                                  t          j                     }t          d||z
  dz             t          d||z
  dz             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_videosrK   _make_ds_structure)rU   tictoctoc2s       r   rO   z$Kinetics.download_and_process_videos   s    ikkikk5c	R7GHHH!!!y{{4tczR6GHHH-s
b/@AAAAAr   c                 :   t          j        | j                  rt          d| j         d          t          j        | j        d          }t          j        | j        d          }| j        | j                                     | j	                  }t          j        |t          j
        |                    }t          |          st          ||           t          |          5 }d |                                                                D             }ddd           n# 1 swxY w Y   | j        dk    r|D ]}t#          ||| j                   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.tarsfilesr.   c                 P    g | ]#}t           j                            |d           $S )z/,:)safe)urllibparsequote)rH   r   s     r   rJ   z-Kinetics._download_videos.<locals>.<listcomp>   s-    iiiv|11$U1CCiiir   Nr   )r   existsrL   RuntimeErrorrN   r+   	_TAR_URLSr-   formatr.   basenamer   r   openread
splitlinesr4   r   r   r   r   map)
rU   tar_pathfile_list_path	split_urlsplit_url_filepathfilelist_video_urlsr   partpoolprocs
             r   r\   zKinetics._download_videos   s    ;t()) 	^!2 ^ ^ ^   9TY//49g66N4#34;;$*;MM	!Y~t}Y7O7OPP122 	4N333$%% 	jiiPTPYPYP[P[PfPfPhPhiiiO	j 	j 	j 	j 	j 	j 	j 	j 	j 	j 	j 	j 	j 	j 	j $))' P P,T8T=NOOOOP P 8Xt/@AADD566HLL/////s   &1D##D'*D'c           
         t          j        | j        d          }t          t          j        || j         d                    s9t          | j        | j                                     | j                  |           t          j        || j         d          }d}t          |          5 }t          j        |          }|D ])}|                    |d         t          |d                   t          |d                             }|d	                             d
d                              dd                              dd                              dd          }t          j        t          j        | j        |          d           t          j        | j        |          }	t          j        |	          r.t          j        |	t          j        | j        ||                     +	 ddd           dS # 1 swxY 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.csvrd   z{ytid}_{start:06}_{end:06}.mp4
youtube_id
time_starttime_end)ytidstartendlabel _' ()T)exist_okN)r   rN   r+   r   r.   r   _ANNOTATION_URLSr-   rm   ro   csv
DictReaderintreplaceosmakedirsrL   isfile)
rU   annotation_pathr|   file_fmtstrcsvfilereaderrowfr   downloaded_files
             r   r]   zKinetics._make_ds_structure   s'    )DI}==tyTZ:M:M:MNNOO 	l.t/?@GGdjGYY[jkkkiDJ1D1D1DEE6+ 	'^G,,F  &&\*c,/00C
O,, '  
 G,,S#66>>sBGGOOPSUWXX``adfhiiDId&7??$OOOO"&)D,=q"A"A;// J'	$"3UA>>  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ,EG<<H H c                     | j         j        S r   )rT   metadatarU   s    r   r   zKinetics.metadata   s    ((r   c                 4    | j                                         S r   )rT   	num_clipsr   s    r   __len__zKinetics.__len__   s    ))+++r   idxc                     | j                             |          \  }}}}| j        |         d         }| j        |                     |          }|||fS )Nr   )rT   get_cliprS   r1   )rU   r   videoaudioinfo	video_idxr   s          r   __getitem__zKinetics.__getitem__   sX    (,(8(A(A#(F(F%udIY'*>%NN5))EeU""r   )r#   r&   Nr   Nr'   Fr   r   Nr   r   r   r   r   Fr*   )r   N)__name__
__module____qualname____doc__rl   r   r   strr   r   r
   r   r   boolr	   r   rQ   rO   r\   r]   propertyr   r   r   r   __classcell__)rX   s   @r   r!   r!      su       7 7t UT^ I ONS  !$("#(,&4$%:>$% #)@# @#CI@# @# 	@#
 @# SM@#  @# H%@# #s(O@# @# "@# @#  (S#X7@# @# @#  "!@#" #@#$ %@#& '@#( )@#* 
+@# @# @# @# @# @#D	B 	B 	B 	B0 0 0 0<! ! ! !F )$sCx. ) ) ) X), , , , ,#s #uVVS-@'A # # # # # # # #r   r!   )#r   r   r[   rg   	functoolsr   multiprocessingr   r   pathlibr   typingr   r   r	   r
   r   r   torchr   folderr   r   utilsr   r   r   r   video_utilsr   visionr   r   r   r!   rG   r   r   <module>r      s   



 				                                 > > > > > > > > > > > > > > > >       . . . . . . . . ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ # # # # # # ! ! ! ! ! !;eCI& ;5d3C ;3 ;SW ; ; ; ;a# a# a# a# a#} a# a# a# a# a#r   