alvinichi commited on
Commit
0090ad3
·
1 Parent(s): 1d2f8c3

Initial commit

Browse files
Files changed (3) hide show
  1. README.md +20 -14
  2. app.py +94 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,14 +1,20 @@
1
- ---
2
- title: ImageToVideo
3
- emoji: 👁
4
- colorFrom: pink
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 5.25.2
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
- short_description: image to video generator
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
1
+ # Ứng dụng tạo video từ hình ảnh
2
+
3
+ Ứng dụng này cho phép người dùng tải lên hình ảnh và tạo video chuyển động từ hình ảnh đó sử dụng mô hình AI.
4
+
5
+ ## Cách sử dụng
6
+
7
+ 1. Tải lên một hình ảnh
8
+ 2. Nhập mô tả cho chuyển động bạn muốn tạo
9
+ 3. Điều chỉnh các tham số (tùy chọn)
10
+ 4. Nhấn "Tạo video"
11
+ 5. Đợi quá trình xử lý hoàn tất và tải xuống video
12
+
13
+ ## Mô hình được sử dụng
14
+
15
+ Ứng dụng này sử dụng mô hình AnimateDiff để tạo hoạt ảnh từ hình ảnh tĩnh.
16
+
17
+ ## Tham khảo
18
+
19
+ - [AnimateDiff GitHub](https://github.com/guoyww/AnimateDiff)
20
+ - [HuggingFace Diffusers](https://huggingface.co/docs/diffusers/index)
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import AnimationPipeline, DDIMScheduler
4
+ from huggingface_hub import hf_hub_download
5
+ import os
6
+ from PIL import Image
7
+ import numpy as np
8
+ from moviepy.editor import ImageSequenceClip
9
+
10
+ # Kiểm tra xem có GPU không
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ print(f"Using device: {device}")
13
+
14
+ # Tải mô hình (đây là ví dụ với AnimateDiff)
15
+ def load_model():
16
+ pipe = AnimationPipeline.from_pretrained(
17
+ "guoyww/animatediff-motion-adapter-v1-5",
18
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
19
+ )
20
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
21
+ pipe = pipe.to(device)
22
+ return pipe
23
+
24
+ # Hàm tạo video từ hình ảnh
25
+ def generate_video(image, prompt, num_frames=24, guidance_scale=7.5):
26
+ # Tiền xử lý hình ảnh
27
+ if image is None:
28
+ return None, "Vui lòng tải lên một hình ảnh"
29
+
30
+ # Đảm bảo hình ảnh là định dạng RGB
31
+ if image.mode != "RGB":
32
+ image = image.convert("RGB")
33
+
34
+ # Thay đổi kích thước hình ảnh về 512x512 (kích thước phổ biến cho mô hình)
35
+ image = image.resize((512, 512))
36
+
37
+ try:
38
+ # Tải mô hình (có thể mất thời gian lần đầu tiên)
39
+ pipe = load_model()
40
+
41
+ # Tạo video
42
+ output = pipe(
43
+ prompt=prompt,
44
+ image=image,
45
+ num_inference_steps=30,
46
+ guidance_scale=guidance_scale,
47
+ num_frames=num_frames
48
+ )
49
+
50
+ # Xử lý kết quả
51
+ frames = output.frames[0]
52
+
53
+ # Chuyển đổi frames thành video
54
+ frame_array = [np.array(frame) for frame in frames]
55
+ clip = ImageSequenceClip(frame_array, fps=8)
56
+
57
+ # Lưu video
58
+ output_path = "output_video.mp4"
59
+ clip.write_videofile(output_path, codec="libx264")
60
+
61
+ return output_path, "Video đã được tạo thành công!"
62
+ except Exception as e:
63
+ return None, f"Lỗi: {str(e)}"
64
+
65
+ # Tạo giao diện Gradio
66
+ with gr.Blocks(title="Ứng dụng tạo video từ hình ảnh") as demo:
67
+ gr.Markdown("# Tạo video từ hình ảnh sử dụng AI")
68
+ gr.Markdown("Tải lên một hình ảnh và nhập mô tả để tạo video chuyển động.")
69
+
70
+ with gr.Row():
71
+ with gr.Column():
72
+ image_input = gr.Image(type="pil", label="Tải lên hình ảnh")
73
+ prompt_input = gr.Textbox(label="Mô tả chuyển động",
74
+ placeholder="Mô tả cách bạn muốn hình ảnh chuyển động...",
75
+ value="A gentle camera zoom in, cinematic lighting")
76
+ num_frames = gr.Slider(minimum=16, maximum=48, value=24, step=8, label="Số khung hình")
77
+ guidance = gr.Slider(minimum=1.0, maximum=15.0, value=7.5, step=0.5, label="Guidance Scale")
78
+ submit_btn = gr.Button("Tạo video")
79
+
80
+ with gr.Column():
81
+ output_video = gr.Video(label="Video kết quả")
82
+ output_message = gr.Textbox(label="Thông báo")
83
+
84
+ submit_btn.click(
85
+ fn=generate_video,
86
+ inputs=[image_input, prompt_input, num_frames, guidance],
87
+ outputs=[output_video, output_message]
88
+ )
89
+
90
+ gr.Markdown("### Lưu ý")
91
+ gr.Markdown("- Quá trình tạo video có thể mất vài phút, đặc biệt là khi tải mô hình lần đầu")
92
+ gr.Markdown("- Kích thước hình ảnh sẽ được thay đổi về 512x512 pixel")
93
+
94
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ gradio==4.0.2
2
+ torch
3
+ torchvision
4
+ diffusers
5
+ transformers
6
+ accelerate
7
+ huggingface_hub
8
+ pillow
9
+ moviepy