feat(接口2): 新增生发后图片(ComfyUI/Flux inpaint)
在发际线预览基础上,每种发际线再出一张「植发3个月」生发图: - hairline/mask.py: headmark 5步法遮罩(额头上部区域∩SegFormer头部=ROI, 取发际线曲线以上闭合区域);用 hairline_texture_black 渲染黑线替代手绘检测; compose_comfy_rgba 合成 RGBA(alpha=255-mask, 透明=重绘区, 对齐 ComfyUI mask=1-alpha) - hairline/comfyui.py: ComfyUI 客户端(默认8182),/upload/image+/prompt(改节点26+随机seed) +轮询/history+/view 取回生发图 - hairline/render.py: 抽出 build_overlay_layer 供遮罩取曲线像素 - hairline/service.py: extract_context 一次出 landmarks/parse_map/502点; generate_grow_results 每种=预览+生发图(同步串行N张,单张ComfyUI失败则grown置空不拖垮整请求) - app.py: /hair/grow 返回 results[].grown_image_base64;重活放线程池避免卡事件循环 - add_hair.json 工作流 + hairline_texture_black/ 黑贴图入库 - 测试: test_mask.py(遮罩几何) + test_api mock ComfyUI 验 grown 字段,35 全绿 实测(5090): female 5张生发图同步约18s;预览/生发图人物五官服饰背景保持、黑线已清除。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+18
-12
@@ -78,19 +78,30 @@ def render_hairline_overlay(photo_bgr: np.ndarray,
|
||||
points502_norm: (502, 3) 归一化坐标(x,y ∈ [0,1]),**MP 顺序**(extract_hairline 输出)。
|
||||
"""
|
||||
H, W = photo_bgr.shape[:2]
|
||||
TH, TW = texture_rgba.shape[:2]
|
||||
pts_obj = mp_order_to_obj_order(points502_norm) # MP序 → OBJ序
|
||||
img_xy = pts_obj[:, :2] * np.array([W, H], dtype=np.float32) # (502,2)
|
||||
overlay = build_overlay_layer(H, W, points502_norm, ext_faces, uv502, texture_rgba)
|
||||
# alpha 合成(RGBA→BGR:贴图 RGB 顺序需反成 BGR)
|
||||
a = overlay[:, :, 3:4] / 255.0
|
||||
rgb = overlay[:, :, :3][..., ::-1] # RGB→BGR
|
||||
out = photo_bgr.astype(np.float32) * (1.0 - a) + rgb * a
|
||||
return np.clip(out, 0, 255).astype(np.uint8)
|
||||
|
||||
overlay = np.zeros((H, W, 4), np.float32) # 累积曲线层 RGBA
|
||||
|
||||
def build_overlay_layer(H, W, points502_norm, ext_faces, uv502, texture_rgba) -> np.ndarray:
|
||||
"""渲染发际线曲线层,返回 (H, W, 4) float32 RGBA(未合成到照片)。
|
||||
|
||||
供渲染合成(render_hairline_overlay)与遮罩(mask.py 取 alpha=曲线像素)共用。
|
||||
"""
|
||||
TH, TW = texture_rgba.shape[:2]
|
||||
pts_obj = mp_order_to_obj_order(points502_norm)
|
||||
img_xy = pts_obj[:, :2] * np.array([W, H], dtype=np.float32)
|
||||
overlay = np.zeros((H, W, 4), np.float32)
|
||||
tex = texture_rgba.astype(np.float32)
|
||||
for (i, j, k) in ext_faces:
|
||||
dst = img_xy[[i, j, k]].astype(np.float32)
|
||||
# UV → 贴图像素;flipY:贴图 y = (1 - v_raw) * TH(与 head3d Three.js flipY=true 一致)
|
||||
src = np.array([[uv502[v][0] * TW, (1.0 - uv502[v][1]) * TH] for v in (i, j, k)],
|
||||
dtype=np.float32)
|
||||
# 退化三角形(投影到一条线)跳过,避免 getAffineTransform 奇异
|
||||
if cv2.contourArea(dst.astype(np.int32)) < 1.0:
|
||||
if cv2.contourArea(dst.astype(np.int32)) < 1.0: # 退化三角形跳过
|
||||
continue
|
||||
M = cv2.getAffineTransform(src, dst)
|
||||
warped = cv2.warpAffine(tex, M, (W, H), flags=cv2.INTER_LINEAR,
|
||||
@@ -99,9 +110,4 @@ def render_hairline_overlay(photo_bgr: np.ndarray,
|
||||
cv2.fillConvexPoly(tri_mask, dst.astype(np.int32), 255)
|
||||
sel = tri_mask > 0
|
||||
overlay[sel] = warped[sel]
|
||||
|
||||
# alpha 合成(RGBA→BGR:贴图 RGB 顺序需反成 BGR)
|
||||
a = overlay[:, :, 3:4] / 255.0
|
||||
rgb = overlay[:, :, :3][..., ::-1] # RGB→BGR
|
||||
out = photo_bgr.astype(np.float32) * (1.0 - a) + rgb * a
|
||||
return np.clip(out, 0, 255).astype(np.uint8)
|
||||
return overlay
|
||||
|
||||
Reference in New Issue
Block a user