Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
def video_to_images(video_file, fps=1):
|
| 8 |
video_path = video_file.name
|
| 9 |
output_folder = "frames"
|
| 10 |
os.makedirs(output_folder, exist_ok=True)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
img = Image.fromarray(frame)
|
| 19 |
-
img.save(frame_filename)
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=video_to_images,
|
|
@@ -27,4 +35,4 @@ iface = gr.Interface(
|
|
| 27 |
title="Video to Images Converter"
|
| 28 |
)
|
| 29 |
|
| 30 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def video_to_images(video_file, fps=1):
|
| 6 |
video_path = video_file.name
|
| 7 |
output_folder = "frames"
|
| 8 |
os.makedirs(output_folder, exist_ok=True)
|
| 9 |
|
| 10 |
+
cap = cv2.VideoCapture(video_path)
|
| 11 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 12 |
+
video_fps = cap.get(cv2.CAP_PROP_FPS)
|
| 13 |
|
| 14 |
+
step = int(video_fps / fps) if fps > 0 else 1
|
| 15 |
+
frame_list = []
|
| 16 |
+
count = 0
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
while True:
|
| 19 |
+
ret, frame = cap.read()
|
| 20 |
+
if not ret:
|
| 21 |
+
break
|
| 22 |
+
if count % step == 0:
|
| 23 |
+
frame_filename = os.path.join(output_folder, f"frame_{count:04d}.png")
|
| 24 |
+
cv2.imwrite(frame_filename, frame)
|
| 25 |
+
frame_list.append(frame_filename)
|
| 26 |
+
count += 1
|
| 27 |
+
|
| 28 |
+
cap.release()
|
| 29 |
+
return frame_list
|
| 30 |
|
| 31 |
iface = gr.Interface(
|
| 32 |
fn=video_to_images,
|
|
|
|
| 35 |
title="Video to Images Converter"
|
| 36 |
)
|
| 37 |
|
| 38 |
+
iface.launch()
|