Upload preview_test.py
Browse files- preview_test.py +50 -0
preview_test.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
|
4 |
+
def extract_frames(video_path):
|
5 |
+
# Open the video file
|
6 |
+
cap = cv2.VideoCapture(video_path)
|
7 |
+
if not cap.isOpened():
|
8 |
+
# If video cannot be opened, return four None values
|
9 |
+
return [None] * 4
|
10 |
+
|
11 |
+
# Get the total number of frames
|
12 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
13 |
+
if total_frames == 0:
|
14 |
+
# If video has no frames, release and return None values
|
15 |
+
cap.release()
|
16 |
+
return [None] * 4
|
17 |
+
|
18 |
+
# Calculate frame indices at 0%, 25%, 50%, and 75%
|
19 |
+
indices = [int(total_frames * i / 4) for i in range(4)]
|
20 |
+
frames = []
|
21 |
+
|
22 |
+
# Extract frames at the calculated indices
|
23 |
+
for idx in indices:
|
24 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
|
25 |
+
ret, frame = cap.read()
|
26 |
+
if ret:
|
27 |
+
# Convert from BGR (OpenCV format) to RGB (Gradio format)
|
28 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
29 |
+
frames.append(frame)
|
30 |
+
else:
|
31 |
+
# If frame extraction fails, append None
|
32 |
+
frames.append(None)
|
33 |
+
|
34 |
+
# Release the video capture object
|
35 |
+
cap.release()
|
36 |
+
|
37 |
+
# Return the four frames as a tuple for Gradio outputs
|
38 |
+
return tuple(frames)
|
39 |
+
|
40 |
+
# Create the Gradio interface
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=extract_frames,
|
43 |
+
inputs=gr.Video(label="Upload a video"),
|
44 |
+
outputs=[gr.Image(label=f"Frame at {i*25}%") for i in range(4)],
|
45 |
+
title="Video Frame Extractor",
|
46 |
+
description="Upload a video to extract four frames from different parts of the video."
|
47 |
+
)
|
48 |
+
|
49 |
+
# Launch the web app
|
50 |
+
iface.launch()
|