Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,83 @@
|
|
1 |
-
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
4 |
from moviepy.editor import VideoFileClip
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Get video properties
|
|
|
|
|
12 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
13 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
14 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
15 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
16 |
-
|
17 |
-
# Create a VideoWriter object for the output
|
18 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
|
19 |
-
out_path = "output_video.mp4"
|
20 |
-
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
21 |
|
|
|
|
|
|
|
|
|
|
|
22 |
frames = []
|
23 |
|
24 |
-
# Read all frames
|
25 |
while True:
|
26 |
ret, frame = cap.read()
|
27 |
if not ret:
|
28 |
break
|
29 |
frames.append(frame)
|
30 |
-
|
31 |
-
# Process frames
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
current_frame = frames[f]
|
36 |
-
previous_frame = frames[f - frame_diff_value]
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
|
47 |
-
# Adjust brightness using moviepy for the entire video
|
48 |
-
V3_clip = VideoFileClip(out_path)
|
49 |
-
V3_clip = lum_contrast(V3_clip, contrast=1, luminosity=brightness_value) # Adjust brightness
|
50 |
-
|
51 |
-
# Save the modified video
|
52 |
-
V3_clip.write_videofile("output_with_brightness.mp4", codec='libx264')
|
53 |
-
|
54 |
-
# Release the video writer and capture
|
55 |
cap.release()
|
56 |
out.release()
|
57 |
|
58 |
-
return
|
59 |
|
60 |
# Gradio interface
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
from moviepy.editor import VideoFileClip
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# Helper function to adjust brightness
|
8 |
+
def adjust_brightness(frame, brightness_value):
|
9 |
+
return cv2.convertScaleAbs(frame, alpha=1, beta=brightness_value)
|
10 |
|
11 |
+
# Helper function to overlay frames with opacity
|
12 |
+
def overlay_frames(frame1, frame2, opacity=0.5):
|
13 |
+
return cv2.addWeighted(frame1, opacity, frame2, 1 - opacity, 0)
|
14 |
+
|
15 |
+
# Main function to process video
|
16 |
+
def process_video(video, frame_diff, brightness_value):
|
17 |
+
# Read the video file
|
18 |
+
input_path = video.name
|
19 |
+
cap = cv2.VideoCapture(input_path)
|
20 |
|
21 |
# Get video properties
|
22 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
23 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
24 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
25 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Create a temporary output file
|
28 |
+
output_path = tempfile.mktemp(suffix=".mp4")
|
29 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
30 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
31 |
+
|
32 |
frames = []
|
33 |
|
34 |
+
# Read all frames
|
35 |
while True:
|
36 |
ret, frame = cap.read()
|
37 |
if not ret:
|
38 |
break
|
39 |
frames.append(frame)
|
40 |
+
|
41 |
+
# Process the frames
|
42 |
+
for f in range(frame_diff, frame_count - frame_diff):
|
43 |
+
# Invert pixels for frame f - frame_diff
|
44 |
+
prev_frame = 255 - frames[f - frame_diff]
|
45 |
current_frame = frames[f]
|
|
|
46 |
|
47 |
+
# Overlay frames with 50/50 opacity
|
48 |
+
overlayed_frame = overlay_frames(prev_frame, current_frame, opacity=0.5)
|
49 |
|
50 |
+
# Adjust brightness
|
51 |
+
bright_frame = adjust_brightness(overlayed_frame, brightness_value)
|
52 |
|
53 |
+
# Write the processed frame
|
54 |
+
out.write(bright_frame)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
cap.release()
|
57 |
out.release()
|
58 |
|
59 |
+
return output_path
|
60 |
|
61 |
# Gradio interface
|
62 |
+
def video_motion_amplify(video, frame_diff_value, brightness_value):
|
63 |
+
# Process the video
|
64 |
+
output_video_path = process_video(video, frame_diff_value, brightness_value)
|
65 |
+
|
66 |
+
# Return the output video path
|
67 |
+
return output_video_path
|
68 |
|
69 |
+
# Gradio Interface
|
70 |
+
interface = gr.Interface(
|
71 |
+
fn=video_motion_amplify,
|
72 |
+
inputs=[
|
73 |
+
gr.inputs.Video(type="file"),
|
74 |
+
gr.inputs.Slider(minimum=0, maximum=50, step=1, label="Frame Difference Value"),
|
75 |
+
gr.inputs.Slider(minimum=-100, maximum=100, step=1, label="Brightness Value"),
|
76 |
+
],
|
77 |
+
outputs=gr.outputs.Video(type="file"),
|
78 |
+
title="Motion Amplification using Frame Difference",
|
79 |
+
description="This app amplifies motion in a video by overlaying frames based on frame difference and adjusting brightness."
|
80 |
+
)
|
81 |
|
82 |
+
# Launch the interface
|
83 |
+
interface.launch()
|