- 页面:pose-hub 选择动作,pose-coach 跟练(教练小甄示范 + 摄像头动作识别计数) - 动作:徒手深蹲、原地高抬腿、低冲击开合、站姿侧平举 - VisionKit 人体识别,按动作幅度计数、评分、纠错;没有摄像头或识别不可用时自动进入领练模式 - 教练逐帧动画:分包内置 600px 帧,COS 高清帧全部加载完后无缝切换 - 语音报数与口令(WechatSI),舒缓背景音乐(COS,教练说话时自动压低,可关闭) - 逻辑单测:npm run test:pose
109 lines
4.3 KiB
TypeScript
109 lines
4.3 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { crispBlend, framePair, framesToLoad, frameUrl, hdCropOf, hdFrameUrl, peakFrame, sequenceOf } from '../coachFrames.ts'
|
|
import { COACH_SEQUENCES } from '../coachSequences.ts'
|
|
import { MOTIONS, createRepEdge, type MoveId } from '../motions.ts'
|
|
|
|
const MOVES: MoveId[] = ['squat', 'march', 'jack', 'raise']
|
|
|
|
/** 按播放规则(换帧点之前显示前一帧)取当前显示的帧在序列中的位置 */
|
|
function shownIndex(m: MoveId, amount: number, side: 'L' | 'R') {
|
|
const p = framePair(m, amount, side)
|
|
return sequenceOf(m, side).indexOf(crispBlend(p.w) < 0.5 ? p.a : p.b)
|
|
}
|
|
|
|
test('每个序列至少 9 帧,幅度从 0 严格递增到 1', () => {
|
|
for (const [name, seq] of Object.entries(COACH_SEQUENCES)) {
|
|
assert.ok(seq.frames.length >= 9, name)
|
|
assert.equal(seq.amounts.length, seq.frames.length, name)
|
|
assert.equal(seq.amounts[0], 0, name)
|
|
assert.equal(seq.amounts[seq.amounts.length - 1], 1, name)
|
|
for (let i = 1; i < seq.amounts.length; i++) assert.ok(seq.amounts[i] > seq.amounts[i - 1], `${name} 第 ${i} 帧`)
|
|
}
|
|
})
|
|
|
|
test('左右交替的动作共用起始站姿,换边时不会跳', () => {
|
|
for (const m of ['march', 'jack'] as MoveId[]) assert.equal(sequenceOf(m, 'L')[0], sequenceOf(m, 'R')[0])
|
|
})
|
|
|
|
test('幅度 0 停在第一帧,幅度 1 停在最后一帧', () => {
|
|
for (const m of MOVES) {
|
|
for (const side of ['L', 'R'] as const) {
|
|
const frames = sequenceOf(m, side)
|
|
const p0 = framePair(m, 0, side)
|
|
assert.equal(p0.a, frames[0])
|
|
assert.ok(crispBlend(p0.w) < 1e-6)
|
|
const p1 = framePair(m, 1, side)
|
|
assert.equal(p1.b, frames[frames.length - 1])
|
|
assert.ok(Math.abs(crispBlend(p1.w) - 1) < 1e-6)
|
|
}
|
|
}
|
|
})
|
|
|
|
test('幅度连续变化时逐帧前进:不回退、不跳帧', () => {
|
|
for (const m of MOVES) {
|
|
for (const side of ['L', 'R'] as const) {
|
|
let prev = 0
|
|
for (let i = 0; i <= 1000; i++) {
|
|
const idx = shownIndex(m, i / 1000, side)
|
|
assert.ok(idx >= prev, `${m}/${side} 回退`)
|
|
assert.ok(idx - prev <= 1, `${m}/${side} 跳帧`)
|
|
prev = idx
|
|
}
|
|
assert.equal(prev, sequenceOf(m, side).length - 1)
|
|
}
|
|
}
|
|
})
|
|
|
|
test('示范节奏一个周期会经过该动作的全部帧', () => {
|
|
for (const m of MOVES) {
|
|
const seen = new Set<string>()
|
|
for (let i = 0; i < 2000; i++) {
|
|
const a = MOTIONS[m].amountAt(i / 2000)
|
|
const p = framePair(m, a.amount, a.side)
|
|
seen.add(crispBlend(p.w) < 0.5 ? p.a : p.b)
|
|
}
|
|
assert.deepEqual([...seen].sort(), framesToLoad(m).sort(), m)
|
|
}
|
|
})
|
|
|
|
test('大部分时间显示单张清晰帧,只在换帧附近过渡', () => {
|
|
let crisp = 0
|
|
for (let i = 0; i <= 100; i++) {
|
|
const w = crispBlend(i / 100)
|
|
if (w < 0.01 || w > 0.99) crisp += 1
|
|
}
|
|
assert.ok(crisp >= 85, `清晰帧占比 ${crisp}%`)
|
|
})
|
|
|
|
test('帧地址指向分包内置 PNG', () => {
|
|
assert.equal(frameUrl('sq_00'), '/training/static/coach/sq_00.png')
|
|
assert.ok(peakFrame('squat').startsWith('sq_'))
|
|
})
|
|
|
|
test('领练计数:教练每个动作周期恰好完成 repsPerCycle 次,且在回到起始附近时计数', () => {
|
|
for (const m of MOVES) {
|
|
const spec = MOTIONS[m]
|
|
const edge = createRepEdge()
|
|
let reps = 0
|
|
for (let i = 0; i <= 3000; i++) {
|
|
const a = spec.amountAt((i / 1000) * 1)
|
|
if (edge(a.amount)) {
|
|
reps += 1
|
|
assert.ok(a.amount <= 0.25, `${m} 计数时幅度 ${a.amount}`)
|
|
}
|
|
}
|
|
assert.equal(reps, spec.repsPerCycle * 3, m)
|
|
}
|
|
})
|
|
|
|
test('高清帧:地址带版本号,裁切框在画布内且包含该动作的人物区域', () => {
|
|
assert.match(hdFrameUrl('https://cdn.example.com/coach', 'sq_00'), /^https:\/\/cdn\.example\.com\/coach\/sq_00\.webp\?v=\w+$/)
|
|
for (const m of MOVES) {
|
|
const [x0, y0, x1, y1] = hdCropOf(m)
|
|
assert.ok(0 <= x0 && x0 < x1 && x1 <= 1 && 0 <= y0 && y0 < y1 && y1 <= 1, m)
|
|
// 人物站在画布中下部:裁切框至少覆盖头顶(约 23%)到脚底(95%)
|
|
assert.ok(y0 <= 0.23 && y1 >= 0.95, `${m} ${y0}~${y1}`)
|
|
}
|
|
})
|