o
    ϯi"                     @   s   d dl mZ d dlmZ d dlmZmZ d dlmZ de	de
dee	ef fdd	Zde	de
dee	ef fd
dZde	dee	ef fddZdS )    )Dict)	g_pathmgr)EncodedVideoInfoVideoFrameInfo)optional_threaded_foreachdata_directory_pathmultithreadedreturnc                    sN   i t t }dtdtddffdd} fdd|D }t||| S )	a  
    Args:
        data_directory_path (str): Path or URI to EpicKitchenDataset data.
                Data at this path must be a folder of structure:
                    {
                        "{video_id}": [
                            "frame_{frame_number}.{file_extension}",
                            "frame_{frame_number}.{file_extension}",
                            "frame_{frame_number}.{file_extension}",
                        ...]
                    ...}
        multithreaded (bool):
            controls whether io operations are performed across multiple threads.

    Returns:
        Dictionary mapping video_id of available videos to the locations of their
        underlying frame files.
    video_id
video_pathr	   Nc           	   
      s  t t|}|D ]}|dd }|d t|d   }|d\}}|  vr?t| || dt|t|t||d | < q	 |  }|jt|ksLJ |j|ks_J d|  d|j d| |j	d t|ksjJ |j
|ksvJ d	|  t|j|j|j|j|jt||j
d | < q	d S 
N.   _)r
   locationframe_file_stemframe_string_lengthmin_frame_numbermax_frame_numberfile_extensionzFrames for z found in two paths: z and z:Frames with two different file extensions found for video )sortedr   lssplitlenr   intr   r   r   r   r
   r   r   )	r
   r   video_frame_file_namesframer   
frame_namestempath_frame_idvideo_frame_infovideo_frames X/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/data/epic_kitchen/utils.pyadd_video_frames#   sL   
zBbuild_frame_manifest_from_flat_directory.<locals>.add_video_framesc                       g | ]}|  d | fqS /r$   ).0r
   r   r$   r%   
<listcomp>M   s    z<build_frame_manifest_from_flat_directory.<locals>.<listcomp>r   r   strr   )r   r   	video_idsr&   video_pathsr$   r   r#   r%   (build_frame_manifest_from_flat_directory
   s   
*r2   c                    sN   t t }i dtdtddffdd} fdd|D }t||| S )	au  
    Args:
        data_directory_path (str): Path or URI to EpicKitchenDataset data.
            If this dataset is to load from the frame-based dataset:
                Data at this path must be a folder of structure:
    {
        "{participant_id}" : [
            "{participant_id}_{participant_video_id}_{frame_number}.{file_extension}",

        ...],
    ...}

        multithreaded (bool):
                controls whether io operations are performed across multiple threads.

        Returns:
            Dictionary mapping video_id of available videos to the locations of their
            underlying frame files.
    participant_idparticipant_pathr	   Nc              
      s<  t tt|}|D ]}|dd }|d t|d   }|d\}}}|| ks-J | d| }	|	 vrOt|	||	 dt|t|t||d |	< q |	 }
|
jt|ks\J |
j	|ksoJ d|	 d|
j	 d| |
j
d t|kszJ |
j|ksJ d	|	 t|
j|
j	|
j|
j|
jt||
jd |	< qd S r   )r   r   r   r.   r   r   r   r   r   r   r   r   r
   r   r   )r3   r4   participant_framesframe_file_namer   r   path_participant_idpath_video_idr    r
   r!   r"   r$   r%   add_participant_video_framesq   sP   
zPbuild_frame_manifest_from_nested_directory.<locals>.add_participant_video_framesc                    r'   r(   r$   )r*   r3   r+   r$   r%   r,      s    z>build_frame_manifest_from_nested_directory.<locals>.<listcomp>r-   )r   r   participant_idsr9   particpant_pathsr$   r1   r%   *build_frame_manifest_from_nested_directoryV   s    
0r<   c                 C   s^   i }t | D ]%}|  d| }t |D ]}|dd }| d| }t||||< qq|S )aY  
    Creates a dictionary from video_id to EncodedVideoInfo for
    encoded videos in the given directory.

    Args:
        data_directory_path (str): The folder to ls to find encoded
        video files.

    Returns:
        Dict[str, EncodedVideoInfo] mapping video_id to EncodedVideoInfo
        for each file in 'data_directory_path'
    r)   N   )r   r   r   )r   encoded_video_infosr3   participant_folder_pathvideo_file_namer
   video_full_pathr$   r$   r%   ,build_encoded_manifest_from_nested_directory   s   rB   N)typingr   iopath.common.file_ior   (pytorchvideo.data.dataset_manifest_utilsr   r   pytorchvideo.data.utilsr   r.   boolr2   r<   rB   r$   r$   r$   r%   <module>   s.   

L

W
