File size: 2,334 Bytes
dc10d60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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):
    # Load the video using OpenCV
    cap = cv2.VideoCapture(video_path)
    
    # Get video properties
    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))
    
    # Create a VideoWriter object for the output
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # Codec for MP4
    out_path = "output_video.mp4"
    out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
    
    frames = []

    # Read all frames into memory
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        frames.append(frame)
    
    # Process frames with frame difference
    V3_frames = []
    for f in range(frame_diff_value, total_frames - frame_diff_value):
        # Original frame and its previous frame
        current_frame = frames[f]
        previous_frame = frames[f - frame_diff_value]
        
        # Invert the previous frame
        previous_frame_inverted = 255 - previous_frame
        
        # Blend current frame with inverted previous frame (50/50 opacity)
        blended_frame = cv2.addWeighted(current_frame, 0.5, previous_frame_inverted, 0.5, 0)
        
        # Append blended frame to new video frames list
        V3_frames.append(blended_frame)

    # Adjust brightness using moviepy for the entire video
    V3_clip = VideoFileClip(out_path)
    V3_clip = lum_contrast(V3_clip, contrast=1, luminosity=brightness_value)  # Adjust brightness
    
    # Save the modified video
    V3_clip.write_videofile("output_with_brightness.mp4", codec='libx264')

    # Release the video writer and capture
    cap.release()
    out.release()

    return "output_with_brightness.mp4"

# Gradio interface
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()