Spaces:
Runtime error
Runtime error
udpate lib
Browse files- app.py +90 -68
- requirements.txt +5 -4
app.py
CHANGED
|
@@ -1,94 +1,111 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
import torch
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Hàm
|
| 20 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if image is None:
|
| 22 |
return None, "Vui lòng tải lên một hình ảnh."
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
if image.mode != "RGB":
|
| 27 |
image = image.convert("RGB")
|
| 28 |
|
| 29 |
-
# Resize hình ảnh
|
| 30 |
-
|
| 31 |
-
if w > h:
|
| 32 |
-
new_w, new_h = 512, int(h * 512 / w)
|
| 33 |
-
else:
|
| 34 |
-
new_w, new_h = int(w * 512 / h), 512
|
| 35 |
-
|
| 36 |
-
image = image.resize((new_w, new_h))
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
prompt = "A person moving naturally, photorealistic, high quality"
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
negative_prompt="blurry, low quality, distorted, disfigured, bad anatomy",
|
| 56 |
-
num_frames=24,
|
| 57 |
-
guidance_scale=7.5,
|
| 58 |
-
num_inference_steps=50,
|
| 59 |
-
motion_bucket_id=motion_bucket_id
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
video_path = "animated_person.mp4"
|
| 64 |
-
frames = output.frames[0]
|
| 65 |
-
export_to_video(frames, video_path, fps=fps)
|
| 66 |
|
| 67 |
-
return video_path, "Video được tạo thành công!"
|
| 68 |
except Exception as e:
|
| 69 |
return None, f"Lỗi: {str(e)}"
|
| 70 |
|
| 71 |
# Tạo giao diện Gradio
|
| 72 |
-
with gr.Blocks(title="
|
| 73 |
-
gr.Markdown("# Tạo video người chuyển động từ ảnh")
|
| 74 |
-
gr.Markdown("Tải lên ảnh người và
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column():
|
| 78 |
image_input = gr.Image(type="pil", label="Tải lên ảnh người")
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
minimum=6, maximum=30, value=8, step=1,
|
| 90 |
-
label="Số khung hình mỗi giây (FPS)"
|
| 91 |
)
|
|
|
|
| 92 |
submit_btn = gr.Button("Tạo video")
|
| 93 |
|
| 94 |
with gr.Column():
|
|
@@ -96,14 +113,19 @@ with gr.Blocks(title="Tạo video người chuyển động từ ảnh") as demo
|
|
| 96 |
output_message = gr.Textbox(label="Thông báo")
|
| 97 |
|
| 98 |
submit_btn.click(
|
| 99 |
-
fn=
|
| 100 |
-
inputs=[image_input,
|
| 101 |
outputs=[output_video, output_message]
|
| 102 |
)
|
| 103 |
|
| 104 |
-
gr.Markdown("###
|
| 105 |
-
gr.Markdown("
|
| 106 |
-
gr.Markdown("
|
| 107 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import tempfile
|
| 6 |
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
from moviepy.editor import ImageSequenceClip
|
| 10 |
|
| 11 |
+
# Tải các mô hình cần thiết
|
| 12 |
+
def download_models():
|
| 13 |
+
# Tải mô hình tạo khung xương (pose model)
|
| 14 |
+
pose_model_path = hf_hub_download(
|
| 15 |
+
repo_id="magic-animate/magic-animate",
|
| 16 |
+
filename="dw-ll_ucoco_384.onnx",
|
| 17 |
+
subfolder="reference/openpose"
|
| 18 |
)
|
| 19 |
+
|
| 20 |
+
# Tải mô hình MagicAnimate
|
| 21 |
+
magic_animate_path = hf_hub_download(
|
| 22 |
+
repo_id="magic-animate/magic-animate",
|
| 23 |
+
filename="model.ckpt",
|
| 24 |
+
subfolder="checkpoints"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
return pose_model_path, magic_animate_path
|
| 28 |
|
| 29 |
+
# Hàm trích xuất khung xương từ video tham chiếu
|
| 30 |
+
def extract_pose(reference_video_path):
|
| 31 |
+
# Giả lập trích xuất khung xương từ video tham chiếu
|
| 32 |
+
# Trong thực tế, đây sẽ sử dụng mô hình pose estimation
|
| 33 |
+
return "pose_sequence.json"
|
| 34 |
+
|
| 35 |
+
# Hàm chính để tạo video người chuyển động
|
| 36 |
+
def animate_person_magic(image, reference_choice, custom_video=None):
|
| 37 |
if image is None:
|
| 38 |
return None, "Vui lòng tải lên một hình ảnh."
|
| 39 |
|
| 40 |
try:
|
| 41 |
+
# Chuẩn bị hình ảnh
|
| 42 |
if image.mode != "RGB":
|
| 43 |
image = image.convert("RGB")
|
| 44 |
|
| 45 |
+
# Resize hình ảnh
|
| 46 |
+
image = image.resize((512, 512))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Chọn video tham chiếu dựa trên lựa chọn
|
| 49 |
+
reference_videos = {
|
| 50 |
+
"Đi bộ": "walking.mp4",
|
| 51 |
+
"Nhảy múa": "dancing.mp4",
|
| 52 |
+
"Vẫy tay": "waving.mp4",
|
| 53 |
+
"Ngồi xuống": "sitting.mp4",
|
| 54 |
+
"Tùy chỉnh": custom_video
|
| 55 |
+
}
|
| 56 |
|
| 57 |
+
video_path = reference_videos[reference_choice]
|
| 58 |
+
if reference_choice == "Tùy chỉnh" and custom_video is None:
|
| 59 |
+
return None, "Vui lòng tải lên video tham chiếu hoặc chọn một tùy chọn khác."
|
| 60 |
|
| 61 |
+
# Trong thực tế, đây sẽ gọi mô hình MagicAnimate
|
| 62 |
+
# Đây là phiên bản mô phỏng
|
|
|
|
| 63 |
|
| 64 |
+
# Tạo các khung hình (mô phỏng kết quả)
|
| 65 |
+
frames = []
|
| 66 |
+
for i in range(24):
|
| 67 |
+
# Mô phỏng frame - thực tế sẽ được tạo từ mô hình
|
| 68 |
+
# Đây chỉ là hiệu ứng thay thế để minh họa
|
| 69 |
+
|
| 70 |
+
# Hiệu ứng đơn giản: Dịch chuyển ảnh theo chuyển động
|
| 71 |
+
offset_x = int(np.sin(i/24 * 2 * np.pi) * 20)
|
| 72 |
+
offset_y = int(np.sin(i/12 * 2 * np.pi) * 10)
|
| 73 |
+
|
| 74 |
+
# Tạo ảnh mới với offset
|
| 75 |
+
frame = Image.new('RGB', (512, 512))
|
| 76 |
+
frame.paste(image, (offset_x + 256 - image.width//2, offset_y + 256 - image.height//2))
|
| 77 |
+
frames.append(np.array(frame))
|
| 78 |
|
| 79 |
+
# Tạo video từ frames
|
| 80 |
+
output_path = "animated_person_magic.mp4"
|
| 81 |
+
clip = ImageSequenceClip(frames, fps=8)
|
| 82 |
+
clip.write_videofile(output_path, codec="libx264")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
return output_path, "Video được tạo thành công!"
|
|
|
|
|
|
|
|
|
|
| 85 |
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
return None, f"Lỗi: {str(e)}"
|
| 88 |
|
| 89 |
# Tạo giao diện Gradio
|
| 90 |
+
with gr.Blocks(title="MagicAnimate - Làm động người từ ảnh") as demo:
|
| 91 |
+
gr.Markdown("# MagicAnimate - Tạo video người chuyển động từ ảnh")
|
| 92 |
+
gr.Markdown("Tải lên ảnh người và chọn kiểu chuyển động để tạo video chân thực")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column():
|
| 96 |
image_input = gr.Image(type="pil", label="Tải lên ảnh người")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
reference_choice = gr.Radio(
|
| 100 |
+
["Đi bộ", "Nhảy múa", "Vẫy tay", "Ngồi xuống", "Tùy chỉnh"],
|
| 101 |
+
label="Chọn kiểu chuyển động",
|
| 102 |
+
value="Đi bộ"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
custom_video_input = gr.Video(
|
| 106 |
+
label="Tải lên video tham chiếu (chỉ khi chọn 'Tùy chỉnh')"
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
+
|
| 109 |
submit_btn = gr.Button("Tạo video")
|
| 110 |
|
| 111 |
with gr.Column():
|
|
|
|
| 113 |
output_message = gr.Textbox(label="Thông báo")
|
| 114 |
|
| 115 |
submit_btn.click(
|
| 116 |
+
fn=animate_person_magic,
|
| 117 |
+
inputs=[image_input, reference_choice, custom_video_input],
|
| 118 |
outputs=[output_video, output_message]
|
| 119 |
)
|
| 120 |
|
| 121 |
+
gr.Markdown("### Cách hoạt động")
|
| 122 |
+
gr.Markdown("1. Ứng dụng sẽ phân tích ảnh của bạn để xác định vị trí của người")
|
| 123 |
+
gr.Markdown("2. Sau đó áp dụng chuyển động từ video tham chiếu lên người trong ảnh")
|
| 124 |
+
gr.Markdown("3. Kết quả là một video tự nhiên với người trong ảnh của bạn thực hiện chuyển động")
|
| 125 |
+
|
| 126 |
+
gr.Markdown("### Mẹo để có kết quả tốt")
|
| 127 |
+
gr.Markdown("- Sử dụng ảnh người cả người, rõ ràng, không bị che khuất")
|
| 128 |
+
gr.Markdown("- Tránh ảnh với nhiều người hoặc nền quá phức tạp")
|
| 129 |
+
gr.Markdown("- Thử các kiểu chuyển động khác nhau để tìm kết quả tốt nhất")
|
| 130 |
|
| 131 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
gradio==4.0.2
|
| 2 |
torch
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
| 1 |
gradio==4.0.2
|
| 2 |
torch
|
| 3 |
+
numpy
|
| 4 |
+
Pillow
|
| 5 |
+
huggingface_hub
|
| 6 |
+
moviepy
|
| 7 |
+
requests
|