§
    dzni](  ã                   óæ   — 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 dedededdfd„Z G 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                 ó(   — t          || |¦  «         d S ©N)r   )r   r   r   s      ú}/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/torchvision/datasets/kinetics.pyÚ_dl_wrapr      s   € Ý   w°	Ñ:Ô:Ð:Ð:Ð:ó    c            )       óf  ‡ — e Zd ZdZddddœZddddœZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.de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Ú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   ©Ú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Ð1˜q�a˜”dÐ1Ð1Ð1r   )r3   r5   r6   r7   r8   r9   r;   )r   r+   r0   r2   r)   r:   ÚprintÚsplit_folderr,   Ú
ValueErrorr   ÚjoinÚdownload_and_process_videosÚsuperÚ__init__r   Úclassesr   Úsamplesr   Úvideo_clipsr/   )Úselfr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   Úclass_to_idxÚ
video_listÚ	__class__s                         €r   rO   zKinetics.__init__[   s�  ø€ õ0 *¨+¸=ÐWlÐWlÐWlÐmÑmÔmˆÔØ$ˆŒØ$8ˆÔ!àˆŒ	ØˆŒàð 		cÝÐ*Ñ+Ô+Ð+Ø $ˆDÔØ"ˆDŒJØ"ˆMØð WÝ Ð!UÑVÔVÐVðWõ !%¤	¨$°Ñ 6Ô 6ˆDÔÝ'¨°7ÐIaÐIaÐIaÐbÑbÔbˆDŒJàð 	/Ø×,Ò,Ñ.Ô.Ð.å‰Œ×Ò˜œÑ#Ô#Ð#å%1°$Ô2CÑ%DÔ%DÑ"ˆŒ�lÝ# DÔ$5°|ÀZÐ_cÐdÑdÔdˆŒØ1Ð1 D¤LÐ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_videosrI   Ú_make_ds_structure)rS   ÚticÚtocÚtoc2s       r   rM   z$Kinetics.download_and_process_videos�   s›   € åŒi‰kŒkˆØ×ÒÑÔÐÝŒi‰kŒkˆÝÐ5¸¸c¹	ÀRÑ7GÑHÔHÐHØ×ÒÑ!Ô!Ð!ÝŒy‰{Œ{ˆÝÐ4°t¸c±zÀRÑ6GÑ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)rF   r   s     r   rH   z-Kinetics._download_videos.<locals>.<listcomp>¼   s-   € ÐiÐiÐiÈ�vœ|×1Ò1°$¸UÐ1ÑCÔCÐiÐiÐir   Nr   )r   ÚexistsrJ   ÚRuntimeErrorrL   r)   Ú	_TAR_URLSr+   Úformatr,   Úbasenamer   r   ÚopenÚreadÚ
splitlinesr2   r   r   r   r   Úmap)
rS   Útar_pathÚfile_list_pathÚ	split_urlÚsplit_url_filepathÚfileÚlist_video_urlsr   ÚpartÚpoolprocs
             r   rZ   zKinetics._download_videos¨   sñ  € õ Œ;�tÔ(Ñ)Ô)ð 	Ýð^ Ô!2ð ^ð ^ð ^ñô ð õ ”9˜TœY¨Ñ/Ô/ˆÝœ 4¤9¨gÑ6Ô6ˆà”N 4Ô#3Ô4×;Ò;À$Ä*Ð;ÑMÔMˆ	Ý!œY ~µt´}ÀYÑ7OÔ7OÑPÔPÐÝÐ1Ñ2Ô2ð 	4Ý˜ NÑ3Ô3Ð3ÝÐ$Ñ%Ô%ð 	j¨ØiÐiÐPT×PYÒPYÑP[ÔP[×PfÒPfÑPhÔPhÐiÑiÔiˆOð	jð 	jð 	jñ 	jô 	jð 	jð 	jð 	jð 	jð 	jð 	jøøøð 	jð 	jð 	jð 	jð Ô$¨Ò)Ð)Ø'ð Pð P�Ý,¨T°8¸TÔ=NÑOÔOÐOÐOðPð Põ �8 X¨tÔ/@ÑAÔAˆDÝ˜DÔ5Ñ6Ô6ˆHØ�LŠL˜˜Ñ/Ô/Ð/Ð/Ð/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.csvrb   z{ytid}_{start:06}_{end:06}.mp4Ú
youtube_idÚ
time_startÚtime_end)ÚytidÚstartÚendÚlabelú Ú_ú'Ú ú(ú)T)Úexist_okN)r   rL   r)   r   r,   r   Ú_ANNOTATION_URLSr+   rk   rm   ÚcsvÚ
DictReaderÚintÚreplaceÚosÚmakedirsrJ   Úisfile)
rS   Úannotation_pathrz   Úfile_fmtstrÚcsvfileÚreaderÚrowÚfr�   Údownloaded_files
             r   r[   zKinetics._make_ds_structureÆ   s'  € õ œ) D¤I¨}Ñ=Ô=ˆÝ�tœy¨¸T¼ZÐ:MÐ:MÐ:MÑNÔNÑOÔOð 	lÝ˜Ô.¨tÔ/?Ô@×GÒGÈdÌjÐGÑYÔYÐ[jÑkÔkÐkÝ”i °D´JÐ1DÐ1DÐ1DÑEÔEˆà6ˆÝ�+ÑÔð 	 'Ý”^ GÑ,Ô,ˆFØð ñ �Ø×&Ò&Ø˜\Ô*Ý˜c ,Ô/Ñ0Ô0Ý˜C 
œOÑ,Ô,ð 'ñ ô �ð
 ˜Gœ×,Ò,¨S°#Ñ6Ô6×>Ò>¸sÀBÑGÔG×OÒOÐPSÐUWÑXÔX×`Ò`ÐadÐfhÑiÔi�Ý”�DœI dÔ&7¸Ñ?Ô?È$ÐOÑOÔOÐOÝ"&¤)¨DÔ,=¸qÑ"AÔ"A�Ý”;˜Ñ/Ô/ð Ý”JØ'Ýœ	 $Ô"3°U¸AÑ>Ô>ñô ð ùðð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	s   Â,EG<Ç<H ÈH c                 ó   — | j         j        S r   )rR   Úmetadata©rS   s    r   r™   zKinetics.metadataé   s   € àÔÔ(Ð(r   c                 ó4   — | j                              ¦   «         S r   )rR   Ú	num_clipsrš   s    r   Ú__len__zKinetics.__len__í   s   € ØÔ×)Ò)Ñ+Ô+Ð+r   Úidxc                 ó¨   — | j                              |¦  «        \  }}}}| j        |         d         }| j        �|                      |¦  «        }|||fS )Nr   )rR   Úget_cliprQ   r/   )rS   rž   ÚvideoÚaudioÚinfoÚ	video_idxr�   s          r   Ú__getitem__zKinetics.__getitem__ð   sX   € Ø(,Ô(8×(AÒ(AÀ#Ñ(FÔ(FÑ%ˆˆu�d˜IØ”˜YÔ'¨Ô*ˆàŒ>Ð%Ø—N’N 5Ñ)Ô)ˆEà�e˜UÐ"Ð"r   )r!   r$   Nr   Nr%   Fr   r   Nr   r   r   r   r   Fr(   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rj   r‰   ÚstrrŒ   r	   r   r
   Úboolr   r   rO   rM   rZ   r[   Úpropertyr™   r�   r   r¥   Ú__classcell__)rV   s   @r   r   r      sl  ø€ € € € € ð7ð 7ðt UØTØ^ðð €Ið OØNØSðð Ðð !ØØ$(Ø"#Ø(,Ø&4ØØ$%ØØ:>ØØØ$%ØØ ØØ#ð)@#ð @#àð@#ð ð@#ð ð	@#ð
 ð@#ð ˜S”Mð@#ð  ð@#ð ˜HÔ%ð@#ð ˜#˜s˜(”Oð@#ð ð@#ð "ð@#ð ð@#ð  (¨¨S°#¨X¬Ô7ð@#ð ð@#ð ð@#ð  "ð!@#ð" ð#@#ð$ ð%@#ð& ð'@#ð( ð)@#ð* 
ð+@#ð @#ð @#ð @#ð @#ð @#ðD	Bð 	Bð 	Bð 	Bð0ð 0ð 0ð 0ð<!ð !ð !ð !ðF ð)˜$˜s C˜xœ.ð )ð )ð )ñ „Xð)ð,˜ð ,ð ,ð ,ð ,ð#˜sð # u¨V°V¸SÐ-@Ô'Að #ð #ð #ð #ð #ð #ð #ð #r   r   ) rŠ   rŽ   rY   re   Ú	functoolsr   Úmultiprocessingr   r   Útypingr   r   r   r	   r
   Útorchr   Úfolderr   r   Úutilsr   r   r   r   Úvideo_utilsr   Úvisionr   rª   r   r   rE   r   r   ú<module>r¶      si  ðØ 
€
€
€
Ø 	€	€	€	Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7à Ð Ð Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ð ^Ø #Ð #Ð #Ð #Ð #Ð #Ø !Ð !Ð !Ð !Ð !Ð !ð;�cð ; cð ;°ð ;¸ð ;ð ;ð ;ð ;ða#ð a#ð a#ð a#ð a#ˆ}ñ a#ô a#ð a#ð a#ð a#r   