
    h&                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
JrJrJrJr  S SKJr  SS	KJrJr  SS
KJrJrJrJr  SSKJr  SSKJr  S\\\	4   S\\\	4   S\SS4S jr " S S\5      rg)    N)partial)Pool)path)Path)AnyCallableOptionalUnion)Tensor   )find_classesmake_dataset)check_integritydownload_and_extract_archivedownload_urlverify_str_arg)
VideoClips)VisionDatasettarpath	videopathlinereturnc                     [        X U5        g N)r   )r   r   r   s      U/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/datasets/kinetics.py_dl_wrapr      s     	:    c            )       Z  ^  \ rS rSrSrSSSS.rSSS	S.r                 S)S\\\	4   S\
S\S\S\\
   S\
S\\   S\\S4   S\S\
S\
S\\\\4      S\
S\
S\
S\
S\
S\S\SS
4(U 4S  jjjrS*S! jrS*S" jrS*S# jr\S\\\4   4S$ j5       rS\
4S% jrS&\
S\\\\
4   4S' jrS(rU =r$ )+Kinetics   u
  `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}.csvN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                 ^  > [        US/ SQS9U l        Xl        Xl        Xl        UU l        U(       a"  Xl        SU l        SnU	(       a  [        S5      eO-[        R                  " X5      U l        [        US/ SQS9U l        U	(       a  U R                  5         [        TU ]5  U R                  5        [        U R                  5      u  U l        n[!        U R                  UUS S	9U l        U R"                   Vs/ s H  nUS
   PM
     nn[%        UUUUUUUUUUUUS9U l        Xpl        g s  snf )Nr'   r!   )argvalid_valuesunknownTHWCz2Cannot download the videos using legacy_structure.r(   )trainvaltest)is_valid_filer   )r/   r1   r2   r3   r4   r5   r7   )r   r'   r,   r.   r%   r6   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x
video_list	__class__s                          r   rF   Kinetics.__init__[   s$   0 *+=Wlm$$8!	 $"DJ"M !UVV  !%		$ 6D'7IabDJ,,.#%1$2C2C%D"l#D$5$5|Z_cd$(LL1LqadL
1%!#%'!5)+'
 # 2s   9D*c                 D    U R                  5         U R                  5         g)zEDownloads all the videos to the _root_ folder in the expected format.N)_download_videos_make_ds_structurerJ   s    r   rD   $Kinetics.download_and_process_videos   s    !r   c           	         [         R                  " U R                  5      (       a  g[         R                  " U R                  S5      n[         R                  " U R                  S5      nU R
                  U R                     R                  U R                  S9n[         R                  " U[         R                  " U5      5      n[        U5      (       d  [        X25        [        U5       nUR                  5       R                  5        Vs/ s H!  n[        R                   R#                  USS9PM#     nnSSS5        U R$                  S:X  a   W H  n['        XaU R                  5        M     g[)        [*        XR                  5      n[-        U R$                  5      n	U	R/                  UW5        gs  snf ! , (       d  f       N= f)zdownload 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.
Ntarsfilesr(   z/,:)safer   )r   existsrA   rC   r%   	_TAR_URLSr'   formatr(   basenamer   r   openread
splitlinesurllibparsequoter.   r   r   r   r   map)
rJ   tar_pathfile_list_path	split_urlsplit_url_filepathfiler   list_video_urlspartpoolprocs
             r   rQ   Kinetics._download_videos   sO    ;;t(())99TYY/499g6NN4#3#34;;$**;M	!YY~t}}Y7OP1223$%PTPYPYP[PfPfPhiPhv||11$U1CPhOi & $$)',TT=N=NO ( 8X/@/@ADD556HLL/ j &%s   *!F4(F/3F4/F44
Gc           
      $   [         R                  " U R                  S5      n[        [         R                  " XR                   S35      5      (       d:  [        U R                  U R                     R                  U R                  S9U5        [         R                  " XR                   S35      nSn[        U5       n[        R                  " U5      nU GH  nUR                  US   [        US   5      [        US   5      S9nUS	   R                  S
S5      R                  SS5      R                  SS5      R                  SS5      n[        R                  " [         R                  " U R                   U5      SS9  [         R                  " U R                   U5      n	[         R"                  " U	5      (       d  M  [        R                  " U	[         R                  " U R                   X5      5        GM      SSS5        g! , (       d  f       g= f)u   move videos from
split_folder/
    ├── clip1.avi
    ├── clip2.avi

to the correct format as described below:
split_folder/
    ├── class1
    │   ├── clip1.avi

annotationsz.csvrX   z{ytid}_{start:06}_{end:06}.mp4
youtube_id
time_starttime_end)ytidstartendlabel _' ()T)exist_okN)r   rC   r%   r   r(   r   _ANNOTATION_URLSr'   r\   r^   csv
DictReaderintreplaceosmakedirsrA   isfile)
rJ   annotation_pathro   file_fmtstrcsvfilereaderrowfrv   downloaded_files
             r   rR   Kinetics._make_ds_structure   s    ))DII}=tyyZZL:MNOO..t/?/?@GGdjjGY[jkiiJJ<t1DE6+'^^G,F&&\*c,/0C
O, ' 
 G,,S#6>>sBGOOPSUWX``adfhiDIId&7&7?$O"&))D,=,=q"A;;//JJ'		$"3"3U>  s   =C<H=;H
Hc                 .    U R                   R                  $ r   )rI   metadatarS   s    r   r   Kinetics.metadata   s    (((r   c                 6    U R                   R                  5       $ r   )rI   	num_clipsrS   s    r   __len__Kinetics.__len__   s    ))++r   idxc                     U R                   R                  U5      u  p#pEU R                  U   S   nU R                  b  U R                  U5      nX#U4$ )Nr   )rI   get_cliprH   r+   )rJ   r   videoaudioinfo	video_idxrv   s          r   __getitem__Kinetics.__getitem__   sS    (,(8(8(A(A#(F%dY'*>>%NN5)EU""r   )r6   rG   r,   r'   r.   r%   rH   r(   rA   r+   rI   )r"   r=   Nr   N)avimp4Fr   r   Nr   r   r   r   r   FTCHW)r   N)__name__
__module____qualname____firstlineno____doc__r[   r~   r
   strr   r   r	   r   tuplebooldictr   rF   rD   rQ   rR   propertyr   r   r   r   __static_attributes____classcell__)rN   s   @r   r   r      s   7t UT^I ONS !$("#(,&4$%:>$% #)?#CI?# ?# 	?#
 ?# SM?#  ?# H%?# #s(O?# ?# "?# ?#  (S#X7?# ?# ?#  "!?#" #?#$ %?#& '?#( )?#* 
+?# ?#B"
06!F )$sCx. ) ), ,#s #uVVS-@'A # #r   r   ) r   r   ra   	functoolsr   multiprocessingr   r   pathlibr   typingr   r   r	   r
   torchr   folderr   r   utilsr   r   r   r   video_utilsr   visionr   r   r   r    r   r   <module>r      st    
 	       1 1  . ^ ^ # !;eCI& ;5d3C ;3 ;SW ;W#} W#r   