Update app.py
Browse files
app.py
CHANGED
@@ -3,18 +3,33 @@ import torch
|
|
3 |
from transformers import pipeline, AutoProcessor, AutoModel
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
-
|
7 |
import tempfile
|
8 |
import os
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
|
11 |
"""
|
12 |
Generate a video from an image using LTX-Video and image captioning.
|
13 |
-
|
14 |
-
Args:
|
15 |
-
image: PIL Image object
|
16 |
-
duration_seconds: Duration of output video in seconds
|
17 |
-
progress_bar: Streamlit progress bar object
|
18 |
"""
|
19 |
try:
|
20 |
if progress_bar:
|
@@ -57,16 +72,15 @@ def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
|
|
57 |
if progress_bar:
|
58 |
progress_bar.progress(0.8, "Creating final video...")
|
59 |
|
60 |
-
# Convert frames to
|
61 |
frames = [np.array(frame) for frame in video_frames]
|
62 |
|
63 |
# Create temporary file for video
|
64 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
65 |
output_path = tmp_file.name
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
clip.write_videofile(output_path, codec='libx264', audio=False)
|
70 |
|
71 |
if progress_bar:
|
72 |
progress_bar.progress(1.0, "Video generation complete!")
|
@@ -75,6 +89,7 @@ def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
|
|
75 |
|
76 |
except Exception as e:
|
77 |
st.error(f"Error generating video: {str(e)}")
|
|
|
78 |
return None, None
|
79 |
|
80 |
def main():
|
@@ -99,33 +114,37 @@ def main():
|
|
99 |
|
100 |
# Generate button
|
101 |
if st.button("Generate Video"):
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
# Generate video
|
107 |
-
video_path, caption = generate_video_from_image(image, duration, my_bar)
|
108 |
-
|
109 |
-
if video_path and os.path.exists(video_path):
|
110 |
-
# Read the video file
|
111 |
-
with open(video_path, 'rb') as video_file:
|
112 |
-
video_bytes = video_file.read()
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
-
label="Download Video",
|
117 |
-
data=video_bytes,
|
118 |
-
file_name="generated_video.mp4",
|
119 |
-
mime="video/mp4"
|
120 |
-
)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
if __name__ == "__main__":
|
131 |
main()
|
|
|
3 |
from transformers import pipeline, AutoProcessor, AutoModel
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
+
import cv2
|
7 |
import tempfile
|
8 |
import os
|
9 |
|
10 |
+
def save_video_frames(frames, output_path, fps=30):
|
11 |
+
"""
|
12 |
+
Save video frames using OpenCV instead of moviepy
|
13 |
+
"""
|
14 |
+
# Get frame dimensions
|
15 |
+
height, width = frames[0].shape[:2]
|
16 |
+
|
17 |
+
# Initialize video writer
|
18 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
19 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
20 |
+
|
21 |
+
# Write frames
|
22 |
+
for frame in frames:
|
23 |
+
# Convert from RGB to BGR (OpenCV uses BGR)
|
24 |
+
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
25 |
+
out.write(frame_bgr)
|
26 |
+
|
27 |
+
# Release video writer
|
28 |
+
out.release()
|
29 |
+
|
30 |
def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
|
31 |
"""
|
32 |
Generate a video from an image using LTX-Video and image captioning.
|
|
|
|
|
|
|
|
|
|
|
33 |
"""
|
34 |
try:
|
35 |
if progress_bar:
|
|
|
72 |
if progress_bar:
|
73 |
progress_bar.progress(0.8, "Creating final video...")
|
74 |
|
75 |
+
# Convert frames to numpy arrays
|
76 |
frames = [np.array(frame) for frame in video_frames]
|
77 |
|
78 |
# Create temporary file for video
|
79 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
80 |
output_path = tmp_file.name
|
81 |
|
82 |
+
# Save video using OpenCV
|
83 |
+
save_video_frames(frames, output_path)
|
|
|
84 |
|
85 |
if progress_bar:
|
86 |
progress_bar.progress(1.0, "Video generation complete!")
|
|
|
89 |
|
90 |
except Exception as e:
|
91 |
st.error(f"Error generating video: {str(e)}")
|
92 |
+
raise # Re-raise the exception to see the full error message
|
93 |
return None, None
|
94 |
|
95 |
def main():
|
|
|
114 |
|
115 |
# Generate button
|
116 |
if st.button("Generate Video"):
|
117 |
+
try:
|
118 |
+
# Create a progress bar
|
119 |
+
progress_text = "Operation in progress. Please wait..."
|
120 |
+
my_bar = st.progress(0, text=progress_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
# Generate video
|
123 |
+
video_path, caption = generate_video_from_image(image, duration, my_bar)
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
+
if video_path and os.path.exists(video_path):
|
126 |
+
# Read the video file
|
127 |
+
with open(video_path, 'rb') as video_file:
|
128 |
+
video_bytes = video_file.read()
|
129 |
+
|
130 |
+
# Create download button
|
131 |
+
st.download_button(
|
132 |
+
label="Download Video",
|
133 |
+
data=video_bytes,
|
134 |
+
file_name="generated_video.mp4",
|
135 |
+
mime="video/mp4"
|
136 |
+
)
|
137 |
+
|
138 |
+
# Display video
|
139 |
+
st.video(video_bytes)
|
140 |
+
|
141 |
+
# Clean up temporary file
|
142 |
+
os.unlink(video_path)
|
143 |
+
else:
|
144 |
+
st.error("Failed to generate video. Please try again.")
|
145 |
+
|
146 |
+
except Exception as e:
|
147 |
+
st.error(f"An error occurred: {str(e)}")
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
main()
|