Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,66 @@
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
-
import gradio as gr
|
4 |
-
from moviepy.editor import VideoFileClip
|
5 |
-
import tempfile
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
26 |
|
27 |
-
# Create a
|
28 |
-
output_path =
|
29 |
-
|
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 |
-
|
40 |
-
|
41 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
return output_video_path
|
68 |
|
69 |
-
# Gradio Interface
|
70 |
-
|
71 |
-
fn=
|
72 |
inputs=[
|
73 |
-
gr.Video(label="Input Video"),
|
74 |
-
gr.Slider(
|
75 |
-
gr.Slider(
|
76 |
],
|
77 |
-
outputs=gr.Video(label="Output Video"),
|
78 |
-
title="Motion Amplification
|
79 |
-
description="This app amplifies motion in a video by overlaying frames based on frame difference and adjusting brightness."
|
80 |
)
|
81 |
|
82 |
-
|
83 |
-
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
|
|
|
|
4 |
|
5 |
+
def process_video(video_path, frame_difference, brightness_value):
|
6 |
+
# Read the video
|
7 |
+
cap = cv2.VideoCapture(video_path)
|
8 |
+
if not cap.isOpened():
|
9 |
+
return "Error: Unable to open video."
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Get video properties
|
12 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
13 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
14 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
|
|
15 |
|
16 |
+
# Create a VideoWriter object to write the output video
|
17 |
+
output_path = "output_video.mp4"
|
18 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
|
|
19 |
|
20 |
+
# Initialize a list to store frames for frame shifting
|
21 |
frames = []
|
22 |
+
|
|
|
23 |
while True:
|
24 |
ret, frame = cap.read()
|
25 |
if not ret:
|
26 |
break
|
27 |
+
# Invert the pixel values of the frame
|
28 |
+
inverted_frame = cv2.bitwise_not(frame)
|
29 |
+
frames.append((frame, inverted_frame))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
cap.release()
|
|
|
32 |
|
33 |
+
# Process video by applying frame difference and overlaying
|
34 |
+
num_frames = len(frames)
|
35 |
+
for i in range(num_frames):
|
36 |
+
# Get the original and shifted frames
|
37 |
+
orig_frame = frames[i][0]
|
38 |
+
shifted_index = (i + frame_difference) % num_frames
|
39 |
+
shifted_frame = frames[shifted_index][1] # Use the inverted frame of shifted frame
|
40 |
|
41 |
+
# Overlay the frames with 50% opacity
|
42 |
+
overlay_frame = cv2.addWeighted(orig_frame, 0.5, shifted_frame, 0.5, 0)
|
43 |
+
|
44 |
+
# Increase the brightness
|
45 |
+
overlay_frame = np.clip(overlay_frame * brightness_value, 0, 255).astype(np.uint8)
|
46 |
+
|
47 |
+
# Write the resulting frame to the output video
|
48 |
+
out.write(overlay_frame)
|
49 |
+
|
50 |
+
out.release()
|
51 |
|
52 |
+
return output_path
|
|
|
53 |
|
54 |
+
# Gradio Interface
|
55 |
+
iface = gr.Interface(
|
56 |
+
fn=process_video,
|
57 |
inputs=[
|
58 |
+
gr.Video(label="Input Video"), # Video input
|
59 |
+
gr.Slider(0, 50, value=1, label="Frame Difference"), # Frame difference input
|
60 |
+
gr.Slider(0.0, 1.0, value=1.0, label="Brightness Adjustment"), # Brightness adjustment input
|
61 |
],
|
62 |
+
outputs=gr.Video(label="Output Video"), # Video output
|
63 |
+
title="Motion Amplification with Frame Difference"
|
|
|
64 |
)
|
65 |
|
66 |
+
iface.launch()
|
|