/** * 关键点转换与平滑:真机上画面外的点坐标是 NaN,不能让它污染后续帧。 * 运行:node --test training/pose/__tests__/ */ import { test } from 'node:test' import assert from 'node:assert/strict' import { anchorToPose, checkFraming, smoothPose, KP, type Pose } from '../keypoints.ts' import { createCounter } from '../counters.ts' import { MOTIONS } from '../motions.ts' import { sampleMotion } from './sampleMotion.ts' const LEG = [KP.L_KNEE, KP.R_KNEE, KP.L_ANKLE, KP.R_ANKLE] /** 示范姿态转成 VisionKit 的 anchor,指定的点坐标为 NaN(画面外) */ function anchorOf(pose: Pose, nanIdx: number[] = []) { return { points: pose.points.map((p, i) => (nanIdx.includes(i) ? { x: NaN, y: NaN } : { x: p.x, y: p.y })), confidence: pose.scores.map((s, i) => (nanIdx.includes(i) ? 0.9 : s)), score: 0.9, } } test('anchorToPose:坐标为 NaN 的点视为看不见', () => { const [f] = sampleMotion('squat', 0.1) const p = anchorToPose(anchorOf(f, LEG), 480, 853, 0)! for (const i of LEG) { assert.equal(p.scores[i], 0, `score ${i}`) assert.ok(Number.isFinite(p.points[i].x) && Number.isFinite(p.points[i].y), `point ${i}`) } }) test('smoothPose:上一帧是 NaN 时直接采用新坐标,不会一直是 NaN', () => { const [f] = sampleMotion('squat', 0.1) const poisoned: Pose = { ...f, points: f.points.map((p, i) => (LEG.includes(i) ? { x: NaN, y: NaN } : p)) } const next = smoothPose(poisoned, f) for (const i of LEG) assert.ok(Number.isFinite(next.points[i].y), `point ${i}`) }) test('站位:脚踝坐标是 NaN 时不能判定为站位良好', () => { const [f] = sampleMotion('squat', 0.1) const p: Pose = { ...f, points: f.points.map((q, i) => (i === KP.L_ANKLE || i === KP.R_ANKLE ? { x: NaN, y: NaN } : q)) } assert.notEqual(checkFraming(p), 'none') }) test('真机复现:开始时腿在画面外(NaN),退回画面后深蹲照常计数', () => { const cycles = 4 const frames = sampleMotion('squat', MOTIONS.squat.cycleSeconds * cycles + 0.3) const counter = createCounter('squat') let smoothed: Pose | null = null // 前 1 秒:离得太近,膝和踝在画面外 for (let i = 0; i < 12; i++) { const raw = anchorToPose(anchorOf(frames[0], LEG), 480, 853, i * 83)! smoothed = smoothPose(smoothed, raw) } for (const f of frames) { smoothed = smoothPose(smoothed, anchorToPose(anchorOf(f), 480, 853, f.t)!) counter.update(smoothed) } assert.equal(counter.count, cycles) })