Spaces:
Runtime error
Runtime error
Update mask_sp_app.py
Browse files- mask_sp_app.py +74 -0
mask_sp_app.py
CHANGED
@@ -1,3 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from diffusers_helper.hf_login import login
|
2 |
import os
|
3 |
os.environ['HF_HOME'] = os.path.abspath(os.path.realpath(os.path.join(os.path.dirname(__file__), './hf_download')))
|
|
|
1 |
+
'''
|
2 |
+
import os
|
3 |
+
from datasets import load_dataset
|
4 |
+
from PIL import Image
|
5 |
+
from gradio_client import Client, handle_file
|
6 |
+
from shutil import copy2
|
7 |
+
|
8 |
+
# 1. 加载HuggingFace数据集
|
9 |
+
dataset = load_dataset("svjack/Xiang_guitar_DreamO_Images")
|
10 |
+
|
11 |
+
# 2. 创建本地目录结构
|
12 |
+
os.makedirs("input_images", exist_ok=True)
|
13 |
+
os.makedirs("xiang_output_videos", exist_ok=True)
|
14 |
+
|
15 |
+
# 3. 初始化Gradio客户端
|
16 |
+
client = Client("http://localhost:7860/")
|
17 |
+
|
18 |
+
# 5. 定义生成空白图片的函数
|
19 |
+
def generate_blank_image(image_path, output_path, color='black'):
|
20 |
+
with Image.open(image_path) as img:
|
21 |
+
width, height = img.size
|
22 |
+
|
23 |
+
fill_color = (255, 255, 255) if color.lower() == 'white' else (0, 0, 0)
|
24 |
+
new_img = Image.new('RGB', (width, height), fill_color)
|
25 |
+
new_img.save(output_path)
|
26 |
+
|
27 |
+
# 4. 定义处理函数
|
28 |
+
def process_image(idx, image):
|
29 |
+
try:
|
30 |
+
# 保存原始图片
|
31 |
+
input_path = f"input_images/{idx:04d}.png"
|
32 |
+
image.save(input_path)
|
33 |
+
|
34 |
+
# 生成空白掩码图片
|
35 |
+
mask_path = f"input_images/{idx:04d}_mask.png"
|
36 |
+
generate_blank_image(input_path, mask_path, color='white')
|
37 |
+
|
38 |
+
# 调用API生成视频
|
39 |
+
result = client.predict(
|
40 |
+
input_image=handle_file(input_path),
|
41 |
+
input_mask=handle_file(mask_path),
|
42 |
+
prompt="a man playing guitar",
|
43 |
+
t2v=False,
|
44 |
+
n_prompt="",
|
45 |
+
seed=31337,
|
46 |
+
total_second_length=5,
|
47 |
+
latent_window_size=9,
|
48 |
+
steps=25,
|
49 |
+
cfg=1,
|
50 |
+
gs=10,
|
51 |
+
rs=0,
|
52 |
+
gpu_memory_preservation=4,
|
53 |
+
use_teacache=False,
|
54 |
+
mp4_crf=16,
|
55 |
+
api_name="/process"
|
56 |
+
)
|
57 |
+
|
58 |
+
# 复制并重命名视频文件
|
59 |
+
output_filename = f"{idx:04d}.mp4"
|
60 |
+
copy2(result[0]["video"], os.path.join("xiang_output_videos", output_filename))
|
61 |
+
|
62 |
+
print(f"已处理第{idx}张图片,生成视频: {output_filename}")
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
print(f"处理第{idx}张图片时出错: {str(e)}")
|
66 |
+
|
67 |
+
# 6. 遍历数据集并处理每张图片
|
68 |
+
for idx, item in enumerate(dataset["train"]):
|
69 |
+
process_image(idx, item["image"])
|
70 |
+
|
71 |
+
print("所有图片处理完成!")
|
72 |
+
|
73 |
+
'''
|
74 |
+
|
75 |
from diffusers_helper.hf_login import login
|
76 |
import os
|
77 |
os.environ['HF_HOME'] = os.path.abspath(os.path.realpath(os.path.join(os.path.dirname(__file__), './hf_download')))
|