NihalGazi's picture
Create app.py
dc10d60 verified
raw
history blame
2.33 kB
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()