o
    ϯi/                     @  s   d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
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 eeZG d	d
 d
ejjjZejjjddddfdddZdS )    )annotationsN)AnyCallableDictListOptionalTupleType)ClipSampler)VideoPathHandler   )LabeledVideoPaths)MultiProcessSamplerc                   @  sX   e Zd ZdZdZejjjdddfdddZ	e
dd Ze
dd Zd ddZdd ZdS )!LabeledVideoDatasetz
    LabeledVideoDataset handles the storage, loading, decoding and clip sampling for a
    video dataset. It assumes each video is stored as either an encoded video
    (e.g. mp4, avi) or a frame video (e.g. a folder of jpg, or png)
    
   NTpyavlabeled_video_paths List[Tuple[str, Optional[dict]]]clip_samplerr
   video_samplerType[torch.utils.data.Sampler]	transformOptional[Callable[[dict], Any]]decode_audiobooldecoderstrreturnNonec                 C  s   || _ || _|| _|| _|| _d| _|tjjj	kr(t
 | _|| j| jd| _n|| j| _d| _d| _d| _d| _t | _dS )a  
        Args:
            labeled_video_paths (List[Tuple[str, Optional[dict]]]): List containing
                    video file paths and associated labels. If video paths are a folder
                    it's interpreted as a frame video, otherwise it must be an encoded
                    video.

            clip_sampler (ClipSampler): Defines how clips should be sampled from each
                video. See the clip sampling documentation for more information.

            video_sampler (Type[torch.utils.data.Sampler]): Sampler for the internal
                video container. This defines the order videos are decoded and,
                if necessary, the distributed split.

            transform (Callable): This callable is evaluated on the clip output before
                the clip is returned. It can be used for user defined preprocessing and
                augmentations on the clips. The clip output format is described in __next__().

            decode_audio (bool): If True, also decode audio from video.

            decoder (str): Defines what type of decoder used to decode a video. Not used for
                frame videos.
        N)	generator        )_decode_audio
_transform_clip_sampler_labeled_videos_decoder_video_random_generatortorchutilsdataRandomSampler	Generator_video_sampler_video_sampler_iter_loaded_video_label_loaded_clip_next_clip_start_timer   video_path_handler)selfr   r   r   r   r   r    r3   [/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/data/labeled_video_dataset.py__init__   s"    

zLabeledVideoDataset.__init__c                 C  s   | j S )z
        Returns:
            The video sampler that defines video sample order. Note that you'll need to
            use this property to set the epoch for a torch.utils.data.DistributedSampler.
        )r,   r2   r3   r3   r4   r   W   s   z!LabeledVideoDataset.video_samplerc                 C  s
   t | jS )zC
        Returns:
            Number of videos in dataset.
        )lenr   r6   r3   r3   r4   
num_videos`   s   
zLabeledVideoDataset.num_videosdictc                   sz  | j stt| j| _ t| jD ]"}| jr| j\}}}n;t| j }z| j| \}}| j	j
|| j| jd}|||f| _W n tyW } ztd|| W Y d}~qd}~ww | | j|j|\}}}	}
}t|tr|
d dkri | _g }tt|D ] }||| || }|du s|d du rd| _ n|| q|| jdur|d  D ]  fdd|D | j < qn|
dkr|||| _|| _| jdu p| jd du }t|tr|d sn|s|r| jd   d| _d	| _| j  |rtd
|j| q| jd }| jd }||j||	|
d||durd|ini }| jdur0| |}|du r0q|  S td| j d)a  
        Retrieves the next clip based on the clip sampling strategy and video sampler.

        Returns:
            A dictionary with the following format.

            .. code-block:: text

                {
                    'video': <video_tensor>,
                    'label': <index_label>,
                    'video_label': <index_label>
                    'video_index': <video_index>,
                    'clip_index': <clip_index>,
                    'aug_index': <aug_index>,
                }
        r   r   z-Failed to load video with error: {}; trial {}Nr   videoc                   s   g | ]}|  qS r3   r3   ).0xkeyr3   r4   
<listcomp>   s    z0LabeledVideoDataset.__next__.<locals>.<listcomp>r    z Failed to load clip {}; trial {}audio)r;   
video_namevideo_index
clip_index	aug_indexzFailed to load video after z	 retries.) r-   iterr   r,   range_MAX_CONSECUTIVE_FAILURESr.   nextr$   r1   video_from_pathr!   r%   	Exceptionloggerdebugformatr#   r0   duration
isinstancelistr/   r7   get_clipappendkeyscloseresetnamer"   RuntimeError)r2   i_tryr;   	info_dictrD   
video_pathe
clip_startclip_endrE   rF   is_last_cliploaded_clip_listi	clip_dictvideo_is_nullframesaudio_samplessample_dictr3   r>   r4   __next__h   s   






	

zLabeledVideoDataset.__next__c                 C  s@   d | _ tjj }| jd ur|d ur|j|j }| j| | S )N)	r-   r'   r(   r)   get_worker_infor&   seedidmanual_seed)r2   worker_info	base_seedr3   r3   r4   __iter__   s   zLabeledVideoDataset.__iter__)r   r   r   r
   r   r   r   r   r   r   r   r   r   r   )r   r9   )__name__
__module____qualname____doc__rI   r'   r(   r)   r*   r5   propertyr   r8   rh   ro   r3   r3   r3   r4   r      s    ;


}r    Tr   	data_pathr   r   r
   r   r   r   4Optional[Callable[[Dict[str, Any]], Dict[str, Any]]]video_path_prefixr   r   r   r   c           	      C  s(   t | }||_t||||||d}|S )a  
    A helper function to create ``LabeledVideoDataset`` object for Ucf101 and Kinetics datasets.

    Args:
        data_path (str): Path to the data. The path type defines how the data
            should be read:

            * For a file path, the file is read and each line is parsed into a
              video path and label.
            * For a directory, the directory structure defines the classes
              (i.e. each subdirectory is a class).

        clip_sampler (ClipSampler): Defines how clips should be sampled from each
                video. See the clip sampling documentation for more information.

        video_sampler (Type[torch.utils.data.Sampler]): Sampler for the internal
                video container. This defines the order videos are decoded and,
                if necessary, the distributed split.

        transform (Callable): This callable is evaluated on the clip output before
                the clip is returned. It can be used for user defined preprocessing and
                augmentations to the clips. See the ``LabeledVideoDataset`` class for clip
                output format.

        video_path_prefix (str): Path to root directory with the videos that are
                loaded in ``LabeledVideoDataset``. All the video paths before loading
                are prefixed with this path.

        decode_audio (bool): If True, also decode audio from video.

        decoder (str): Defines what type of decoder used to decode a video.

    r:   )r   	from_pathpath_prefixr   )	rv   r   r   r   rx   r   r   r   datasetr3   r3   r4   labeled_video_dataset   s   
*r|   )rv   r   r   r
   r   r   r   rw   rx   r   r   r   r   r   r   r   )
__future__r   loggingtypingr   r   r   r   r   r   r	   torch.utils.datar'   pytorchvideo.data.clip_samplingr
   pytorchvideo.data.videor   r   r   r(   r   	getLoggerrp   rM   r)   IterableDatasetr   r*   r|   r3   r3   r3   r4   <module>   s"   $
 e