|
import gradio as gr |
|
import cv2 |
|
import tempfile |
|
import os |
|
|
|
def process_video(video_path): |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
if not cap.isOpened(): |
|
return None |
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") |
|
output_path = temp_file.name |
|
|
|
|
|
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)) |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), isColor=False) |
|
|
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
out.write(gray_frame) |
|
|
|
|
|
cap.release() |
|
out.release() |
|
|
|
return output_path |
|
|
|
def preview_video(video): |
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") |
|
temp_path = temp_file.name |
|
with open(temp_path, "wb") as f: |
|
f.write(video) |
|
|
|
|
|
processed_video_path = process_video(temp_path) |
|
|
|
|
|
os.unlink(temp_path) |
|
|
|
return processed_video_path |
|
|
|
|
|
iface = gr.Interface( |
|
fn=preview_video, |
|
inputs=gr.Video(label="Upload Video"), |
|
outputs=gr.Video(label="Preview Video"), |
|
title="Video Preview with OpenCV", |
|
description="Upload a video, and it will be processed (e.g., grayscale) and displayed." |
|
) |
|
|
|
|
|
iface.launch() |