- 删除从未显示的 idle 手势帧(小程序包内 png 和视频用高清帧),训练分包减少 17KB - 删除骨架连线 BONES、镜像函数 mirrorPose(骨架已去掉)和未使用的 Bgm 类型 - 只给单测用的 sampleMotion 移到测试目录 - 删除训练菜单里未使用的 .menu-icon-text 样式 - 补全 history 单测数据缺少的 adviceKind 字段(严格类型检查报错)
194 lines
9.1 KiB
Python
194 lines
9.1 KiB
Python
"""
|
||
教练帧导出:对齐后的主帧 → 各端使用的尺寸 + 帧清单
|
||
|
||
用法:python3 coach_export.py (读取 coach_frames.py 的产物 work/frames/)
|
||
python3 coach_export.py --from-hd (没有 work/ 时,用仓库里的 1000px 高清帧重新导出小程序帧)
|
||
导出:
|
||
TUICallKit-Vue3/training/static/coach/*.png 600px 调色板 PNG(轻微锐化),内置进小程序分包(离线兜底)
|
||
training-videos/out/coach/*.webp 高清帧:1000px 画布,每个动作裁出人物所在区域,上传到 COS
|
||
training-videos/public/coach/hd/*.webp 1000px 整张画布,Remotion 示范视频使用
|
||
TUICallKit-Vue3/training/pose/coachSequences.ts 帧清单、每帧的动作幅度、高清帧裁切框与版本号(自动生成,勿手改)
|
||
|
||
手势帧、错误示范帧(STATIC_FRAMES)不随动作帧重新生成,直接用高清帧目录里已有的版本;
|
||
错误示范帧只有示范视频用,不打进小程序包(分包有 2MB 上限)。
|
||
"""
|
||
import hashlib
|
||
import json
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
from PIL import Image, ImageFilter
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
WORK = ROOT / "work" / "frames"
|
||
MINI_DIR = ROOT.parent / "TUICallKit-Vue3" / "training" / "static" / "coach"
|
||
HD_DIR = ROOT / "public" / "coach" / "hd"
|
||
TS_OUT = ROOT.parent / "TUICallKit-Vue3" / "training" / "pose" / "coachSequences.ts"
|
||
CDN_DIR = ROOT / "out" / "coach"
|
||
MINI_SIZE = 600
|
||
CDN_MARGIN = 12
|
||
# 每个动作的帧名前缀:同一个动作的所有帧共用一个裁切框,帧与帧之间位置不变
|
||
MOVE_PREFIX = {"squat": ("sq_",), "raise": ("ra_",), "jack": ("jl_", "jr_"), "march": ("ml_", "mr_")}
|
||
|
||
STATIC_FRAMES = ["wave", "cheer", "thumbs", "wrong_squat", "wrong_march", "wrong_jack", "wrong_raise"]
|
||
MINI_STATIC = ["wave", "cheer", "thumbs"]
|
||
SEQUENCE_ORDER = ["squat", "raise", "jack_l", "jack_r", "march_l", "march_r"]
|
||
|
||
|
||
def resize_rgba(im: Image.Image, size: int) -> Image.Image:
|
||
"""在预乘 Alpha 空间缩放:直接缩放 RGBA 会把透明像素的黑色混进边缘,形成黑边"""
|
||
return im.convert("RGBa").resize((size, size), Image.Resampling.LANCZOS).convert("RGBA")
|
||
|
||
|
||
def sharpen(im: Image.Image) -> Image.Image:
|
||
"""手机上帧会被放大 2~3 倍显示:导出时轻微锐化,放大后不发虚(在预乘空间做,边缘不起白边)"""
|
||
r, g, b, a = im.convert("RGBa").split()
|
||
rgb = Image.merge("RGB", (r, g, b)).filter(ImageFilter.UnsharpMask(radius=1.2, percent=45, threshold=2))
|
||
return Image.merge("RGBa", (*rgb.split(), a)).convert("RGBA")
|
||
|
||
|
||
def write_mini(im: Image.Image, name: str):
|
||
small = sharpen(resize_rgba(im, MINI_SIZE))
|
||
small.quantize(colors=256, method=Image.Quantize.FASTOCTREE, dither=Image.Dither.FLOYDSTEINBERG).save(
|
||
MINI_DIR / f"{name}.png", optimize=True
|
||
)
|
||
|
||
|
||
def write_hd(im: Image.Image, name: str):
|
||
tmp = HD_DIR / f"{name}.tmp.png"
|
||
im.save(tmp)
|
||
subprocess.run(["cwebp", "-quiet", "-q", "90", "-alpha_q", "95", "-m", "6", str(tmp), "-o", str(HD_DIR / f"{name}.webp")], check=True)
|
||
tmp.unlink()
|
||
|
||
|
||
def union_crop(images: list) -> tuple:
|
||
"""同一个动作所有帧的人物区域并集,外扩一点边距"""
|
||
box = None
|
||
for im in images:
|
||
b = im.getchannel("A").point(lambda v: 255 if v > 8 else 0).getbbox()
|
||
box = b if box is None else (min(box[0], b[0]), min(box[1], b[1]), max(box[2], b[2]), max(box[3], b[3]))
|
||
w, h = images[0].size
|
||
return max(0, box[0] - CDN_MARGIN), max(0, box[1] - CDN_MARGIN), min(w, box[2] + CDN_MARGIN), min(h, box[3] + CDN_MARGIN)
|
||
|
||
|
||
def write_cdn(motion: list) -> dict:
|
||
"""高清帧:只裁出人物区域(整张 1000px 画布解码后 4MB/帧,一个动作几十帧手机吃不消),WebP"""
|
||
CDN_DIR.mkdir(parents=True, exist_ok=True)
|
||
for f in CDN_DIR.glob("*.webp"):
|
||
f.unlink()
|
||
crops = {}
|
||
for move, prefixes in MOVE_PREFIX.items():
|
||
names = [n for n in motion if n.startswith(prefixes)]
|
||
images = {n: Image.open(WORK / "master" / f"{n}.png").convert("RGBA") for n in names}
|
||
box = union_crop(list(images.values()))
|
||
size = next(iter(images.values())).size
|
||
crops[move] = [round(box[0] / size[0], 4), round(box[1] / size[1], 4), round(box[2] / size[0], 4), round(box[3] / size[1], 4)]
|
||
for n, im in images.items():
|
||
tmp = CDN_DIR / f"{n}.tmp.png"
|
||
im.crop(box).save(tmp)
|
||
subprocess.run(
|
||
["cwebp", "-quiet", "-q", "88", "-alpha_q", "92", "-m", "6", "-sharp_yuv", str(tmp), "-o", str(CDN_DIR / f"{n}.webp")],
|
||
check=True,
|
||
)
|
||
tmp.unlink()
|
||
digest = hashlib.sha1()
|
||
for f in sorted(CDN_DIR.glob("*.webp")):
|
||
digest.update(f.read_bytes())
|
||
return {"version": digest.hexdigest()[:10], "crops": crops}
|
||
|
||
|
||
def write_manifest(sequences: dict, hd: dict):
|
||
lines = [
|
||
"/**",
|
||
" * 教练动作帧清单(由 training-videos/scripts/coach_export.py 自动生成,勿手改)",
|
||
" *",
|
||
" * 每个序列按动作幅度 0→1 排列,全部是 AI 原图帧(内置在 training/static/coach),",
|
||
" * 同一个动作出自同一张设定稿;右侧动作由左侧镜像换边生成。amounts 是每帧实测的动作幅度。",
|
||
" */",
|
||
"export type CoachSequence = {",
|
||
" /** 帧名,按动作幅度 0→1 排列 */",
|
||
" frames: string[]",
|
||
" /** 每帧的动作幅度(0 = 起始姿势,1 = 动作到位),与 frames 一一对应、严格递增 */",
|
||
" amounts: number[]",
|
||
"}",
|
||
"",
|
||
"export const COACH_SEQUENCES: Record<string, CoachSequence> = {",
|
||
]
|
||
for name in SEQUENCE_ORDER:
|
||
seq = sequences[name]
|
||
lines.append(f" {name}: {{")
|
||
lines.append(f" frames: [{', '.join(repr(f) for f in seq['frames'])}],")
|
||
lines.append(f" amounts: [{', '.join(format(a, 'g') for a in seq['amounts'])}],")
|
||
lines.append(" },")
|
||
lines.append("}")
|
||
lines += [
|
||
"",
|
||
"/**",
|
||
" * 高清帧(training-videos/out/coach 上传到 COS):每个动作裁出人物所在区域,",
|
||
" * crops 是裁切框在整张画布上的比例 [左, 上, 右, 下];version 随内容变化,用来刷新 CDN 缓存。",
|
||
" */",
|
||
"export const COACH_HD: { version: string; crops: Record<string, [number, number, number, number]> } = {",
|
||
f" version: '{hd['version']}',",
|
||
" crops: {",
|
||
]
|
||
for move, c in hd["crops"].items():
|
||
lines.append(f" {move}: [{', '.join(format(v, 'g') for v in c)}],")
|
||
lines += [" },", "}"]
|
||
TS_OUT.write_text("\n".join(lines) + "\n")
|
||
|
||
|
||
def check(sequences: dict):
|
||
for name in SEQUENCE_ORDER:
|
||
seq = sequences.get(name)
|
||
if not seq:
|
||
raise SystemExit(f"缺少序列 {name},先运行 coach_frames.py")
|
||
a = seq["amounts"]
|
||
if len(a) != len(seq["frames"]) or a[0] != 0 or a[-1] != 1 or any(y <= x for x, y in zip(a, a[1:])):
|
||
raise SystemExit(f"序列 {name} 的幅度必须从 0 严格递增到 1:{a}")
|
||
|
||
|
||
def export_all():
|
||
sequences = json.loads((WORK / "sequences.json").read_text())
|
||
check(sequences)
|
||
motion = sorted({f for s in sequences.values() for f in s["frames"]})
|
||
missing_static = [n for n in STATIC_FRAMES if not (HD_DIR / f"{n}.webp").exists()]
|
||
if missing_static:
|
||
raise SystemExit(f"高清帧目录缺少手势 / 错误示范帧:{', '.join(missing_static)}")
|
||
MINI_DIR.mkdir(parents=True, exist_ok=True)
|
||
HD_DIR.mkdir(parents=True, exist_ok=True)
|
||
# 清掉旧版本留下、已不再使用的帧
|
||
for d, ext, keep in ((MINI_DIR, "png", set(motion) | set(MINI_STATIC)), (HD_DIR, "webp", set(motion) | set(STATIC_FRAMES))):
|
||
for f in d.glob(f"*.{ext}"):
|
||
if f.stem not in keep:
|
||
f.unlink()
|
||
for name in motion:
|
||
im = Image.open(WORK / "master" / f"{name}.png").convert("RGBA")
|
||
write_mini(im, name)
|
||
write_hd(im, name)
|
||
for name in MINI_STATIC:
|
||
write_mini(Image.open(HD_DIR / f"{name}.webp").convert("RGBA"), name)
|
||
hd = write_cdn(motion)
|
||
write_manifest(sequences, hd)
|
||
size = sum(f.stat().st_size for f in MINI_DIR.glob("*.png"))
|
||
cdn = sum(f.stat().st_size for f in CDN_DIR.glob("*.webp"))
|
||
print(f"已导出 {len(motion)} 个动作帧 + {len(MINI_STATIC)} 个手势帧,小程序帧共 {size / 1024:.0f}KB")
|
||
print(f"高清帧 {len(motion)} 张共 {cdn / 1024:.0f}KB → {CDN_DIR.relative_to(ROOT)}(上传到 COS),版本 {hd['version']};已更新 coachSequences.ts")
|
||
|
||
|
||
def export_from_hd():
|
||
MINI_DIR.mkdir(parents=True, exist_ok=True)
|
||
names = sorted(f.stem for f in HD_DIR.glob("*.webp") if not f.stem.startswith("wrong_"))
|
||
listed = set(re.findall(r"'([a-z_0-9]+)'", TS_OUT.read_text())) | set(MINI_STATIC)
|
||
missing = sorted(listed - set(names))
|
||
if missing:
|
||
raise SystemExit(f"高清帧目录缺少:{', '.join(missing)}")
|
||
for name in names:
|
||
write_mini(Image.open(HD_DIR / f"{name}.webp").convert("RGBA"), name)
|
||
print(f"已从高清帧重新导出 {len(names)} 个小程序帧")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
export_from_hd() if "--from-hd" in sys.argv else export_all()
|