o
    ϯi                   
   @   s   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mZm	Z	m
Z
mZm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 G d	d
 d
ejjjZ	dde	e dedee	e e	e f fddZdS )    N)defaultdict)AnyCallableListOptionalTupleType)	g_pathmgr)ClipSampler)
FrameVideo   )MultiProcessSamplerc                   @   s   e Zd ZdZdZejjjdddfde	de
deejjj deeegef  d	e	d
ee ddfddZedee d
edee fddZedejjjfddZdefddZdd ZdS )CharadesaV  
    Action recognition video dataset for
    `Charades <https://prior.allenai.org/projects/charades>`_ stored as image frames.

    This dataset handles the parsing of frames, loading and clip sampling for the
    videos. All io is done through :code:`iopath.common.file_io.PathManager`, enabling
    non-local storage uri's to be used.
       N 	data_pathclip_samplervideo_sampler	transformvideo_path_prefixframes_per_clipreturnc                 C   sv   t jd || _|| _t||d\| _| _| _|| j| _	d| _
|dur-tjtj|dnd| _d| _d| _d| _dS )a  
        Args:
            data_path (str): Path to the data file. This file must be a space
                separated csv with the format: (original_vido_id video_id frame_id
                path_labels)

            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 (Optional[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__().

            video_path_prefix (str): prefix path to add to all paths from data_path.

            frames_per_clip (Optional[int]): The number of frames per clip to sample.
        z&PYTORCHVIDEO.dataset.Charades.__init__)prefixN)r           )torch_C_log_api_usage_once
_transform_clip_sampler_read_video_paths_and_labels_path_to_videos_labels_video_labels_video_sampler_video_sampler_iter	functoolspartialr   _sample_clip_frames_frame_filter_loaded_video_loaded_clip_next_clip_start_time)selfr   r   r   r   r   r    r-   N/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/data/charades.py__init__    s(   

zCharades.__init__frame_indicesc                    sB   t  }td|d |}t|d|d  } fdd|D S )z
        Args:
            frame_indices (list): list of frame indices.
            frames_per+clip (int): The number of frames per clip to sample.

        Returns:
            (list): Outputs a subsampled list with num_samples frames.
        r   r   c                       g | ]} | qS r-   r-   ).0idxr0   r-   r.   
<listcomp>j       z0Charades._sample_clip_frames.<locals>.<listcomp>)lenr   linspaceclamplong)r0   r   
num_framesindicesr-   r4   r.   r'   Z   s   zCharades._sample_clip_framesc                 C   s   | j S N)r#   r,   r-   r-   r.   r   l   s   zCharades.video_samplerc                    s   j stt j _  jr j\}nt j  j }t|}|f _ 	 j
|ji \}}}}}|dkrC||| j _ jd  jd }}	| _
|rYd _d _
 fddtt|	t|	d D }
||
 j t||d	} jdur |}|S )
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   videor0   Nr   c                    s   g | ]	} j  | qS r-   )r!   )r2   ir,   video_indexr-   r.   r5      s    z%Charades.__next__.<locals>.<listcomp>r   )r?   labelvideo_label
video_namerB   
clip_index	aug_index)r$   iterr   r#   r)   nextr    r   from_frame_pathsr   r+   durationget_clipr(   r*   rangeminmaxr"   strr   )r,   r?   path_to_video_frames
clip_startclip_endrF   rG   is_last_clipframesr0   labels_by_framesample_dictr-   rA   r.   __next__p   sD   





	
zCharades.__next__c                 C   s   | S r=   r-   r>   r-   r-   r.   __iter__   s   zCharades.__iter__)__name__
__module____qualname____doc__NUM_CLASSESr   utilsdataRandomSamplerrP   r
   r   Samplerr   r   dictr   intr/   staticmethodr   r'   propertyr   rX   rY   r-   r-   r-   r.   r      sD    

:Cr   r   video_path_label_filer   r   c                    s
  t t t tt| dM}tj|dd}|D ];}t|dks"J |d }tj	||d } | 
| |d dd	}g }|rLd
d |dD }| 
| qW d   n1 s^w   Y    }	 fdd|	D  fdd|	D dd D }
 |
fS )ap  
    Args:
        video_path_label_file (List[str]): a file that contains frame paths for each
            video and the corresponding frame label. The file must be a space separated
            csv of the format:
                `original_vido_id video_id frame_id path labels`

        prefix (str): prefix path to add to all paths from video_path_label_file.

    r )	delimiter   original_vido_idpathlabels"r   c                 S   s   g | ]}t |qS r-   )rd   )r2   xr-   r-   r.   r5      r6   z0_read_video_paths_and_labels.<locals>.<listcomp>,Nc                    r1   r-   r-   r2   key)image_pathsr-   r.   r5      r6   c                    r1   r-   r-   rr   )rn   r-   r.   r5      r6   c                 S   s   g | ]}t ttj| qS r-   )listset	itertoolschain)r2   
label_listr-   r-   r.   r5      s    )r   ru   r	   opencsv
DictReaderr7   osrm   joinappendreplacesplitkeys)rg   r   f
csv_readerrowrE   rm   frame_labelsry   video_namesvideo_labelsr-   )rt   rn   r.   r      s*   
r   )r   )r{   r%   rw   r}   collectionsr   typingr   r   r   r   r   r   r   torch.utils.dataiopath.common.file_ior	   pytorchvideo.data.clip_samplingr
   pytorchvideo.data.frame_videor   r_   r   r`   IterableDatasetr   rP   rd   r   r-   r-   r-   r.   <module>   s,     &