File size: 955 Bytes
eeae5c5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Video processor function used in the original VIdeoPrism repo.
import mediapy
import numpy as np
def read_and_preprocess_video(
filename: str, target_num_frames: int, target_frame_size: tuple[int, int]
):
"""Reads and preprocesses a video."""
frames = mediapy.read_video(filename)
# Sample to target number of frames.
frame_indices = np.linspace(0, len(frames), num=target_num_frames, endpoint=False, dtype=np.int32)
frames = np.array([frames[i] for i in frame_indices])
# Resize to target size.
original_height, original_width = frames.shape[-3:-1]
target_height, target_width = target_frame_size
assert original_height * target_width == original_width * target_height, (
"Currently does not support aspect ratio mismatch."
)
frames = mediapy.resize_video(frames, shape=target_frame_size)
# Normalize pixel values to [0.0, 1.0].
frames = mediapy.to_float01(frames)
return frames |