AI 跟练的素材生产工具,产物写入 TUICallKit-Vue3/training: - 教练动作帧:AI 设定稿 → 切帧对齐、锁脸、插帧 → 导出小程序帧与 COS 高清帧 - 示范视频:Remotion 渲染四个动作的示范视频 - 背景音乐:程序合成的原创循环音乐(npm run bgm)
79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
"""
|
||
镜像换边:左侧动作帧水平翻转得到右侧动作帧,头部(脸 + 马尾)保留原图方向。
|
||
|
||
左右两侧出自同一张设定稿(同一次渲染),换边时脸、衣服都不会“闪”;
|
||
只翻转身体,所以马尾始终在同一侧,不会左右跳。
|
||
"""
|
||
import cv2
|
||
import numpy as np
|
||
|
||
ALPHA_ON = 40
|
||
|
||
|
||
def _premul(rgba):
|
||
f = rgba.astype(np.float32) / 255
|
||
f[..., :3] *= f[..., 3:4]
|
||
return f
|
||
|
||
|
||
def _unpremul(f):
|
||
a = np.maximum(f[..., 3:4], 1e-6)
|
||
out = f.copy()
|
||
out[..., :3] = np.where(f[..., 3:4] > 0, f[..., :3] / a, 0)
|
||
return (np.clip(out, 0, 1) * 255).round().astype(np.uint8)
|
||
|
||
|
||
def _mirror(arr, xc):
|
||
"""以 x = xc 为轴水平翻转(整像素平移,不重采样)"""
|
||
w = arr.shape[1]
|
||
flipped = arr[:, ::-1]
|
||
shift = int(round(2 * xc - (w - 1)))
|
||
out = np.zeros_like(arr)
|
||
if shift >= 0:
|
||
out[:, shift:] = flipped[:, : w - shift]
|
||
else:
|
||
out[:, : w + shift] = flipped[:, -shift:]
|
||
return out
|
||
|
||
|
||
def _head_mask(rgba, kp, y_cut):
|
||
"""肩线以上、包含鼻子的连通区域 = 头 + 脖子 + 马尾上段"""
|
||
on = rgba[..., 3] > ALPHA_ON
|
||
on[int(y_cut) :] = False
|
||
_, lab = cv2.connectedComponents(on.astype(np.uint8), connectivity=8)
|
||
k = lab[int(round(kp["nose"][1])), int(round(kp["nose"][0]))]
|
||
if k == 0:
|
||
raise ValueError("鼻子不在人物区域内")
|
||
return lab == k
|
||
|
||
|
||
def _hair_mask(rgba, head, sh_y):
|
||
"""与头部相连的深色像素 = 头发 + 整条马尾(马尾会垂到肩线以下)"""
|
||
rgb = rgba[..., :3].astype(np.float32)
|
||
lum = 0.3 * rgb[..., 0] + 0.59 * rgb[..., 1] + 0.11 * rgb[..., 2]
|
||
dark = (lum < 72) & (rgba[..., 3] > ALPHA_ON)
|
||
dark[int(sh_y + 60) :] = False
|
||
_, lab = cv2.connectedComponents(dark.astype(np.uint8), connectivity=8)
|
||
ids = np.unique(lab[head & dark])
|
||
return np.isin(lab, ids[ids > 0])
|
||
|
||
|
||
def mirror_frame(rgba: np.ndarray, kp: dict) -> np.ndarray:
|
||
"""rgba:对齐后的主帧;kp:该帧关键点(需要 nose / lsh / rsh)"""
|
||
xc = (kp["lsh"][0] + kp["rsh"][0]) / 2
|
||
sh_y = min(kp["lsh"][1], kp["rsh"][1])
|
||
y_cut = sh_y - 14
|
||
head = _head_mask(rgba, kp, y_cut)
|
||
hair = _hair_mask(rgba, head, sh_y)
|
||
# 头部(肩线以上)用原图,靠近肩线处渐变到翻转后的身体
|
||
region = cv2.dilate((head | _mirror(head, xc)).astype(np.uint8), np.ones((9, 9), np.uint8)) > 0
|
||
region[int(y_cut) :] = False
|
||
ramp = np.clip((y_cut - np.arange(rgba.shape[0], dtype=np.float32)) / 16, 0, 1)[:, None]
|
||
w_head = cv2.GaussianBlur(region.astype(np.float32), (0, 0), 2.5) * ramp
|
||
# 原图的马尾保留;翻转过来的马尾(另一侧肩上)连同半透明发梢一起用原图同位置的内容盖掉
|
||
own = cv2.dilate(hair.astype(np.uint8), np.ones((7, 7), np.uint8))
|
||
other = cv2.dilate(_mirror(hair, xc).astype(np.uint8), np.ones((17, 17), np.uint8))
|
||
w_hair = cv2.GaussianBlur(np.maximum(own, other).astype(np.float32), (0, 0), 2)
|
||
w = np.maximum(w_head, w_hair)[..., None]
|
||
return _unpremul(_premul(_mirror(rgba, xc)) * (1 - w) + _premul(rgba) * w)
|