|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
|
|
def process_video(video_path, frame_difference, brightness_value): |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
if not cap.isOpened(): |
|
return "Error: Unable to open video." |
|
|
|
|
|
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
fps = int(cap.get(cv2.CAP_PROP_FPS)) |
|
|
|
|
|
output_path = "output_video.mp4" |
|
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height)) |
|
|
|
|
|
frames = [] |
|
|
|
while True: |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
inverted_frame = cv2.bitwise_not(frame) |
|
frames.append((frame, inverted_frame)) |
|
|
|
cap.release() |
|
|
|
|
|
num_frames = len(frames) - frame_difference |
|
num_frames = max(num_frames, 0) |
|
|
|
|
|
for i in range(num_frames): |
|
|
|
orig_frame = frames[i][0] |
|
shifted_index = (i + frame_difference) % len(frames) |
|
shifted_frame = frames[shifted_index][1] |
|
|
|
|
|
overlay_frame = cv2.addWeighted(orig_frame, 0.5, shifted_frame, 0.5, 0) |
|
|
|
|
|
overlay_frame = np.clip(overlay_frame * brightness_value, 0, 255).astype(np.uint8) |
|
|
|
|
|
out.write(overlay_frame) |
|
|
|
out.release() |
|
|
|
return output_path |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_video, |
|
inputs=[ |
|
gr.Video(label="Input Video"), |
|
gr.Slider(0, 50, step=1, value=1, label="Frame Difference (Integer)"), |
|
gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="Brightness Adjustment (0.0 to 2.0)"), |
|
], |
|
outputs=gr.Video(label="Output Video"), |
|
title="Motion Amplification with Frame Difference" |
|
) |
|
|
|
iface.launch() |