Spaces:
Running
Running
seawolf2357
commited on
Commit
•
c892817
1
Parent(s):
612cb39
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,54 @@
|
|
1 |
import cv2
|
|
|
2 |
import os
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
+
import gradio as gr
|
3 |
import os
|
4 |
+
import tempfile
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
def images_to_video(input_files, fps=30):
|
8 |
+
# Create a temporary directory to store images
|
9 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
10 |
+
# Save uploaded files to temp directory
|
11 |
+
for i, file_info in enumerate(input_files):
|
12 |
+
file_path = os.path.join(temp_dir, f"image_{i}{Path(file_info.name).suffix}")
|
13 |
+
with open(file_path, 'wb') as file:
|
14 |
+
file.write(file_info.read())
|
15 |
+
# Get all the image files from temp directory
|
16 |
+
images = [img for img in os.listdir(temp_dir) if img.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
17 |
+
if not images:
|
18 |
+
raise ValueError("No images found to create a video.")
|
19 |
+
|
20 |
+
images.sort() # Sort the images by name
|
21 |
+
|
22 |
+
# Read the first image to set the video size
|
23 |
+
frame = cv2.imread(os.path.join(temp_dir, images[0]))
|
24 |
+
height, width, layers = frame.shape
|
25 |
+
size = (width, height)
|
26 |
+
|
27 |
+
# Create a unique filename for the output video
|
28 |
+
video_filename = os.path.join('/mnt/data', 'output_video.mp4')
|
29 |
+
|
30 |
+
# Initialize the video writer
|
31 |
+
out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
|
32 |
+
|
33 |
+
# Loop through all images and add to the video
|
34 |
+
for image in images:
|
35 |
+
frame = cv2.imread(os.path.join(temp_dir, image))
|
36 |
+
out.write(frame)
|
37 |
+
|
38 |
+
# Release the video writer
|
39 |
+
out.release()
|
40 |
+
|
41 |
+
# Return the path of the created video file
|
42 |
+
return video_filename
|
43 |
+
|
44 |
+
# Create the Gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=images_to_video,
|
47 |
+
inputs=[gr.inputs.Image(type="file", label="Upload Images", multiple=True)],
|
48 |
+
outputs=gr.outputs.Video(label="Output Video"),
|
49 |
+
title="Images to Video Converter",
|
50 |
+
description="Upload multiple images to create a video."
|
51 |
+
)
|
52 |
+
|
53 |
+
# Launch the interface
|
54 |
+
iface.launch()
|