Spaces:
Running
Running
Add corrected video_generator_animatediff.py
Browse files
motion_utils/video_generator_animatediff.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
import imageio
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from diffusers import AnimateDiffPipeline
|
| 8 |
+
from diffusers.utils import load_image
|
| 9 |
+
|
| 10 |
+
def generate_video_from_image(image_path, prompt, duration, fps, output_path):
|
| 11 |
+
# Charger l'image et la convertir en RGB
|
| 12 |
+
image = load_image(image_path).convert("RGB")
|
| 13 |
+
|
| 14 |
+
# Charger le pipeline AnimateDiff
|
| 15 |
+
model_id = "cerspense/zeroscope_v2_576w"
|
| 16 |
+
pipe = AnimateDiffPipeline.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16")
|
| 17 |
+
pipe.enable_model_cpu_offload()
|
| 18 |
+
|
| 19 |
+
# Calculer le nombre de frames à générer
|
| 20 |
+
num_frames = duration * fps
|
| 21 |
+
|
| 22 |
+
# Générer les frames à partir du prompt et de l'image
|
| 23 |
+
output = pipe(prompt=prompt, image=image, num_inference_steps=25, guidance_scale=7.5, num_frames=num_frames)
|
| 24 |
+
frames = output.frames
|
| 25 |
+
|
| 26 |
+
# Convertir les frames en tableau numpy et sauvegarder en vidéo
|
| 27 |
+
frames = [np.array(frame) for frame in frames]
|
| 28 |
+
imageio.mimsave(output_path, frames, fps=fps)
|