|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
from moviepy.editor import VideoFileClip |
|
from moviepy.video.fx.all import lum_contrast |
|
|
|
def process_video(video_path, frame_diff_value, brightness_value): |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
|
|
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
fps = int(cap.get(cv2.CAP_PROP_FPS)) |
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
out_path = "output_video.mp4" |
|
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height)) |
|
|
|
frames = [] |
|
|
|
|
|
while True: |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
frames.append(frame) |
|
|
|
|
|
V3_frames = [] |
|
for f in range(frame_diff_value, total_frames - frame_diff_value): |
|
|
|
current_frame = frames[f] |
|
previous_frame = frames[f - frame_diff_value] |
|
|
|
|
|
previous_frame_inverted = 255 - previous_frame |
|
|
|
|
|
blended_frame = cv2.addWeighted(current_frame, 0.5, previous_frame_inverted, 0.5, 0) |
|
|
|
|
|
V3_frames.append(blended_frame) |
|
|
|
|
|
V3_clip = VideoFileClip(out_path) |
|
V3_clip = lum_contrast(V3_clip, contrast=1, luminosity=brightness_value) |
|
|
|
|
|
V3_clip.write_videofile("output_with_brightness.mp4", codec='libx264') |
|
|
|
|
|
cap.release() |
|
out.release() |
|
|
|
return "output_with_brightness.mp4" |
|
|
|
|
|
inputs = [ |
|
gr.Video(label="Upload Video"), |
|
gr.Slider(0, 50, label="Frame Difference", value=10), |
|
gr.Slider(0, 100, label="Brightness", value=50) |
|
] |
|
|
|
outputs = gr.Video(label="Processed Video") |
|
|
|
gr.Interface(fn=process_video, inputs=inputs, outputs=outputs, title="Motion Amplification Using Frame Difference").launch() |