alvinichi commited on
Commit
3328cca
·
1 Parent(s): 4642fe2

udpate lib

Browse files
Files changed (2) hide show
  1. app.py +90 -68
  2. requirements.txt +5 -4
app.py CHANGED
@@ -1,94 +1,111 @@
1
  import gradio as gr
2
- import os
3
  import torch
4
- import numpy as np
 
 
5
  from PIL import Image
6
- from diffusers import DiffusionPipeline, DDIMScheduler
7
- from diffusers.utils import export_to_video
 
8
 
9
- # Khởi tạo mô hình
10
- def load_model():
11
- pipe = DiffusionPipeline.from_pretrained(
12
- "guoyww/animatediff-motion-adapter-v1-5",
13
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
 
 
14
  )
15
- pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
16
- pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
17
- return pipe
 
 
 
 
 
 
18
 
19
- # Hàm xử chính để tạo video từ ảnh
20
- def animate_person(image, prompt, motion_bucket_id=127, fps=8):
 
 
 
 
 
 
21
  if image is None:
22
  return None, "Vui lòng tải lên một hình ảnh."
23
 
24
  try:
25
- # Xử lý và chuẩn bị hình ảnh
26
  if image.mode != "RGB":
27
  image = image.convert("RGB")
28
 
29
- # Resize hình ảnh để phù hợp với mô hình
30
- w, h = image.size
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
- # Tạo mặt nạ để tập trung vào chủ thể (người)
39
- # Mặt nạ đơn giản - trong thực tế có thể cần mô hình phân đoạn người phức tạp hơn
 
 
 
 
 
 
40
 
41
- # Tải mô hình
42
- pipe = load_model()
 
43
 
44
- # Tạo video
45
- if not prompt:
46
- prompt = "A person moving naturally, photorealistic, high quality"
47
 
48
- # Thêm hướng dẫn về chuyển động người để có kết quả tốt hơn
49
- full_prompt = f"{prompt}, person in motion, smooth movement, natural pose, high quality, detailed"
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Sinh video
52
- output = pipe(
53
- prompt=full_prompt,
54
- image=image,
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
- # Xuất video
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="Tạo video người chuyển động từ ảnh") as demo:
73
- gr.Markdown("# Tạo video người chuyển động từ ảnh")
74
- gr.Markdown("Tải lên ảnh người và xem họ chuyển động tự nhiên trong video")
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
- prompt_input = gr.Textbox(
80
- label="Mô tả chuyển động",
81
- placeholder="Mô tả cách người trong ảnh sẽ chuyển động...",
82
- value="Person walking naturally, photorealistic"
83
- )
84
- motion_input = gr.Slider(
85
- minimum=1, maximum=255, value=127, step=1,
86
- label="Mức độ chuyển động (1-255)"
87
- )
88
- fps_input = gr.Slider(
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=animate_person,
100
- inputs=[image_input, prompt_input, motion_input, fps_input],
101
  outputs=[output_video, output_message]
102
  )
103
 
104
- gr.Markdown("### Lưu ý")
105
- gr.Markdown("- Quá trình tạo video thể mất vài phút")
106
- gr.Markdown("- Kết quả tốt nhất với ảnh người nét, chụp thẳng")
107
- gr.Markdown("- Sử dụng prompt cụ thể để điều khiển kiểu chuyển động")
 
 
 
 
 
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 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ả 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
- torchvision
4
- diffusers>=0.24.0
5
- transformers>=4.31.0
6
- accelerate
 
 
1
  gradio==4.0.2
2
  torch
3
+ numpy
4
+ Pillow
5
+ huggingface_hub
6
+ moviepy
7
+ requests