Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,77 @@
|
|
1 |
import streamlit as st
|
2 |
-
import os
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
-
|
7 |
-
|
8 |
-
# Application title
|
9 |
-
st.title("Image to Video Converter")
|
10 |
-
st.write("Upload your images, and we'll convert them into a simple video!")
|
11 |
-
|
12 |
-
# Directory to save uploaded images
|
13 |
-
upload_dir = "uploaded_images"
|
14 |
-
os.makedirs(upload_dir, exist_ok=True)
|
15 |
-
|
16 |
-
# Step 1: Upload multiple images
|
17 |
-
uploaded_files = st.file_uploader("Upload images (at least 2)", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
18 |
-
|
19 |
-
if uploaded_files:
|
20 |
-
# Display uploaded images
|
21 |
-
st.write("Preview of uploaded images:")
|
22 |
-
images = []
|
23 |
-
for file in uploaded_files:
|
24 |
-
image = Image.open(file)
|
25 |
-
images.append(np.array(image))
|
26 |
-
st.image(image, caption=file.name, width=150)
|
27 |
-
|
28 |
-
# Check if there are at least two images
|
29 |
-
if len(images) >= 2:
|
30 |
-
# Step 2: Choose video settings
|
31 |
-
fps = st.slider("Select frames per second (FPS)", min_value=1, max_value=60, value=10)
|
32 |
-
video_name = st.text_input("Enter the video file name", "output.mp4")
|
33 |
-
|
34 |
-
# Step 3: Generate video
|
35 |
-
if st.button("Generate Video"):
|
36 |
-
# Define video parameters
|
37 |
-
height, width, _ = images[0].shape
|
38 |
-
video_path = os.path.join(upload_dir, video_name)
|
39 |
-
video_writer = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
for file in os.listdir(upload_dir):
|
59 |
-
file_path = os.path.join(upload_dir, file)
|
60 |
-
os.remove
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import tempfile
|
6 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def create_simple_video(images, output_path, fps=1):
|
9 |
+
"""
|
10 |
+
Create a simple video from a list of images
|
11 |
+
|
12 |
+
Args:
|
13 |
+
images (list): List of PIL Image objects
|
14 |
+
output_path (str): Path to save the output video
|
15 |
+
fps (int): Frames per second
|
16 |
+
"""
|
17 |
+
# Convert PIL images to OpenCV format
|
18 |
+
cv_images = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in images]
|
19 |
+
|
20 |
+
# Get dimensions from first image
|
21 |
+
height, width, _ = cv_images[0].shape
|
22 |
+
|
23 |
+
# Define video writer
|
24 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
25 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
26 |
+
|
27 |
+
# Write frames
|
28 |
+
for img in cv_images:
|
29 |
+
out.write(img)
|
30 |
+
|
31 |
+
out.release()
|
32 |
|
33 |
+
def main():
|
34 |
+
st.title("🖼️ Image to Simple Video Converter")
|
35 |
+
|
36 |
+
# Sidebar for configuration
|
37 |
+
st.sidebar.header("Video Settings")
|
38 |
+
fps = st.sidebar.slider("Frames per Second", min_value=1, max_value=5, value=1)
|
39 |
+
|
40 |
+
# File uploader
|
41 |
+
uploaded_files = st.file_uploader(
|
42 |
+
"Upload Images",
|
43 |
+
type=['png', 'jpg', 'jpeg', 'webp'],
|
44 |
+
accept_multiple_files=True
|
45 |
+
)
|
46 |
+
|
47 |
+
if uploaded_files:
|
48 |
+
# Sort images to maintain order
|
49 |
+
uploaded_files.sort(key=lambda x: x.name)
|
50 |
+
|
51 |
+
# Convert uploaded files to PIL Images
|
52 |
+
images = [Image.open(file) for file in uploaded_files]
|
53 |
+
|
54 |
+
# Create temporary file for video
|
55 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
|
56 |
+
video_path = temp_video.name
|
57 |
+
|
58 |
+
# Create video
|
59 |
+
create_simple_video(images, video_path, fps)
|
60 |
+
|
61 |
+
# Display video
|
62 |
+
st.video(video_path)
|
63 |
+
|
64 |
+
# Provide download button
|
65 |
+
with open(video_path, 'rb') as video_file:
|
66 |
+
st.download_button(
|
67 |
+
label="Download Video",
|
68 |
+
data=video_file.read(),
|
69 |
+
file_name="converted_video.mp4",
|
70 |
+
mime="video/mp4"
|
71 |
+
)
|
72 |
+
|
73 |
+
# Clean up temporary file
|
74 |
+
os.unlink(video_path)
|
75 |
|
76 |
+
if __name__ == "__main__":
|
77 |
+
main()
|
|
|
|
|
|