Enhance a video by increasing its frame rate using frame interpolation. Parameters: - mp4 (Path): Path to the video file. - keep_original_duration (bool): Indicator to maintain the original video duration after frame interpolation. - custom_fps (float): Targ
(
self,
mp4: Path = Input(description="Upload an mp4 video file."),
framerate_multiplier: int = Input(
description="Determines how many intermediate frames to generate between original frames. E.g., a value of 2 will double the frame rate, and 4 will quadruple it, etc.",
default=2,
choices=[2, 4, 8, 16, 32],
),
keep_original_duration: bool = Input(
description="Should the enhanced video retain the original duration? If set to `True`, the model will adjust the frame rate to maintain the video's original duration after adding interpolated frames. If set to `False`, the frame rate will be set based on `custom_fps`.",
default=True,
),
custom_fps: float = Input(
description="Set `keep_original_duration` to `False` to use this! Desired frame rate (fps) for the enhanced video. This will only be considered if `keep_original_duration` is set to `False`.",
default=None,
ge=1,
le=240,
),
)
| 78 | os.makedirs(out_dir) |
| 79 | |
| 80 | def predict( |
| 81 | self, |
| 82 | mp4: Path = Input(description="Upload an mp4 video file."), |
| 83 | framerate_multiplier: int = Input( |
| 84 | description="Determines how many intermediate frames to generate between original frames. E.g., a value of 2 will double the frame rate, and 4 will quadruple it, etc.", |
| 85 | default=2, |
| 86 | choices=[2, 4, 8, 16, 32], |
| 87 | ), |
| 88 | keep_original_duration: bool = Input( |
| 89 | description="Should the enhanced video retain the original duration? If set to `True`, the model will adjust the frame rate to maintain the video's original duration after adding interpolated frames. If set to `False`, the frame rate will be set based on `custom_fps`.", |
| 90 | default=True, |
| 91 | ), |
| 92 | custom_fps: float = Input( |
| 93 | description="Set `keep_original_duration` to `False` to use this! Desired frame rate (fps) for the enhanced video. This will only be considered if `keep_original_duration` is set to `False`.", |
| 94 | default=None, |
| 95 | ge=1, |
| 96 | le=240, |
| 97 | ), |
| 98 | ) -> Iterator[Path]: |
| 99 | """ |
| 100 | Enhance a video by increasing its frame rate using frame interpolation. |
| 101 | |
| 102 | Parameters: |
| 103 | - mp4 (Path): Path to the video file. |
| 104 | - keep_original_duration (bool): Indicator to maintain the original video duration after frame interpolation. |
| 105 | - custom_fps (float): Target frame rate for the enhanced video when not maintaining the original duration. |
| 106 | - framerate_multiplier (int): Multiplier for the number of frames. |
| 107 | |
| 108 | Returns: |
| 109 | Iterator[Path]: Paths to the generated enhanced video files. |
| 110 | """ |
| 111 | |
| 112 | num_iterations = int(math.log2(framerate_multiplier)) |
| 113 | original_seq_name = os.path.basename(mp4).split(".")[0] |
| 114 | |
| 115 | for enhancing_iteration in tqdm(range(num_iterations), desc="Enhancing iterations"): |
| 116 | # Opening the video and extracting essential properties |
| 117 | video = cv2.VideoCapture(str(mp4)) |
| 118 | original_video_fps = video.get(cv2.CAP_PROP_FPS) |
| 119 | width, height = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 120 | original_num_frames = sum(video.read()[0] for _ in range(int(video.get(cv2.CAP_PROP_FRAME_COUNT)))) |
| 121 | |
| 122 | # Informing the user of video details before processing |
| 123 | print(f"Video Name: {original_seq_name}") |
| 124 | print(f"Original Frame Rate (FPS): {original_video_fps}") |
| 125 | print(f"Original Total Number of Frames: {original_num_frames}") |
| 126 | |
| 127 | img_array = [] |
| 128 | # Processing each set of 4 frames for frame rate enhancement |
| 129 | for t in tqdm(range(0, original_num_frames - 3), desc="Processing frames"): |
| 130 | video.set(cv2.CAP_PROP_POS_FRAMES, t) |
| 131 | _, rawFrame0 = video.read() |
| 132 | _, rawFrame1 = video.read() |
| 133 | _, rawFrame2 = video.read() |
| 134 | _, rawFrame3 = video.read() |
| 135 | |
| 136 | # If any frame in the set of 4 is missing, stop processing |
| 137 | if any(frame is None for frame in [rawFrame0, rawFrame1, rawFrame2, rawFrame3]): |
nothing calls this directly
no test coverage detected