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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -41
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
- 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()
 
 
 
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()