ó
    Eñi‰&  ã                   óô   • 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      ÚZ/home/mande/repo/quber/.venv/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        X€l        X l        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Ô!àŒ	ØˆŒæØ $ÔØ"ˆDŒJØ"ˆMÞÜ Ð!UÓVÐVð ô !%§	¢	¨$Ó 6ˆDÔÜ'¨°7ÒIaÑbˆDŒJæØ×,Ñ,Ô.ä‰Ñ˜Ÿ™Ô#ä%1°$×2CÑ2CÓ%DÑ"ˆŒ�lÜ# D×$5Ñ$5°|ÀZÐ_cÑdˆŒØ$(§L¢LÓ1¢L˜q�a˜”d¡Lˆ
Ð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)zý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.
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×(Ñ(×)Ñ)ØÜ—9’9˜TŸY™Y¨Ó/ˆÜŸš 4§9¡9¨gÓ6ˆà—N‘N 4×#3Ñ#3Ñ4×;Ñ;À$Ç*Á*Ð;ÐMˆ	Ü!ŸYšY ~´t·}²}ÀYÓ7OÓPÐÜÐ1×2Ñ2Ü˜Ô3ÜÐ$Ô%¨ØPT×PYÑPYÓP[×PfÑPfÔPhÓiÒPhÈœvŸ|™|×1Ñ1°$¸UÐ1ÓCÑPhˆOÐi÷ &ð ×$Ñ$¨Ó)Û'�Ü,¨T¸T×=NÑ=NÖOò (ô œ8 X×/@Ñ/@ÓAˆDÜ˜D×5Ñ5Ó6ˆHØ�L‰L˜˜Õ/ùò 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                   X‡5      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‘  € ô Ÿ)š) D§I¡I¨}Ó=ˆÜœtŸyšy¨¿Z¹Z¸LÈÐ:MÓN×OÑOÜ˜×.Ñ.¨t×/?Ñ/?Ñ@×GÑGÈdÏjÉjÐGÐYÐ[jÔkÜ—i’i ·J±J°<¸tÐ1DÓEˆà6ˆÜ�+Ô 'Ü—^’^ GÓ,ˆFÜ�Ø×&Ñ&Ø˜\Ñ*Ü˜c ,Ñ/Ó0Ü˜C 
™OÓ,ð 'ð �ð
 ˜G™×,Ñ,¨S°#Ó6×>Ñ>¸sÀBÓG×OÑOÐPSÐUWÓX×`Ñ`ÐadÐfhÓi�Ü—’œDŸIšI d×&7Ñ&7¸Ó?È$ÒOÜ"&§)¢)¨D×,=Ñ,=¸qÓ"A�Ü—;’;˜×/Ó/Ü—J’JØ'ÜŸ	š	 $×"3Ñ"3°UÓ>÷ñ ÷ ×Öú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Ñ'¨Ñ*ˆà�>‰>Ñ%Ø—N‘N 5Ó)ˆ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Ñð?#ð ð?#ð ð	?#ð
 ð?#ð ˜S‘Mð?#ð  ð?#ð ˜HÑ%ð?#ð ˜#˜s˜(‘Oð?#ð ð?#ð "ð?#ð ð?#ð  (¨¨S°#¨X©Ñ7ð?#ð ð?#ð ð?#ð  "ð!?#ð" ð#?#ð$ ð%?#ð& ð'?#ð( ð)?#ð* 
÷+?#ð ?#ôB"ô
0ô6!ðF ð)˜$˜s C˜x™.ó )ó ð)ð,˜ô ,ð#˜sð # u¨V°V¸SÐ-@Ñ'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å ç .ß ^Ó ^Ý #Ý !ð;�e˜C ˜IÑ&ð ;°5¸¸d¸Ñ3Cð ;È3ð ;ÐSWô ;ôW#ˆ}õ W#r   