NihalGazi's picture
Update app.py
ded77bf verified
raw
history blame
2.65 kB
import cv2
import numpy as np
import gradio as gr
from moviepy.editor import VideoFileClip
import tempfile
# Helper function to adjust brightness
def adjust_brightness(frame, brightness_value):
return cv2.convertScaleAbs(frame, alpha=1, beta=brightness_value)
# Helper function to overlay frames with opacity
def overlay_frames(frame1, frame2, opacity=0.5):
return cv2.addWeighted(frame1, opacity, frame2, 1 - opacity, 0)
# Main function to process video
def process_video(video, frame_diff, brightness_value):
# Read the video file
input_path = video.name
cap = cv2.VideoCapture(input_path)
# Get video properties
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create a temporary output file
output_path = tempfile.mktemp(suffix=".mp4")
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
frames = []
# Read all frames
while True:
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
# Process the frames
for f in range(frame_diff, frame_count - frame_diff):
# Invert pixels for frame f - frame_diff
prev_frame = 255 - frames[f - frame_diff]
current_frame = frames[f]
# Overlay frames with 50/50 opacity
overlayed_frame = overlay_frames(prev_frame, current_frame, opacity=0.5)
# Adjust brightness
bright_frame = adjust_brightness(overlayed_frame, brightness_value)
# Write the processed frame
out.write(bright_frame)
cap.release()
out.release()
return output_path
# Gradio interface
def video_motion_amplify(video, frame_diff_value, brightness_value):
# Process the video
output_video_path = process_video(video, frame_diff_value, brightness_value)
# Return the output video path
return output_video_path
# Gradio Interface
interface = gr.Interface(
fn=video_motion_amplify,
inputs=[
gr.inputs.Video(type="file"),
gr.inputs.Slider(minimum=0, maximum=50, step=1, label="Frame Difference Value"),
gr.inputs.Slider(minimum=-100, maximum=100, step=1, label="Brightness Value"),
],
outputs=gr.outputs.Video(type="file"),
title="Motion Amplification using Frame Difference",
description="This app amplifies motion in a video by overlaying frames based on frame difference and adjusting brightness."
)
# Launch the interface
interface.launch()