File size: 2,393 Bytes
a62b6de
a1bec20
dc10d60
ded77bf
a62b6de
 
 
 
 
dc10d60
 
a62b6de
 
 
dc10d60
a62b6de
 
 
ded77bf
a62b6de
dc10d60
a62b6de
dc10d60
 
 
 
a62b6de
 
 
dc10d60
 
 
b9f4838
 
 
 
a62b6de
 
 
 
b9f4838
a62b6de
dc10d60
a62b6de
 
 
 
 
 
 
 
 
 
ded77bf
a62b6de
dc10d60
a62b6de
 
 
ded77bf
a62b6de
b9f4838
 
ded77bf
a62b6de
 
ded77bf
dc10d60
a62b6de
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

def process_video(video_path, frame_difference, brightness_value):
    # Read the video
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        return "Error: Unable to open video."

    # Get video properties
    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    
    # Create a VideoWriter object to write the output video
    output_path = "output_video.mp4"
    out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))

    # Initialize a list to store frames for frame shifting
    frames = []
    
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        # Invert the pixel values of the frame
        inverted_frame = cv2.bitwise_not(frame)
        frames.append((frame, inverted_frame))

    cap.release()

    # Calculate number of frames to process (Original length minus frame difference)
    num_frames = len(frames) - frame_difference
    num_frames = max(num_frames, 0)  # Ensure non-negative

    # Process video by applying frame difference and overlaying
    for i in range(num_frames):
        # Get the original and shifted frames
        orig_frame = frames[i][0]
        shifted_index = (i + frame_difference) % len(frames)
        shifted_frame = frames[shifted_index][1]  # Use the inverted frame of shifted frame

        # Overlay the frames with 50% opacity
        overlay_frame = cv2.addWeighted(orig_frame, 0.5, shifted_frame, 0.5, 0)

        # Increase the brightness
        overlay_frame = np.clip(overlay_frame * brightness_value, 0, 255).astype(np.uint8)

        # Write the resulting frame to the output video
        out.write(overlay_frame)

    out.release()
    
    return output_path

# Gradio Interface
iface = gr.Interface(
    fn=process_video,
    inputs=[
        gr.Video(label="Input Video"),  # Video input
        gr.Slider(0, 50, step=1, value=1, label="Frame Difference (Integer)"),  # Integer Frame difference input
        gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="Brightness Adjustment (0.0 to 2.0)"),  # Brightness adjustment input
    ],
    outputs=gr.Video(label="Output Video"),  # Video output
    title="Motion Amplification with Frame Difference"
)

iface.launch()