files

find_files_like(datapath, pattern) Finds files in a folder whose name matches a pattern
load_video(filename[, as_timeseries, …]) Loads a video from file.
load_video_framerate(filename[, …]) Returns the video frame rate
load_video_generator(filename[, …]) Returns a generator that can load a video from file frame-by-frame.
load_video_metadata(filename[, ffmpeg_path, …]) Returns a video files metadata
save_video(data, filename[, width, height, …]) Saves a video to file.
save_video_sidebyside(videofile, percept, …) Saves both an input video and the percept to file, side-by-side.
set_skvideo_path([ffmpeg_path, libav_path]) Sets the path to the FFMPEG and/or LibAV libraries.

find_files_like

pulse2percept.files.find_files_like(datapath, pattern)[source]

Finds files in a folder whose name matches a pattern

This function looks for files in folder datapath that match a regular expression pattern.

Parameters:

datapath : str

Path to search

pattern : str

A valid regular expression pattern

Examples

# Find all ‘.npz’ files in parent dir >>> files = find_files_like(‘..’, ‘.*.npz$’)

load_video

pulse2percept.files.load_video(filename, as_timeseries=True, as_gray=False, ffmpeg_path=None, libav_path=None)[source]

Loads a video from file.

This function loads a video from file with the help of Scikit-Video, and returns the data either as a NumPy array (if as_timeseries is False) or as a p2p.utils.TimeSeries object (if as_timeseries is True).

Parameters:

filename : str

Video file name

as_timeseries: bool, optional, default: True

If True, returns the data as a p2p.utils.TimeSeries object.

as_gray : bool, optional, default: False

If True, loads only the luminance channel of the video.

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.

Returns:

video : ndarray | p2p2.utils.TimeSeries

If as_timeseries is False, returns video data according to the Scikit-Video standard; that is, an ndarray of dimension (T, M, N, C), (T, M, N), (M, N, C), or (M, N), where T is the number of frames, M is the height, N is the width, and C is the number of channels (will be either 1 for grayscale or 3 for RGB).

If as_timeseries is True, returns video data as a TimeSeries object of dimension (M, N, C), (M, N, T), (M, N, C), or (M, N). The sampling rate corresponds to 1 / frame rate.

Examples

Load a video as a p2p.utils.TimeSeries object:

>>> from skvideo import datasets
>>> video = load_video(datasets.bikes())
>>> video.tsample
0.04
>>> video.shape
(272, 640, 3, 250)

Load a video as a NumPy ndarray:

>>> from skvideo import datasets
>>> video = load_video(datasets.bikes(), as_timeseries=False)
>>> video.shape
(250, 272, 640, 3)

Load a video as a NumPy ndarray and convert to grayscale:

>>> from skvideo import datasets
>>> video = load_video(datasets.bikes(), as_timeseries=False, as_gray=True)
>>> video.shape
(250, 272, 640, 1)

load_video_framerate

pulse2percept.files.load_video_framerate(filename, ffmpeg_path=None, libav_path=None)[source]

Returns the video frame rate

This function returns the frame rate of the video, as given by its metadata field @r_frame_rate’.

Parameters:

filename : str

Video file name

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.

Returns:

fps : float

Video frame rate (frames per second).

load_video_generator

pulse2percept.files.load_video_generator(filename, ffmpeg_path=None, libav_path=None)[source]

Returns a generator that can load a video from file frame-by-frame.

This function returns a generator reader that can load a video from a file frame-by-frame. Every call to reader.nextFrame() will return a single frame of the video as a NumPy array with dimensions (M, N) or (M, N, C), where M is the height, N is the width, and C is the number of channels (will be either 1 for grayscale or 3 for RGB).

Parameters:

filename : str

Video file name

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.

Returns:

reader : skvideo.io.FFmpegReader | skvideo.io.LibAVReader

A Scikit-Video reader object

Examples

>>> from skvideo import datasets
>>> reader = load_video_generator(datasets.bikes())
>>> for frame in reader.nextFrame():
...    pass

load_video_metadata

pulse2percept.files.load_video_metadata(filename, ffmpeg_path=None, libav_path=None)[source]

Returns a video files metadata

This function loads the metadata of a video file, which is returned as a dict. Among the available information are the width and height of the video, the aspect ratio, the frame rate, the bit rate, and the duration.

Parameters:

filename : str

Video file name

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.

Returns:

metadata : dict

A dictionary containing all metadata.

save_video

pulse2percept.files.save_video(data, filename, width=None, height=None, fps=30, ffmpeg_path=None, libav_path=None)[source]

Saves a video to file.

This function stores a NumPy ndarray to file using Scikit-Video.

Parameters:

data : ndarray | p2p.utils.TimeSeries

Video data as a NumPy ndarray must have dimension (T, M, N, C), (T, M, N), (M, N, C), or (M, N), where T is the number of frames, M is the height, N is the width, and C is the number of channels (must be either 1 for grayscale or 3 for RGB).

Video data as a TimeSeries object must have dimension (M, N, C, T) or (M, N, T). The sampling step will be used as the video’s frame rate.

filename : str

Video file name.

width : int, optional

Desired width of the movie. Default: Automatically determined based on height (without changing the aspect ratio). If height is not given, the percept’s original width is used.

height : int, optional

Desired height of the movie. Default: Automatically determined based on width (without changing the aspect ratio). If width is not given, the percept’s original height is used.

fps : int, optional, default: 30

Desired frame rate of the video (frames per second).

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.

Notes

To distinguish between 3-D inputs of shape (T, M, N) and (M, N, C), the last dimension is checked: If it is small (<= 4), it is most likely a color channel - hence the input is interpreted as (M, N, C). Else it is interpreted as (T, M, N).

save_video_sidebyside

pulse2percept.files.save_video_sidebyside(videofile, percept, savefile, fps=30, ffmpeg_path=None, libav_path=None)[source]

Saves both an input video and the percept to file, side-by-side.

This function creates a new video from an input video file and a p2p.utils.TimeSeries object, assuming they correspond to model input and model output, and plots them side-by-side. Both input video and percept are resampled according to fps. The percept is resized to match the height of the input video.

Parameters:

videofile : str

File name of input video.

percept : p2p.utils.TimeSeries

A TimeSeries object with dimension (M, N, C, T) or (M, N, T), where T is the number of frames, M is the height, N is the width, and C is the number of channels.

savefile : str

File name of output video.

fps : int, optional, default: 30

Desired frame rate of output video.

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.

set_skvideo_path

pulse2percept.files.set_skvideo_path(ffmpeg_path=None, libav_path=None)[source]

Sets the path to the FFMPEG and/or LibAV libraries.

If scikit-video complains that either ffmpeg or libav cannot be found, you can set the path to the executables directly. On Unix, these binaries usually live in /usr/bin. On Windows, point to the directory that contains your ffmpeg.exe.

Parameters:

ffmpeg_path : str, optional, default: system’s default path

Path to ffmpeg library.

libav_path : str, optional, default: system’s default path

Path to libav library.