NihalGazi commited on
Commit
dc10d60
·
verified ·
1 Parent(s): a7f78fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from moviepy.editor import VideoFileClip
5
+ from moviepy.video.fx.all import lum_contrast
6
+
7
+ def process_video(video_path, frame_diff_value, brightness_value):
8
+ # Load the video using OpenCV
9
+ cap = cv2.VideoCapture(video_path)
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 into memory
25
+ while True:
26
+ ret, frame = cap.read()
27
+ if not ret:
28
+ break
29
+ frames.append(frame)
30
+
31
+ # Process frames with frame difference
32
+ V3_frames = []
33
+ for f in range(frame_diff_value, total_frames - frame_diff_value):
34
+ # Original frame and its previous frame
35
+ current_frame = frames[f]
36
+ previous_frame = frames[f - frame_diff_value]
37
+
38
+ # Invert the previous frame
39
+ previous_frame_inverted = 255 - previous_frame
40
+
41
+ # Blend current frame with inverted previous frame (50/50 opacity)
42
+ blended_frame = cv2.addWeighted(current_frame, 0.5, previous_frame_inverted, 0.5, 0)
43
+
44
+ # Append blended frame to new video frames list
45
+ V3_frames.append(blended_frame)
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 "output_with_brightness.mp4"
59
+
60
+ # Gradio interface
61
+ inputs = [
62
+ gr.Video(label="Upload Video"),
63
+ gr.Slider(0, 50, label="Frame Difference", value=10),
64
+ gr.Slider(0, 100, label="Brightness", value=50)
65
+ ]
66
+
67
+ outputs = gr.Video(label="Processed Video")
68
+
69
+ gr.Interface(fn=process_video, inputs=inputs, outputs=outputs, title="Motion Amplification Using Frame Difference").launch()