NihalGazi's picture
Update app.py
b9f4838 verified
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()