hoangkha1810 commited on
Commit
b326f69
·
verified ·
1 Parent(s): 00e5fef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -3,11 +3,12 @@ import torch
3
  import os
4
  import uuid
5
  from diffusers import AnimateDiffPipeline, EulerDiscreteScheduler
6
- from diffusers.utils import export_to_video
7
  from huggingface_hub import hf_hub_download
8
  from safetensors.torch import load_file
 
9
  import logging
10
  import importlib.util
 
11
 
12
  # Thiết lập logging
13
  logging.basicConfig(level=logging.INFO)
@@ -15,13 +16,11 @@ logger = logging.getLogger(__name__)
15
 
16
  # Kiểm tra backend
17
  def check_backend():
18
- if importlib.util.find_spec("imageio") and importlib.util.find_spec("imageio_ffmpeg"):
19
- logger.info("imageio and imageio-ffmpeg are installed. Using recommended backend.")
20
- elif importlib.util.find_spec("cv2"):
21
- logger.warning("imageio/imageio-ffmpeg not found. Using legacy OpenCV backend.")
22
  else:
23
- logger.error("No suitable backend found for export_to_video. Please install imageio and imageio-ffmpeg.")
24
- raise ImportError("Missing backend for export_to_video. Install with: pip install imageio imageio-ffmpeg")
25
 
26
  check_backend()
27
 
@@ -95,17 +94,35 @@ def generate_image(prompt, base="Realistic", motion="", step=1, progress=gr.Prog
95
  prompt=prompt,
96
  guidance_scale=1.2,
97
  num_inference_steps=step,
 
98
  callback=progress_callback,
99
  callback_steps=1,
100
  width=256,
101
  height=256
102
  )
103
 
104
- # Xuất video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  name = str(uuid.uuid4()).replace("-", "")
106
  path = os.path.join(output_dir, f"{name}.mp4")
107
  logger.info(f"Saving video to {path}")
108
- export_to_video(output.frames[0], path, fps=10)
 
109
 
110
  # Kiểm tra file video
111
  if not os.path.exists(path):
 
3
  import os
4
  import uuid
5
  from diffusers import AnimateDiffPipeline, EulerDiscreteScheduler
 
6
  from huggingface_hub import hf_hub_download
7
  from safetensors.torch import load_file
8
+ from moviepy.editor import ImageSequenceClip
9
  import logging
10
  import importlib.util
11
+ import numpy as np
12
 
13
  # Thiết lập logging
14
  logging.basicConfig(level=logging.INFO)
 
16
 
17
  # Kiểm tra backend
18
  def check_backend():
19
+ if importlib.util.find_spec("moviepy"):
20
+ logger.info("moviepy is installed. Using moviepy for video export.")
 
 
21
  else:
22
+ logger.error("moviepy is required for video export. Install with: pip install moviepy")
23
+ raise ImportError("Missing moviepy. Install with: pip install moviepy")
24
 
25
  check_backend()
26
 
 
94
  prompt=prompt,
95
  guidance_scale=1.2,
96
  num_inference_steps=step,
97
+ num_frames=32, # Tạo 32 khung hình (sẽ lặp lại để đạt 192)
98
  callback=progress_callback,
99
  callback_steps=1,
100
  width=256,
101
  height=256
102
  )
103
 
104
+ # Chuẩn bị khung hình
105
+ frames = output.frames[0] # frames là list các numpy arrays
106
+ target_fps = 24
107
+ target_duration = 8 # 8 giây
108
+ target_frames = target_fps * target_duration # 192 khung hình
109
+
110
+ # Lặp lại khung hình để đạt 192
111
+ current_frames = len(frames)
112
+ if current_frames < target_frames:
113
+ repeat_factor = (target_frames + current_frames - 1) // current_frames
114
+ frames = np.repeat(frames, repeat_factor, axis=0)[:target_frames]
115
+ elif current_frames > target_frames:
116
+ frames = frames[:target_frames]
117
+
118
+ logger.info(f"Generated {len(frames)} frames for video.")
119
+
120
+ # Xuất video với moviepy
121
  name = str(uuid.uuid4()).replace("-", "")
122
  path = os.path.join(output_dir, f"{name}.mp4")
123
  logger.info(f"Saving video to {path}")
124
+ clip = ImageSequenceClip(list(frames), fps=target_fps)
125
+ clip.write_videofile(path, codec="libx264", audio=False)
126
 
127
  # Kiểm tra file video
128
  if not os.path.exists(path):