Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ import tempfile
|
|
8 |
import os
|
9 |
import threading
|
10 |
import traceback
|
|
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
CORS(app)
|
@@ -33,6 +35,25 @@ def download_pipeline():
|
|
33 |
print(f"Error downloading pipeline: {e}")
|
34 |
return False
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def generate_and_export_animation(prompt):
|
38 |
global pipe
|
@@ -48,9 +69,9 @@ def generate_and_export_animation(prompt):
|
|
48 |
output = pipe(
|
49 |
prompt=prompt,
|
50 |
negative_prompt="bad quality, worse quality, low resolution, blur",
|
51 |
-
num_frames=
|
52 |
guidance_scale=2.0,
|
53 |
-
num_inference_steps=
|
54 |
)
|
55 |
print('Video frames generated')
|
56 |
|
@@ -58,7 +79,8 @@ def generate_and_export_animation(prompt):
|
|
58 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
|
59 |
temp_video_path = temp_file.name
|
60 |
print('temp_video_path', temp_video_path)
|
61 |
-
export_to_video(output.frames[0], temp_video_path)
|
|
|
62 |
|
63 |
with open(temp_video_path, 'rb') as video_file:
|
64 |
video_binary = video_file.read()
|
|
|
8 |
import os
|
9 |
import threading
|
10 |
import traceback
|
11 |
+
import cv2
|
12 |
+
import numpy as np
|
13 |
|
14 |
app = Flask(__name__)
|
15 |
CORS(app)
|
|
|
35 |
print(f"Error downloading pipeline: {e}")
|
36 |
return False
|
37 |
|
38 |
+
def generate_and_export_animation(frames_list, temp_video_path):
|
39 |
+
# Convert PIL images to numpy arrays
|
40 |
+
frames_np = [np.array(frame) for frame in frames_list]
|
41 |
+
|
42 |
+
# Determine the dimensions (height, width) of the frames
|
43 |
+
height, width, _ = frames_np[0].shape
|
44 |
+
|
45 |
+
# Create a VideoWriter object to write the frames to a video file
|
46 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4 format
|
47 |
+
out = cv2.VideoWriter(temp_video_path, fourcc, 12, (width, height)) # Adjust fps as needed
|
48 |
+
|
49 |
+
# Write each frame to the video file
|
50 |
+
for frame_np in frames_np:
|
51 |
+
# Convert RGB image (numpy array) to BGR (OpenCV format) for writing
|
52 |
+
frame_bgr = cv2.cvtColor(frame_np, cv2.COLOR_RGB2BGR)
|
53 |
+
out.write(frame_bgr)
|
54 |
+
|
55 |
+
# Release the VideoWriter object
|
56 |
+
out.release()
|
57 |
|
58 |
def generate_and_export_animation(prompt):
|
59 |
global pipe
|
|
|
69 |
output = pipe(
|
70 |
prompt=prompt,
|
71 |
negative_prompt="bad quality, worse quality, low resolution, blur",
|
72 |
+
num_frames=16,
|
73 |
guidance_scale=2.0,
|
74 |
+
num_inference_steps=6
|
75 |
)
|
76 |
print('Video frames generated')
|
77 |
|
|
|
79 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
|
80 |
temp_video_path = temp_file.name
|
81 |
print('temp_video_path', temp_video_path)
|
82 |
+
# export_to_video(output.frames[0], temp_video_path)
|
83 |
+
generate_and_export_animation(output.frames[0], temp_video_path)
|
84 |
|
85 |
with open(temp_video_path, 'rb') as video_file:
|
86 |
video_binary = video_file.read()
|