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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ.setdefault("GRADIO_TEMP_DIR", "/data2/lzliu/tmp/gradio")
|
| 3 |
+
os.environ.setdefault("TMPDIR", "/data2/lzliu/tmp")
|
| 4 |
+
os.makedirs("/data2/lzliu/tmp/gradio", exist_ok=True)
|
| 5 |
+
os.makedirs("/data2/lzliu/tmp", exist_ok=True)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# 其余保持不变
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
import logging
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import torch
|
| 14 |
+
import os
|
| 15 |
+
import uuid
|
| 16 |
+
from test_stablehairv2 import log_validation
|
| 17 |
+
from test_stablehairv2 import UNet3DConditionModel, ControlNetModel, CCProjection
|
| 18 |
+
from test_stablehairv2 import AutoTokenizer, CLIPVisionModelWithProjection, AutoencoderKL, UNet2DConditionModel
|
| 19 |
+
from omegaconf import OmegaConf
|
| 20 |
+
import numpy as np
|
| 21 |
+
import cv2
|
| 22 |
+
from test_stablehairv2 import _maybe_align_image
|
| 23 |
+
from HairMapper.hair_mapper_run import bald_head
|
| 24 |
+
|
| 25 |
+
import base64
|
| 26 |
+
|
| 27 |
+
with open("imgs/background.jpg", "rb") as f:
|
| 28 |
+
b64_img = base64.b64encode(f.read()).decode()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def inference(id_image, hair_image):
|
| 32 |
+
os.makedirs("gradio_inputs", exist_ok=True)
|
| 33 |
+
os.makedirs("gradio_outputs", exist_ok=True)
|
| 34 |
+
|
| 35 |
+
id_path = "gradio_inputs/id.png"
|
| 36 |
+
hair_path = "gradio_inputs/hair.png"
|
| 37 |
+
id_image.save(id_path)
|
| 38 |
+
hair_image.save(hair_path)
|
| 39 |
+
|
| 40 |
+
# ===== 图像对齐 =====
|
| 41 |
+
aligned_id = _maybe_align_image(id_path, output_size=1024, prefer_cuda=True)
|
| 42 |
+
aligned_hair = _maybe_align_image(hair_path, output_size=1024, prefer_cuda=True)
|
| 43 |
+
|
| 44 |
+
# 保存对齐结果(方便 Gradio 输出)
|
| 45 |
+
aligned_id_path = "gradio_outputs/aligned_id.png"
|
| 46 |
+
aligned_hair_path = "gradio_outputs/aligned_hair.png"
|
| 47 |
+
cv2.imwrite(aligned_id_path, cv2.cvtColor(aligned_id, cv2.COLOR_RGB2BGR))
|
| 48 |
+
cv2.imwrite(aligned_hair_path, cv2.cvtColor(aligned_hair, cv2.COLOR_RGB2BGR))
|
| 49 |
+
|
| 50 |
+
# ===== 调用 HairMapper 秃头化 =====
|
| 51 |
+
bald_id_path = "gradio_outputs/bald_id.png"
|
| 52 |
+
cv2.imwrite(bald_id_path, cv2.cvtColor(aligned_id, cv2.COLOR_RGB2BGR))
|
| 53 |
+
bald_head(bald_id_path, bald_id_path)
|
| 54 |
+
|
| 55 |
+
# ===== 原本的 Args =====
|
| 56 |
+
class Args:
|
| 57 |
+
pretrained_model_name_or_path = "./stable-diffusion-v1-5/stable-diffusion-v1-5"
|
| 58 |
+
model_path = "./trained_model"
|
| 59 |
+
image_encoder = "openai/clip-vit-large-patch14"
|
| 60 |
+
controlnet_model_name_or_path = None
|
| 61 |
+
revision = None
|
| 62 |
+
output_dir = "gradio_outputs"
|
| 63 |
+
seed = 42
|
| 64 |
+
num_validation_images = 1
|
| 65 |
+
validation_ids = [aligned_id_path] # 用对齐后的图像
|
| 66 |
+
validation_hairs = [aligned_hair_path] # 用对齐后的图像
|
| 67 |
+
use_fp16 = False
|
| 68 |
+
|
| 69 |
+
args = Args()
|
| 70 |
+
|
| 71 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 72 |
+
|
| 73 |
+
# 初始化 logger
|
| 74 |
+
logging.basicConfig(
|
| 75 |
+
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
|
| 76 |
+
datefmt="%m/%d/%Y %H:%M:%S",
|
| 77 |
+
level=logging.INFO,
|
| 78 |
+
)
|
| 79 |
+
logger = logging.getLogger(__name__)
|
| 80 |
+
|
| 81 |
+
# ===== 模型加载(和 main() 对齐) =====
|
| 82 |
+
tokenizer = AutoTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="tokenizer",
|
| 83 |
+
revision=args.revision)
|
| 84 |
+
image_encoder = CLIPVisionModelWithProjection.from_pretrained(args.image_encoder, revision=args.revision).to(device)
|
| 85 |
+
vae = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder="vae", revision=args.revision).to(
|
| 86 |
+
device, dtype=torch.float32)
|
| 87 |
+
|
| 88 |
+
infer_config = OmegaConf.load('./configs/inference/inference_v2.yaml')
|
| 89 |
+
|
| 90 |
+
unet2 = UNet2DConditionModel.from_pretrained(
|
| 91 |
+
args.pretrained_model_name_or_path, subfolder="unet", revision=args.revision, torch_dtype=torch.float32
|
| 92 |
+
).to(device)
|
| 93 |
+
conv_in_8 = torch.nn.Conv2d(8, unet2.conv_in.out_channels, kernel_size=unet2.conv_in.kernel_size,
|
| 94 |
+
padding=unet2.conv_in.padding)
|
| 95 |
+
conv_in_8.requires_grad_(False)
|
| 96 |
+
unet2.conv_in.requires_grad_(False)
|
| 97 |
+
torch.nn.init.zeros_(conv_in_8.weight)
|
| 98 |
+
conv_in_8.weight[:, :4, :, :].copy_(unet2.conv_in.weight)
|
| 99 |
+
conv_in_8.bias.copy_(unet2.conv_in.bias)
|
| 100 |
+
unet2.conv_in = conv_in_8
|
| 101 |
+
|
| 102 |
+
controlnet = ControlNetModel.from_unet(unet2).to(device)
|
| 103 |
+
state_dict2 = torch.load(os.path.join(args.model_path, "pytorch_model.bin"), map_location="cpu")
|
| 104 |
+
controlnet.load_state_dict(state_dict2, strict=False)
|
| 105 |
+
|
| 106 |
+
prefix = "motion_module"
|
| 107 |
+
ckpt_num = "4140000"
|
| 108 |
+
save_path = os.path.join(args.model_path, f"{prefix}-{ckpt_num}.pth")
|
| 109 |
+
denoising_unet = UNet3DConditionModel.from_pretrained_2d(
|
| 110 |
+
args.pretrained_model_name_or_path,
|
| 111 |
+
save_path,
|
| 112 |
+
subfolder="unet",
|
| 113 |
+
unet_additional_kwargs=infer_config.unet_additional_kwargs,
|
| 114 |
+
).to(device)
|
| 115 |
+
|
| 116 |
+
cc_projection = CCProjection().to(device)
|
| 117 |
+
state_dict3 = torch.load(os.path.join(args.model_path, "pytorch_model_1.bin"), map_location="cpu")
|
| 118 |
+
cc_projection.load_state_dict(state_dict3, strict=False)
|
| 119 |
+
|
| 120 |
+
from ref_encoder.reference_unet import ref_unet
|
| 121 |
+
Hair_Encoder = ref_unet.from_pretrained(
|
| 122 |
+
args.pretrained_model_name_or_path, subfolder="unet", revision=args.revision, low_cpu_mem_usage=False,
|
| 123 |
+
device_map=None, ignore_mismatched_sizes=True
|
| 124 |
+
).to(device)
|
| 125 |
+
state_dict2 = torch.load(os.path.join(args.model_path, "pytorch_model_2.bin"), map_location="cpu")
|
| 126 |
+
Hair_Encoder.load_state_dict(state_dict2, strict=False)
|
| 127 |
+
|
| 128 |
+
# 推理
|
| 129 |
+
log_validation(
|
| 130 |
+
vae, tokenizer, image_encoder, denoising_unet,
|
| 131 |
+
args, device, logger,
|
| 132 |
+
cc_projection, controlnet, Hair_Encoder
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
output_video = os.path.join(args.output_dir, "validation", "generated_video_0.mp4")
|
| 136 |
+
|
| 137 |
+
# 提取视频帧用于可拖动预览
|
| 138 |
+
frames_dir = os.path.join(args.output_dir, "frames", uuid.uuid4().hex)
|
| 139 |
+
os.makedirs(frames_dir, exist_ok=True)
|
| 140 |
+
cap = cv2.VideoCapture(output_video)
|
| 141 |
+
frames_list = []
|
| 142 |
+
idx = 0
|
| 143 |
+
while True:
|
| 144 |
+
ret, frame = cap.read()
|
| 145 |
+
if not ret:
|
| 146 |
+
break
|
| 147 |
+
fp = os.path.join(frames_dir, f"{idx:03d}.png")
|
| 148 |
+
cv2.imwrite(fp, frame)
|
| 149 |
+
frames_list.append(fp)
|
| 150 |
+
idx += 1
|
| 151 |
+
cap.release()
|
| 152 |
+
|
| 153 |
+
max_frames = len(frames_list) if frames_list else 1
|
| 154 |
+
first_frame = frames_list[0] if frames_list else None
|
| 155 |
+
|
| 156 |
+
return aligned_id_path, aligned_hair_path, bald_id_path, output_video, frames_list, gr.update(minimum=1,
|
| 157 |
+
maximum=max_frames,
|
| 158 |
+
value=1,
|
| 159 |
+
step=1), first_frame
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# Gradio 前端
|
| 163 |
+
# 原 Interface 版本(保留以便回退)
|
| 164 |
+
# demo = gr.Interface(
|
| 165 |
+
# fn=inference,
|
| 166 |
+
# inputs=[
|
| 167 |
+
# gr.Image(type="pil", label="上传身份图(ID Image)"),
|
| 168 |
+
# gr.Image(type="pil", label="上传发型图(Hair Reference Image)")
|
| 169 |
+
# ],
|
| 170 |
+
# outputs=[
|
| 171 |
+
# gr.Image(type="filepath", label="对齐后的身份图"),
|
| 172 |
+
# gr.Image(type="filepath", label="对齐后的发型图"),
|
| 173 |
+
# gr.Image(type="filepath", label="秃头化后的身份图"),
|
| 174 |
+
# gr.Video(label="生成的视频")
|
| 175 |
+
# ],
|
| 176 |
+
# title="StableHairV2 多视角发型迁移",
|
| 177 |
+
# description="上传身份图和发型参考图,查看对齐结果并生成多视角视频"
|
| 178 |
+
# )
|
| 179 |
+
# if __name__ == "__main__":
|
| 180 |
+
# demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 181 |
+
|
| 182 |
+
# Blocks 美化版
|
| 183 |
+
css = f"""
|
| 184 |
+
html, body {{
|
| 185 |
+
height: 100%;
|
| 186 |
+
margin: 0;
|
| 187 |
+
padding: 0;
|
| 188 |
+
}}
|
| 189 |
+
.gradio-container {{
|
| 190 |
+
width: 100% !important;
|
| 191 |
+
height: 100% !important;
|
| 192 |
+
margin: 0 !important;
|
| 193 |
+
padding: 0 !important;
|
| 194 |
+
background-image: url("data:image/jpeg;base64,{b64_img}");
|
| 195 |
+
background-size: cover;
|
| 196 |
+
background-position: center;
|
| 197 |
+
background-attachment: fixed; /* 背景固定 */
|
| 198 |
+
}}
|
| 199 |
+
#title-card {{
|
| 200 |
+
background: rgba(255, 255, 255, 0.8);
|
| 201 |
+
border-radius: 12px;
|
| 202 |
+
padding: 16px 24px;
|
| 203 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
|
| 204 |
+
margin-bottom: 20px;
|
| 205 |
+
}}
|
| 206 |
+
#title-card h2 {{
|
| 207 |
+
text-align: center;
|
| 208 |
+
margin: 4px 0 12px 0;
|
| 209 |
+
font-size: 28px;
|
| 210 |
+
}}
|
| 211 |
+
#title-card p {{
|
| 212 |
+
text-align: center;
|
| 213 |
+
font-size: 16px;
|
| 214 |
+
color: #374151;
|
| 215 |
+
}}
|
| 216 |
+
.out-card {{
|
| 217 |
+
border:1px solid #e5e7eb; border-radius:10px; padding:10px;
|
| 218 |
+
background: rgba(255,255,255,0.85);
|
| 219 |
+
}}
|
| 220 |
+
.two-col {{
|
| 221 |
+
display:grid !important; grid-template-columns: 360px minmax(680px, 1fr); gap:16px
|
| 222 |
+
}}
|
| 223 |
+
.left-pane {{min-width: 360px}}
|
| 224 |
+
.right-pane {{min-width: 680px}}
|
| 225 |
+
/* Tabs 美化 */
|
| 226 |
+
.tabs {{
|
| 227 |
+
background: rgba(255,255,255,0.88);
|
| 228 |
+
border-radius: 12px;
|
| 229 |
+
box-shadow: 0 8px 24px rgba(0,0,0,0.08);
|
| 230 |
+
padding: 8px;
|
| 231 |
+
border: 1px solid #e5e7eb;
|
| 232 |
+
}}
|
| 233 |
+
.tab-nav {{
|
| 234 |
+
display: flex; gap: 8px; margin-bottom: 8px;
|
| 235 |
+
background: transparent;
|
| 236 |
+
border-bottom: 1px solid #e5e7eb;
|
| 237 |
+
padding-bottom: 6px;
|
| 238 |
+
}}
|
| 239 |
+
.tab-nav button {{
|
| 240 |
+
background: rgba(255,255,255,0.7);
|
| 241 |
+
border: 1px solid #e5e7eb;
|
| 242 |
+
backdrop-filter: blur(6px);
|
| 243 |
+
border-radius: 8px;
|
| 244 |
+
padding: 6px 12px;
|
| 245 |
+
color: #111827;
|
| 246 |
+
transition: all .2s ease;
|
| 247 |
+
}}
|
| 248 |
+
.tab-nav button:hover {{
|
| 249 |
+
transform: translateY(-1px);
|
| 250 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.06);
|
| 251 |
+
}}
|
| 252 |
+
.tab-nav button[aria-selected="true"] {{
|
| 253 |
+
background: #4f46e5;
|
| 254 |
+
color: #fff;
|
| 255 |
+
border-color: #4f46e5;
|
| 256 |
+
box-shadow: 0 6px 14px rgba(79,70,229,0.25);
|
| 257 |
+
}}
|
| 258 |
+
.tabitem {{
|
| 259 |
+
background: rgba(255,255,255,0.88);
|
| 260 |
+
border-radius: 10px;
|
| 261 |
+
padding: 8px;
|
| 262 |
+
}}
|
| 263 |
+
/* 发型库滚动限制容器:固定260px高度,内部可滚动 */
|
| 264 |
+
#hair_gallery_wrap {{
|
| 265 |
+
height: 260px !important;
|
| 266 |
+
overflow-y: scroll !important;
|
| 267 |
+
overflow-x: auto !important;
|
| 268 |
+
}}
|
| 269 |
+
#hair_gallery_wrap .grid, #hair_gallery_wrap .wrap {{
|
| 270 |
+
height: 100% !important;
|
| 271 |
+
overflow-y: scroll !important;
|
| 272 |
+
}}
|
| 273 |
+
/* 确保画廊本体占满容���高度,避免滚动条落到页面底部 */
|
| 274 |
+
#hair_gallery {{
|
| 275 |
+
height: 100% !important;
|
| 276 |
+
}}
|
| 277 |
+
"""
|
| 278 |
+
|
| 279 |
+
with gr.Blocks(
|
| 280 |
+
theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"),
|
| 281 |
+
css=css
|
| 282 |
+
) as demo:
|
| 283 |
+
# ==== 顶部 Panel ====
|
| 284 |
+
with gr.Group(elem_id="title-card"):
|
| 285 |
+
gr.Markdown("""
|
| 286 |
+
<h2 id='title'>StableHairV2 多视角发型迁移</h2>
|
| 287 |
+
<p>上传身份图与发型参考图,系统将自动完成 <b>对齐 → 秃头化 → 视频生成</b>。</p>
|
| 288 |
+
""")
|
| 289 |
+
|
| 290 |
+
with gr.Row(elem_classes=["two-col"]):
|
| 291 |
+
with gr.Column(scale=5, min_width=260, elem_classes=["left-pane"]):
|
| 292 |
+
id_input = gr.Image(type="pil", label="身份图", height=200)
|
| 293 |
+
hair_input = gr.Image(type="pil", label="发型参考图", height=200)
|
| 294 |
+
|
| 295 |
+
with gr.Row():
|
| 296 |
+
run_btn = gr.Button("开始生成", variant="primary")
|
| 297 |
+
clear_btn = gr.Button("清空")
|
| 298 |
+
|
| 299 |
+
# ========= 发型库(点击即填充到“发型参考图”) =========
|
| 300 |
+
def _list_imgs(dir_path: str):
|
| 301 |
+
exts = (".png", ".jpg", ".jpeg", ".webp")
|
| 302 |
+
# exts = (".jpg")
|
| 303 |
+
try:
|
| 304 |
+
files = [os.path.join(dir_path, f) for f in sorted(os.listdir(dir_path))
|
| 305 |
+
if f.lower().endswith(exts)]
|
| 306 |
+
return files
|
| 307 |
+
except Exception:
|
| 308 |
+
return []
|
| 309 |
+
|
| 310 |
+
hair_list = _list_imgs("hair_resposity")
|
| 311 |
+
|
| 312 |
+
with gr.Accordion("发型库(点击选择后自动填充)", open=True):
|
| 313 |
+
with gr.Group(elem_id="hair_gallery_wrap"):
|
| 314 |
+
gallery = gr.Gallery(
|
| 315 |
+
value=hair_list,
|
| 316 |
+
columns=4, rows=2, allow_preview=True, label="发型库",
|
| 317 |
+
elem_id="hair_gallery"
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
def _pick_hair(evt: gr.SelectData): # type: ignore[name-defined]
|
| 321 |
+
i = evt.index if hasattr(evt, 'index') else 0
|
| 322 |
+
i = 0 if i is None else int(i)
|
| 323 |
+
if 0 <= i < len(hair_list):
|
| 324 |
+
return gr.update(value=hair_list[i])
|
| 325 |
+
return gr.update()
|
| 326 |
+
|
| 327 |
+
gallery.select(_pick_hair, inputs=None, outputs=hair_input)
|
| 328 |
+
|
| 329 |
+
with gr.Column(scale=7, min_width=520, elem_classes=["right-pane"]):
|
| 330 |
+
with gr.Tabs():
|
| 331 |
+
with gr.TabItem("生成视频"):
|
| 332 |
+
with gr.Group(elem_classes=["out-card"]):
|
| 333 |
+
video_out = gr.Video(label="生成的视频", height=340)
|
| 334 |
+
with gr.Row():
|
| 335 |
+
frame_slider = gr.Slider(1, 21, value=1, step=1, label="多视角预览(拖动查看帧)")
|
| 336 |
+
frame_preview = gr.Image(type="filepath", label="预览帧", height=260)
|
| 337 |
+
frames_state = gr.State([])
|
| 338 |
+
|
| 339 |
+
with gr.TabItem("归一化对齐结果"):
|
| 340 |
+
with gr.Group(elem_classes=["out-card"]):
|
| 341 |
+
with gr.Row():
|
| 342 |
+
aligned_id_out = gr.Image(type="filepath", label="对齐后的身份图", height=240)
|
| 343 |
+
aligned_hair_out = gr.Image(type="filepath", label="对齐后的发型图", height=240)
|
| 344 |
+
|
| 345 |
+
with gr.TabItem("秃头化结果"):
|
| 346 |
+
with gr.Group(elem_classes=["out-card"]):
|
| 347 |
+
bald_id_out = gr.Image(type="filepath", label="秃头化后的身份图", height=260)
|
| 348 |
+
|
| 349 |
+
# 逻辑保持不变
|
| 350 |
+
run_btn.click(fn=inference,
|
| 351 |
+
inputs=[id_input, hair_input],
|
| 352 |
+
outputs=[aligned_id_out, aligned_hair_out, bald_id_out,
|
| 353 |
+
video_out, frames_state, frame_slider, frame_preview])
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
def _on_slide(frames, idx):
|
| 357 |
+
if not frames:
|
| 358 |
+
return gr.update()
|
| 359 |
+
i = int(idx) - 1
|
| 360 |
+
i = max(0, min(i, len(frames) - 1))
|
| 361 |
+
return gr.update(value=frames[i])
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
frame_slider.change(_on_slide, inputs=[frames_state, frame_slider], outputs=frame_preview)
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
def _clear():
|
| 368 |
+
return None, None, None, None, None
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
clear_btn.click(_clear, None,
|
| 372 |
+
[id_input, hair_input, aligned_id_out, aligned_hair_out, bald_id_out])
|
| 373 |
+
|
| 374 |
+
if __name__ == "__main__":
|
| 375 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
|