o
    ϯic(                     @   s^  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZ d dlmZ eG d	d
 d
eZeG dd deZeG dd deZeG dd deZeG dd deZG dd deZG dd dZG dd dZdedefddZ	d$deeef dedefdd Z	d$d!eeef dedefd"d#Z dS )%    N)	dataclass)Enum)DictOptionalUnion)EncodedVideo)
FrameVideo)DataclassFieldCasterload_dataclass_dict_from_csv#save_dataclass_objs_to_headered_csv)Videoc                   @   s"   e Zd ZU dZeed< eed< dS )EncodedVideoInfozH
    Class representing the location of an available encoded video.
    video_id	file_pathN)__name__
__module____qualname____doc__str__annotations__ r   r   \/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/data/dataset_manifest_utils.pyr      s   
 r   c                   @   sJ   e Zd ZU dZeed< eed< eed< eed< eed< eed< eed< d	S )
VideoFrameInfozN
    Class representing the locations of all frames that compose a video.
    r   locationframe_file_stemframe_string_lengthmin_frame_numbermax_frame_numberfile_extensionNr   r   r   r   r   r   intr   r   r   r   r      s   
 r   c                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )	VideoInfoza
    Class representing the video-level metadata of a video from an arbitrary video dataset.
    r   
resolutiondurationfpsN)r   r   r   r   r   r   floatr   r   r   r   r!   ,      
 r!   c                   @   s&   e Zd ZU eed< eed< eed< dS )VideoClipInfor   
start_time	stop_timeN)r   r   r   r   r   r%   r   r   r   r   r'   8   s   
 r'   c                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )ImageFrameInfozI
    Class representing the metadata (and labels) for a single frame
    r   frame_idframe_numberframe_file_pathNr   r   r   r   r   r*   ?   r&   r*   c                   @   s   e Zd ZdZdZdS )VideoDatasetType      N)r   r   r   Framer   r   r   r   r   r.   K   s    r.   c                   @   s6   e Zd Zedee dededeeef fddZ	dS )ImageDatasetframe_manifest_file_pathvideo_info_file_pathmultithreaded_ioreturnc                 C   s   t |td}t | td}t|| i }|D ]6}t|||}|| }||j }	t|t|	j	|	j
D ]\}
}tjtj|
d }t||||
||< q2q|S )Nr   r   )r
   r!   r   VideoDataset/_remove_video_info_missing_or_incomplete_videos_frame_number_to_filepathsr   zipranger   r   ospathsplitextbasenamer*   )r3   r4   r5   video_infosvideo_framesimage_infosr   frame_filepaths
video_infovideo_frame_infoframe_filepathr,   r+   r   r   r   _load_imagesQ   s8   


zImageDataset._load_imagesN)
r   r   r   staticmethodr   r   boolr   r*   rG   r   r   r   r   r2   P   s    
r2   c                   @   s   e Zd Zedee dedededeee	f f
ddZ
eded	eeef defd
dZeded	eeef fddZededeeef d	eeef dee fddZedeeeeef f d	eeef ddfddZdS )r7   video_data_manifest_file_pathr4   r5   dataset_typer6   c                 C   s>   t |td}|tjkrt| ||S |tjkrt| |S d S )Nr   )r
   r!   r.   r1   r7   _load_frame_videosr   _load_encoded_videos)rJ   r4   r5   rK   r@   r   r   r   _load_videosv   s   

zVideoDataset._load_videosr3   r@   c                    s.   t | tdt  fddD S )Nr   c              	      s4   i | ]}|t t|| j| j d qS ))video_frame_pathsr#   r$   r5   )r   r7   r9   r#   r$   ).0r   r5   rA   r@   r   r   
<dictcomp>   s    	z3VideoDataset._load_frame_videos.<locals>.<dictcomp>)r
   r   r7   r8   )r3   r@   r5   r   rQ   r   rL      s   	zVideoDataset._load_frame_videos encoded_video_manifest_file_pathc                 C   s*   t | td}t|| dd | D S )Nr   c                 S   s   i | ]\}}|t |jqS r   )r   	from_pathr   )rP   r   encoded_video_infor   r   r   rR      s    z5VideoDataset._load_encoded_videos.<locals>.<dictcomp>)r
   r   r7   r8   items)rS   r@   encoded_video_infosr   r   r   rM      s   z!VideoDataset._load_encoded_videosr   rA   c                 C   s   ||  }||j  }g }|j|j d }t|D ]A}||j }||jk s(||jkr+ d S t|}	|j}
|jt|	 t|
 }d| }|
 | |	 d|j }|	|j
 d|  q|S )Nr/   0./)r   r   r   r;   r   r   r   lenr   appendr   )r   rA   r@   rD   rE   rC   
num_framesframe_indexr,   frame_path_indexframe_prefixnum_zero_padzero_paddingframe_componentr   r   r   r9      s2   



z'VideoDataset._frame_number_to_filepathsvideo_data_infosNc                 C   s   t |}|D ]7}|| }|| vr||= qt| | tkr=| | }t|j|j }|j|j }t|| |jkr=| |= ||= qt | }|D ]	}||vrM| |= qDd S N)	listtyper   roundr#   r$   r   r   abs)rd   r@   	video_idsr   rD   video_frames_infoexpected_framesr]   r   r   r   r8      s.   z<VideoDataset._remove_video_info_missing_or_incomplete_videos)r   r   r   rH   r   r   rI   r.   r   r   rN   r!   rL   rM   r   r9   r   r   r8   r   r   r   r   r7   u   sZ    




#
r7   time_strr6   c              	   C   s^   dD ]$}zt j | |}t j dd}t||  W   S  ty&   Y qw td|  d)z
    Get Seconds from timestamp of form 'HH:MM:SS'.

    Args:
        time_str (str)

    Returns:
        float of seconds

    )z%H:%M:%S.%fz%H:%M:%S z/No valid data format found for provided string rY   )datetimestrptimer%   total_seconds
ValueError)rm   fmttime_since_min_timemin_timer   r   r   get_seconds_from_hms_time   s   rv   rW   	file_namec                 C   0   |du rt   dn|}tt|  | |S )a  
    Saves the encoded video dictionary as a csv file that can be read for future usage.

    Args:
        video_frames (Dict[str, EncodedVideoInfo]):
            Dictionary mapping video_ids to metadata about the location of
            their video data.

        file_name (str):
            location to save file (will be automatically generated if None).

    Returns:
        string of the filename where the video info is stored.
    Nz/encoded_video_manifest.csvr<   getcwdr   rf   values)rW   rw   r   r   r   save_encoded_video_manifest     r|   rA   c                 C   rx   )a  
    Saves the video frame dictionary as a csv file that can be read for future usage.

    Args:
        video_frames (Dict[str, VideoFrameInfo]):
            Dictionary mapping video_ids to metadata about the location of
            their video frame files.

        file_name (str):
            location to save file (will be automatically generated if None).

    Returns:
        string of the filename where the video info is stored.
    Nz/video_frame_metadata.csvry   )rA   rw   r   r   r   save_video_frame_info&  r}   r~   re   )!ro   r<   dataclassesr   enumr   typingr   r   r   pytorchvideo.data.encoded_videor   pytorchvideo.data.frame_videor   pytorchvideo.data.utilsr	   r
   r   pytorchvideo.data.videor   r   r   r!   r'   r*   r.   r2   r7   r   r%   rv   r|   r~   r   r   r   r   <module>   sP   	% 


