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() # Process video by applying frame difference and overlaying num_frames = len(frames) for i in range(num_frames): # Get the original and shifted frames orig_frame = frames[i][0] shifted_index = (i + frame_difference) % num_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, value=1, label="Frame Difference"), # Frame difference input gr.Slider(0.0, 1.0, value=1.0, label="Brightness Adjustment"), # Brightness adjustment input ], outputs=gr.Video(label="Output Video"), # Video output title="Motion Amplification with Frame Difference" ) iface.launch()