o
    ϯiK                     @  sZ   d dl mZ d dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ G dd dZdS )    )annotationsN)ListOptionalTuple)	g_pathmgr)make_datasetc                   @  sn   e Zd ZdZedddZeddd	ZedddZ	d d!ddZdd Z	e
de	Z	d"ddZd#ddZdS )$LabeledVideoPathszQ
    LabeledVideoPaths contains pairs of video path and integer index label.
    	data_pathstrreturnc                 C  s6   t |r
t|S t |rt|S t| d)a[  
        Factory function that creates a LabeledVideoPaths object depending on the path
        type.
        - If it is a directory path it uses the LabeledVideoPaths.from_directory function.
        - If it's a file it uses the LabeledVideoPaths.from_csv file.
        Args:
            file_path (str): The path to the file to be read.
         not found.)r   isfiler   from_csvisdirfrom_directoryFileNotFoundError)clsr	    r   Y/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/data/labeled_video_paths.py	from_path   s
   



zLabeledVideoPaths.from_path	file_pathc                 C  s   t |sJ | dg }t |d1}|  D ]"}|dd}t|dkr0|d }d}n|\}}||t|f qW d   n1 sHw   Y  t|dks[J d| d| |S )	a"  
        Factory function that creates a LabeledVideoPaths object by reading a file with the
        following format:
            <path> <integer_label>
            ...
            <path> <integer_label>

        Args:
            file_path (str): The path to the file to be read.
        r   rN   r   Failed to load dataset from .)	r   existsopenread
splitlinesrsplitlenappendint)r   r   video_paths_and_labelf
path_label
line_splitlabelr   r   r   r   $   s    
zLabeledVideoPaths.from_csvdir_pathc                   s   t |sJ | dtdd t| D   fddtt D }t||dd}t|dks<J d	| d
| |S )a  
        Factory function that creates a LabeledVideoPaths object by parsing the structure
        of the given directory's subdirectories into the classification labels. It
        expects the directory format to be the following:
             dir_path/<class_name>/<video_name>.mp4

        Classes are indexed from 0 to the number of classes, alphabetically.

        E.g.
            dir_path/class_x/xxx.ext
            dir_path/class_x/xxy.ext
            dir_path/class_x/xxz.ext
            dir_path/class_y/123.ext
            dir_path/class_y/nsdf3.ext
            dir_path/class_y/asd932_.ext

        Would produce two classes labeled 0 and 1 with 3 videos paths associated with each.

        Args:
            dir_path (str): Root directory to the video class directories .
        r   c                 s  s    | ]
}|  r|jV  qd S N)is_dirname).0r%   r   r   r   	<genexpr>b   s    z3LabeledVideoPaths.from_directory.<locals>.<genexpr>c                   s   i | ]} | |qS r   r   )r-   iclassesr   r   
<dictcomp>d   s    z4LabeledVideoPaths.from_directory.<locals>.<dictcomp>)mp4avi)
extensionsr   r   r   )	r   r   sortedpathlibPathiterdirranger!   r   )r   r)   class_to_idxr$   r   r0   r   r   F   s   
z LabeledVideoPaths.from_directory paths_and_labelsList[Tuple[str, Optional[int]]]Nonec                 C  s   || _ || _dS )z
        Args:
            paths_and_labels [(str, int)]: a list of tuples containing the video
                path and integer label.
        N)_paths_and_labels_path_prefix)selfr=   path_prefixr   r   r   __init__m   s   
zLabeledVideoPaths.__init__c                 C  s
   || _ d S r*   )rA   )rB   prefixr   r   r   rC   x   s   
zLabeledVideoPaths.path_prefixNindexr#   Tuple[str, int]c                 C  s&   | j | \}}tj| j|d|ifS )z
        Args:
            index (int): the path and label index.

        Returns:
            The path and label tuple for the given index.
        r(   )r@   ospathjoinrA   )rB   rF   rI   r(   r   r   r   __getitem__}   s   zLabeledVideoPaths.__getitem__c                 C  s
   t | jS )zQ
        Returns:
            The number of video paths and label pairs.
        )r!   r@   )rB   r   r   r   __len__   s   
zLabeledVideoPaths.__len__)r	   r
   r   r   )r   r
   r   r   )r)   r
   r   r   )r<   )r=   r>   r   r?   )rF   r#   r   rG   )r   r#   )__name__
__module____qualname____doc__classmethodr   r   r   rD   rC   propertyrK   rL   r   r   r   r   r      s    !'

r   )
__future__r   rH   r7   typingr   r   r   iopath.common.file_ior   torchvision.datasets.folderr   r   r   r   r   r   <module>   s   