Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def process_video(video_path):
|
| 7 |
+
# Open the video file
|
| 8 |
+
cap = cv2.VideoCapture(video_path)
|
| 9 |
+
if not cap.isOpened():
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
# Create a temporary file to save the processed video
|
| 13 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 14 |
+
output_path = temp_file.name
|
| 15 |
+
|
| 16 |
+
# Get video properties
|
| 17 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 18 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 19 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 20 |
+
|
| 21 |
+
# Define the codec and create a VideoWriter object
|
| 22 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 23 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), isColor=False)
|
| 24 |
+
|
| 25 |
+
# Process each frame
|
| 26 |
+
while cap.isOpened():
|
| 27 |
+
ret, frame = cap.read()
|
| 28 |
+
if not ret:
|
| 29 |
+
break
|
| 30 |
+
|
| 31 |
+
# Convert the frame to grayscale (example processing)
|
| 32 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 33 |
+
|
| 34 |
+
# Write the processed frame to the output video
|
| 35 |
+
out.write(gray_frame)
|
| 36 |
+
|
| 37 |
+
# Release everything
|
| 38 |
+
cap.release()
|
| 39 |
+
out.release()
|
| 40 |
+
|
| 41 |
+
return output_path
|
| 42 |
+
|
| 43 |
+
def preview_video(video):
|
| 44 |
+
# Save the uploaded video to a temporary file
|
| 45 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 46 |
+
temp_path = temp_file.name
|
| 47 |
+
with open(temp_path, "wb") as f:
|
| 48 |
+
f.write(video)
|
| 49 |
+
|
| 50 |
+
# Process the video using OpenCV
|
| 51 |
+
processed_video_path = process_video(temp_path)
|
| 52 |
+
|
| 53 |
+
# Clean up the temporary file
|
| 54 |
+
os.unlink(temp_path)
|
| 55 |
+
|
| 56 |
+
return processed_video_path
|
| 57 |
+
|
| 58 |
+
# Define the Gradio interface
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=preview_video, # Function to call
|
| 61 |
+
inputs=gr.Video(label="Upload Video"), # Input component: Video upload
|
| 62 |
+
outputs=gr.Video(label="Preview Video"), # Output component: Video preview
|
| 63 |
+
title="Video Preview with OpenCV",
|
| 64 |
+
description="Upload a video, and it will be processed (e.g., grayscale) and displayed."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Launch the interface
|
| 68 |
+
iface.launch()
|