Files
long 3cd3553af2 feat: 新增 training-videos 素材流水线
AI 跟练的素材生产工具,产物写入 TUICallKit-Vue3/training:
- 教练动作帧:AI 设定稿 → 切帧对齐、锁脸、插帧 → 导出小程序帧与 COS 高清帧
- 示范视频:Remotion 渲染四个动作的示范视频
- 背景音乐:程序合成的原创循环音乐(npm run bgm)
2026-10-31 10:33:25 +08:00

66 lines
2.7 KiB
Python

"""
锁脸:同一个动作的所有帧都用第一帧(站姿)的脸
每一帧是 AI 单独画的,五官、发际线每帧都有细微差别,按十几帧每秒播放时脸会“一闪一闪”。
这里按模板匹配找到每帧的脸,把整帧水平平移让头对齐第一帧(头不再左右晃),
再把第一帧的脸(额头刘海到下巴)用羽化椭圆贴上去。头顶轮廓、马尾不动,避免盖住举过头的手臂。
"""
import cv2
import numpy as np
BG = np.array([244, 248, 246], np.float32)
def _on_bg(rgba: np.ndarray) -> np.ndarray:
f = rgba.astype(np.float32)
return f[..., :3] * (f[..., 3:4] / 255) + BG * (1 - f[..., 3:4] / 255)
def _face(kp: dict):
"""脸中心(两眼中点与鼻子之间)和眼距"""
ex = (kp["leye"][0] + kp["reye"][0]) / 2
ey = (kp["leye"][1] + kp["reye"][1]) / 2
return ex, ey + 0.6 * (kp["nose"][1] - ey), abs(kp["leye"][0] - kp["reye"][0])
def _shift_x(rgba: np.ndarray, dx: int) -> np.ndarray:
out = np.zeros_like(rgba)
if dx > 0:
out[:, dx:] = rgba[:, :-dx]
elif dx < 0:
out[:, :dx] = rgba[:, -dx:]
else:
out[:] = rgba
return out
def lock_faces(frames: list, kps: list):
"""frames:对齐后的主帧(RGBA);kps:对应关键点(画布坐标)。返回处理后的帧与平移后的关键点"""
ref = frames[0]
cx, cy, d = _face(kps[0])
h = int(round(2.2 * d)) # 模板半边长:盖住整个脸和刘海
x0, y0 = int(round(cx)) - h, int(round(cy)) - h
tpl = _on_bg(ref)[y0 : y0 + 2 * h, x0 : x0 + 2 * h]
src = ref[y0 : y0 + 2 * h, x0 : x0 + 2 * h].astype(np.float32)
# 羽化椭圆:上到刘海发际线、下到下巴,左右到脸颊
yy, xx = np.mgrid[0 : 2 * h, 0 : 2 * h].astype(np.float32)
ex, ey = cx - x0, cy - y0 - 0.3 * d
ell = (((xx - ex) / (1.35 * d)) ** 2 + ((yy - ey) / (1.75 * d)) ** 2 <= 1).astype(np.float32)
mask = cv2.GaussianBlur(ell, (0, 0), 0.2 * d)[..., None]
out_frames, out_kps = [], []
for f, kp in zip(frames, kps):
fx, fy, _ = _face(kp)
pad = 40
sx0, sy0 = int(round(fx)) - h - pad, int(round(fy)) - h - pad
win = _on_bg(f)[sy0 : sy0 + 2 * (h + pad), sx0 : sx0 + 2 * (h + pad)]
_, _, _, loc = cv2.minMaxLoc(-cv2.matchTemplate(win, tpl, cv2.TM_SQDIFF))
tx, ty = sx0 + loc[0], sy0 + loc[1]
dx = x0 - tx
g = _shift_x(f, dx).astype(np.float32)
region = g[ty : ty + 2 * h, x0 : x0 + 2 * h]
region[:] = src * mask + region * (1 - mask)
out_frames.append(np.clip(g, 0, 255).round().astype(np.uint8))
out_kps.append({k: (x + dx, y, s) for k, (x, y, s) in kp.items()})
return out_frames, out_kps