Ë
    Dêñi‰&  ã                   óò   — 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 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y)é    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                 ó   — t        || |«       y ©N)r   )r   r   r   s      ú_/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/torchvision/datasets/kinetics.pyÚ_dl_wrapr      s   € Ü   w°	Õ:ó    c            )       ó@  ‡ — 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 ))Ú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}.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                 óZ  •— t        |dg d¢¬«      | _        || _        |
| _        || _        || _        |r|| _        d| _        d}|	r;t        d«      ‚t        j                  ||«      | _        t        |dg d¢¬«      | _        |	r| j                  «        t        ‰| �5  | j                  «       t        | j                  «      \  | _        }t!        | j                  ||d ¬	«      | _        | j"                  D �cg c]  }|d
   ‘Œ	 }}t%        ||||||||||||¬«      | _        || _        y c c}w )Nr&   r    )ÚargÚvalid_valuesÚunknownÚTHWCz2Cannot download the videos using legacy_structure.r'   )ÚtrainÚvalÚtest)Úis_valid_filer   )r.   r0   r1   r2   r3   r4   r6   )r   r&   r+   r-   r$   r5   Ú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/   r0   r1   r2   r3   r4   r5   r6   Úclass_to_idxÚxÚ
video_listÚ	__class__s                          €r   rE   zKinetics.__init__[   s(  ø€ ô0 *¨+¸=ÒWlÔmˆÔØ$ˆŒØ$8ˆÔ!àˆŒ	ØˆŒáØ $ˆDÔØ"ˆ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˜q�a˜“dÐ1ˆ
Ð1Ü%ØØØØØ!Ø#Ø%Ø'Ø!5Ø)Ø+Ø'ô
ˆÔð #ˆ�ùò 2s   Ã6D(c                 óD   — | j                  «        | j                  «        y)zEDownloads all the videos to the _root_ folder in the expected format.N)Ú_download_videosÚ_make_ds_structure©rI   s    r   rC   z$Kinetics.download_and_process_videosœ   s   € à×ÑÔØ×ÑÕ!r   c                 óh  — t        j                  | j                  «      ryt        j                  | j                  d«      }t        j                  | j                  d«      }| j
                  | j                     j                  | j                  ¬«      }t        j                  |t        j                  |«      «      }t        |«      st        ||«       t        |«      5 }|j                  «       j                  «       D �cg c]#  }t        j                   j#                  |d¬«      ‘Œ% }}ddd«       | j$                  dk(  rD ]  }t'        ||| j                  «       Œ yt)        t*        || j                  «      }t-        | j$                  «      }	|	j/                  |«       yc c}w # 1 sw Y   ŒxY w)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.
        NÚtarsÚfiles©r'   z/,:)Úsafer   )r   Úexistsr@   rB   r$   Ú	_TAR_URLSr&   Úformatr'   Úbasenamer   r   ÚopenÚreadÚ
splitlinesÚurllibÚparseÚquoter-   r   r   r   r   Úmap)
rI   Útar_pathÚfile_list_pathÚ	split_urlÚsplit_url_filepathÚfiler   Úlist_video_urlsÚpartÚpoolprocs
             r   rO   zKinetics._download_videos¡   s^  € ô �;‰;�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Ü˜ NÔ3ÜÐ$Ó%ð 	j¨ØPT×PYÑPYÓP[×PfÑPfÓPhÖiÈœvŸ|™|×1Ñ1°$¸UÐ1ÕCÐiˆOÐi÷	jð ×$Ñ$¨Ò)Ø'ò P�Ü,¨T°8¸T×=NÑ=NÕOñPô œ8 X¨t×/@Ñ/@ÓAˆDÜ˜D×5Ñ5Ó6ˆHØ�L‰L˜˜Õ/ùò j÷	jð 	jús   Ã!F(Ä (F#Ä(F(Æ#F(Æ(F1c           
      ó  — t        j                  | j                  d«      }t        t        j                  || j                  › d�«      «      s=t        | j                  | j                     j                  | j                  ¬«      |«       t        j                  || j                  › d�«      }d}t        |«      5 }t        j                  |«      }|D �]  }|j                  |d   t        |d   «      t        |d   «      ¬«      }|d	   j                  d
d«      j                  dd«      j                  dd«      j                  dd«      }t        j                  t        j                  | j                   |«      d¬«       t        j                  | j                   |«      }	t        j"                  |	«      sŒãt        j                  |	t        j                  | j                   ||«      «       �Œ 	 ddd«       y# 1 sw Y   yxY w)uü   move videos from
        split_folder/
            â”œâ”€â”€ clip1.avi
            â”œâ”€â”€ clip2.avi

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

        Úannotationsz.csvrU   z{ytid}_{start:06}_{end:06}.mp4Ú
youtube_idÚ
time_startÚtime_end)ÚytidÚstartÚendÚlabelú Ú_ú'Ú ú(ú)T)Úexist_okN)r   rB   r$   r   r'   r   Ú_ANNOTATION_URLSr&   rY   r[   ÚcsvÚ
DictReaderÚintÚreplaceÚosÚmakedirsr@   Úisfile)
rI   Úannotation_pathrk   Úfile_fmtstrÚcsvfileÚreaderÚrowÚfrr   Údownloaded_files
             r   rP   zKinetics._make_ds_structure¼   sŸ  € ô Ÿ)™) D§I¡I¨}Ó=ˆÜœtŸy™y¨¸T¿Z¹Z¸LÈÐ:MÓNÔOÜ˜×.Ñ.¨t×/?Ñ/?Ñ@×GÑGÈdÏjÉjÐGÓYÐ[jÔkÜ—i‘i °D·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¸AÓ>öñ÷	÷ 	ñ 	ús   Â:C:G7Æ58G7Ç7H c                 ó.   — | j                   j                  S r   )rH   ÚmetadatarQ   s    r   rŠ   zKinetics.metadataß   s   € à×Ñ×(Ñ(Ð(r   c                 ó6   — | j                   j                  «       S r   )rH   Ú	num_clipsrQ   s    r   Ú__len__zKinetics.__len__ã   s   € Ø×Ñ×)Ñ)Ó+Ð+r   Úidxc                 óª   — | j                   j                  |«      \  }}}}| j                  |   d   }| j                  �| j                  |«      }|||fS )Nr   )rH   Úget_cliprG   r*   )rI   rŽ   ÚvideoÚaudioÚinfoÚ	video_idxrr   s          r   Ú__getitem__zKinetics.__getitem__æ   sY   € Ø(,×(8Ñ(8×(AÑ(AÀ#Ó(FÑ%ˆˆu�d˜IØ—‘˜YÑ'¨Ñ*ˆà�>‰>Ð%Ø—N‘N 5Ó)ˆEà�e˜UÐ"Ð"r   )r!   r<   Nr   N)ÚaviÚmp4Fr   r   Nr   r   r   r   r   FÚTCHW)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rX   rz   r
   Ústrr   r}   r	   r   ÚtupleÚboolÚdictr   rE   rC   rO   rP   ÚpropertyrŠ   r�   r   r•   Ú__classcell__)rM   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   r^   Ú	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   