diff --git a/TUICallKit-Vue3/package.json b/TUICallKit-Vue3/package.json index 9846baa6b..4b7d31fd3 100644 --- a/TUICallKit-Vue3/package.json +++ b/TUICallKit-Vue3/package.json @@ -9,7 +9,8 @@ "dev:app": "UNI_INPUT_DIR=\"$PWD\" uni -p app", "build:mp-weixin": "UNI_INPUT_DIR=\"$PWD\" uni build -p mp-weixin", "build:h5": "UNI_INPUT_DIR=\"$PWD\" uni build", - "build:app": "UNI_INPUT_DIR=\"$PWD\" uni build -p app" + "build:app": "UNI_INPUT_DIR=\"$PWD\" uni build -p app", + "test:pose": "node --no-warnings --test \"training/pose/__tests__/*.test.ts\"" }, "keywords": [], "author": "", diff --git a/TUICallKit-Vue3/pages.json b/TUICallKit-Vue3/pages.json index 223d8b39f..ffb0bb303 100644 --- a/TUICallKit-Vue3/pages.json +++ b/TUICallKit-Vue3/pages.json @@ -162,6 +162,23 @@ "navigationBarTextStyle": "black", "backgroundColor": "#f8fafc" } + }, + { + "path": "pages/pose-hub", + "style": { + "navigationBarTitleText": "AI 跟练", + "navigationBarBackgroundColor": "#f4fbf4", + "navigationBarTextStyle": "black", + "backgroundColor": "#f4fbf4" + } + }, + { + "path": "pages/pose-coach", + "style": { + "navigationStyle": "custom", + "disableScroll": true, + "backgroundColor": "#0b1210" + } } ] }, diff --git a/TUICallKit-Vue3/training/pages/components/amount-meter.vue b/TUICallKit-Vue3/training/pages/components/amount-meter.vue new file mode 100644 index 000000000..72e6cb738 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/amount-meter.vue @@ -0,0 +1,123 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/components/coach-sprite.vue b/TUICallKit-Vue3/training/pages/components/coach-sprite.vue new file mode 100644 index 000000000..9b1a1b4cb --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/coach-sprite.vue @@ -0,0 +1,229 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/components/pose-coach.scss b/TUICallKit-Vue3/training/pages/components/pose-coach.scss new file mode 100644 index 000000000..3d7779162 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/pose-coach.scss @@ -0,0 +1,748 @@ +// AI 跟练页(跟练阶段)样式 +// +// 训练中以教练示范为主画面(亮色舞台),自己的摄像头画面缩在右上角小窗; +// 站位、离开画面或手动切换时自己的画面铺满,界面换成深色半透明面板(.is-dark)。 +// 层级:舞台 0 < 全屏画面 1 < 渐变遮罩 2 < 小窗 5 < 界面 10 < 暂停弹层 20 + +$glass: rgba(15, 23, 20, 0.55); +$glass-strong: rgba(15, 23, 20, 0.72); +$mint: #34d399; +$ink: #161d19; +$ink-soft: #6b7a72; + +.page { + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: #0b1210; + overflow: hidden; +} + +.cam-stage { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; +} + +// ───────── 教练舞台 ───────── +.stage { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + z-index: 0; + background-color: #f4f8f6; +} + +.coach-box { + position: absolute; +} + +.coach-box.is-stage { + z-index: 0; +} + +// 看自己时,教练缩到右上角 +.coach-box.is-card { + right: 24rpx; + z-index: 5; + width: 216rpx; + height: 288rpx; + border-radius: 28rpx; + border: 4rpx solid #fff; + background: linear-gradient(180deg, #ffffff, #ecfdf5); + box-shadow: 0 16rpx 40rpx rgba(0, 0, 0, 0.3); + overflow: hidden; +} + +.shade-top, +.shade-bottom { + position: absolute; + left: 0; + right: 0; + z-index: 2; + pointer-events: none; +} + +.shade-top { + top: 0; + height: 260rpx; + background: linear-gradient(180deg, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0)); +} + +.shade-bottom { + bottom: 0; + height: 460rpx; + background: linear-gradient(0deg, rgba(0, 0, 0, 0.65), rgba(0, 0, 0, 0)); +} + +// ───────── 介绍页返回 ───────── +.intro-back { + position: fixed; + left: 24rpx; + z-index: 20; + width: 64rpx; + height: 64rpx; + border-radius: 50%; + background: rgba(255, 255, 255, 0.85); + box-shadow: 0 4rpx 16rpx rgba(22, 29, 25, 0.1); + display: flex; + align-items: center; + justify-content: center; +} + +.intro-back__arrow { + width: 18rpx; + height: 18rpx; + margin-left: 6rpx; + border-left: 4rpx solid $ink; + border-bottom: 4rpx solid $ink; + transform: rotate(45deg); +} + +// ───────── 顶栏 ───────── +.topbar { + position: absolute; + left: 0; + right: 0; + top: 0; + z-index: 10; + box-sizing: content-box; + display: flex; + align-items: center; + justify-content: space-between; + padding-left: 24rpx; + padding-right: 210rpx; // 避开胶囊按钮 +} + +.icon-btn { + position: relative; + width: 64rpx; + height: 64rpx; + border-radius: 50%; + display: flex; + align-items: center; + justify-content: center; + + &.is-off { + opacity: 0.55; + } +} + +// 右侧按钮组:背景音乐 ♪ + 教练语音 +.topbar__actions { + display: flex; + align-items: center; + + .icon-btn + .icon-btn { + margin-left: 16rpx; + } +} + +.icon-btn__note { + font-size: 34rpx; + line-height: 1; +} + +// 背景音乐关闭:♪ 上加一道斜线 +.icon-btn--music.is-off::after { + content: ''; + position: absolute; + left: 14rpx; + right: 14rpx; + top: 50%; + height: 4rpx; + margin-top: -2rpx; + border-radius: 2rpx; + transform: rotate(-45deg); +} + +.icon-btn__x { + font-size: 44rpx; + line-height: 1; + margin-top: -4rpx; +} + +.icon-btn__label { + font-size: 24rpx; + font-weight: 700; +} + +.topbar__center { + flex: 1; + display: flex; + flex-direction: column; + align-items: center; +} + +.topbar__title { + font-size: 28rpx; + font-weight: 700; +} + +.topbar__time { + font-size: 24rpx; + font-variant-numeric: tabular-nums; +} + +.is-light { + .icon-btn { + background: rgba(255, 255, 255, 0.85); + box-shadow: 0 4rpx 16rpx rgba(22, 29, 25, 0.08); + } + .icon-btn__x, + .icon-btn__label, + .icon-btn__note, + .topbar__title { + color: $ink; + } + .icon-btn--music.is-off::after { + background: $ink; + } + .topbar__time { + color: $ink-soft; + } +} + +.is-dark { + .icon-btn { + background: $glass; + } + .icon-btn__x, + .icon-btn__label, + .icon-btn__note, + .topbar__title { + color: #fff; + } + .icon-btn--music.is-off::after { + background: #fff; + } + .topbar__time { + color: rgba(255, 255, 255, 0.8); + } +} + +// ───────── 计数环 + 连击 ───────── +.ring-slot { + position: absolute; + left: 28rpx; + z-index: 10; + display: flex; + flex-direction: column; + align-items: center; +} + +.combo { + margin-top: 16rpx; + display: flex; + align-items: baseline; + gap: 6rpx; + padding: 8rpx 22rpx; + border-radius: 999rpx; + background: linear-gradient(135deg, #f59e0b, #ef4444); + box-shadow: 0 8rpx 24rpx rgba(239, 68, 68, 0.35); + animation: combo-in 0.45s cubic-bezier(0.34, 1.56, 0.64, 1); +} + +.combo__n { + color: #fff; + font-size: 36rpx; + font-weight: 900; + font-style: italic; +} + +.combo__t { + color: #fff; + font-size: 22rpx; + font-weight: 700; +} + +@keyframes combo-in { + 0% { + transform: scale(0.4); + opacity: 0; + } + 100% { + transform: scale(1); + opacity: 1; + } +} + +// ───────── 教练模式切换 ───────── +.mode-switch { + position: absolute; + right: 24rpx; + z-index: 10; + width: 216rpx; + box-sizing: border-box; + display: flex; + padding: 4rpx; + border-radius: 999rpx; + + text { + flex: 1; + text-align: center; + font-size: 22rpx; + padding: 6rpx 0; + border-radius: 999rpx; + } + + .is-on { + font-weight: 700; + } +} + +.is-light .mode-switch { + background: rgba(22, 29, 25, 0.06); + + text { + color: $ink-soft; + } + .is-on { + background: #fff; + color: #006c49; + box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08); + } +} + +.is-dark .mode-switch { + background: $glass; + + text { + color: rgba(255, 255, 255, 0.75); + } + .is-on { + background: #fff; + color: #006c49; + } +} + +// ───────── 中央:倒计时 / 离开画面 ───────── +.center { + position: absolute; + left: 0; + right: 0; + top: 30%; + z-index: 10; + display: flex; + flex-direction: column; + align-items: center; + pointer-events: none; +} + +// 倒计时时给画面罩一层,数字不和教练重叠得看不清 +.scrim { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + z-index: 9; +} + +.is-light .scrim { + background: rgba(244, 248, 246, 0.72); +} + +.is-dark .scrim { + background: rgba(0, 0, 0, 0.35); +} + +.countdown { + display: flex; + flex-direction: column; + align-items: center; +} + +.countdown__n { + font-size: 280rpx; + line-height: 1; + font-weight: 900; + animation: count-pop 0.9s ease-out; +} + +.countdown__t { + margin-top: 12rpx; + font-size: 34rpx; + font-weight: 700; + letter-spacing: 4rpx; +} + +.is-light { + .countdown__n { + color: var(--accent); + } + .countdown__t { + color: $ink; + } +} + +.is-dark { + .countdown__n { + color: #fff; + text-shadow: 0 8rpx 40rpx rgba(0, 0, 0, 0.45); + } + .countdown__t { + color: #fff; + } +} + +@keyframes count-pop { + 0% { + transform: scale(1.8); + opacity: 0; + } + 30% { + transform: scale(1); + opacity: 1; + } + 100% { + transform: scale(0.94); + } +} + +.lost { + display: flex; + flex-direction: column; + align-items: center; + padding: 32rpx 56rpx; + border-radius: 36rpx; + background: $glass-strong; +} + +.lost__title { + color: #fff; + font-size: 36rpx; + font-weight: 800; +} + +.lost__sub { + margin-top: 8rpx; + color: rgba(255, 255, 255, 0.8); + font-size: 26rpx; +} + +// ───────── 评分弹出 ───────── +.pop { + position: absolute; + left: 0; + right: 0; + z-index: 10; + display: flex; + justify-content: center; + align-items: baseline; + gap: 12rpx; + pointer-events: none; + animation: pop-up 1.1s ease-out forwards; +} + +.pop__text { + font-size: 72rpx; + font-weight: 900; + font-style: italic; +} + +.pop__score { + font-size: 40rpx; + font-weight: 800; + color: #10b981; +} + +.is-light { + .pop__text { + color: var(--accent); + text-shadow: 0 4rpx 18rpx rgba(255, 255, 255, 0.9); + } + .pop--perfect .pop__text { + color: #f59e0b; + } +} + +.is-dark { + .pop__text { + color: #fff; + text-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.4); + } + .pop__score { + color: $mint; + } + .pop--perfect .pop__text { + color: #fde68a; + } +} + +.pop--ok .pop__score { + color: #fbbf24 !important; +} + +@keyframes pop-up { + 0% { + transform: translateY(40rpx) scale(0.6); + opacity: 0; + } + 20% { + transform: translateY(0) scale(1.1); + opacity: 1; + } + 35% { + transform: scale(1); + } + 80% { + opacity: 1; + } + 100% { + transform: translateY(-60rpx); + opacity: 0; + } +} + +// ───────── 底部栏 ───────── +.dock { + position: absolute; + left: 28rpx; + right: 28rpx; + bottom: calc(env(safe-area-inset-bottom) + 28rpx); + z-index: 10; + display: flex; + flex-direction: column; + align-items: center; +} + +.framing { + display: flex; + flex-direction: column; + align-items: center; + margin-bottom: 12rpx; +} + +.framing__ring { + width: 120rpx; + height: 120rpx; + border-radius: 50%; + display: flex; + align-items: center; + justify-content: center; +} + +.framing__ring-inner { + width: 100rpx; + height: 100rpx; + border-radius: 50%; + background: $glass-strong; + display: flex; + align-items: center; + justify-content: center; + color: #fff; + font-size: 24rpx; + font-weight: 700; +} + +.framing__tip { + margin-top: 20rpx; + padding: 16rpx 32rpx; + border-radius: 999rpx; + background: $glass-strong; + color: #fff; + font-size: 30rpx; + font-weight: 600; +} + +.tip { + display: flex; + align-items: center; + gap: 14rpx; + margin-bottom: 24rpx; + padding: 18rpx 30rpx; + border-radius: 999rpx; + background: #fbbf24; + box-shadow: 0 10rpx 30rpx rgba(251, 191, 36, 0.4); + animation: tip-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1); +} + +.tip__icon { + width: 40rpx; + height: 40rpx; + line-height: 40rpx; + border-radius: 50%; + background: $ink; + color: #fbbf24; + text-align: center; + font-size: 26rpx; + font-weight: 900; +} + +.tip__text { + color: $ink; + font-size: 32rpx; + font-weight: 800; +} + +@keyframes tip-in { + 0% { + transform: translateY(20rpx) scale(0.8); + opacity: 0; + } + 100% { + transform: none; + opacity: 1; + } +} + +// 教练口令(“吸气 · 屁股向后坐”):离得远也要看清 +.caption { + margin-bottom: 24rpx; + padding: 14rpx 34rpx; + border-radius: 999rpx; + font-size: 32rpx; + font-weight: 700; +} + +.is-light .caption { + background: #fff; + color: $ink; + box-shadow: 0 8rpx 24rpx rgba(22, 29, 25, 0.08); +} + +.is-dark .caption { + background: $glass; + color: #fff; +} + +.dock__row { + width: 100%; + box-sizing: border-box; + display: flex; + align-items: center; + gap: 28rpx; + padding: 22rpx 22rpx 22rpx 32rpx; + border-radius: 36rpx; +} + +.is-light .dock__row { + background: rgba(255, 255, 255, 0.9); + box-shadow: 0 12rpx 36rpx rgba(22, 29, 25, 0.1); +} + +.is-dark .dock__row { + background: $glass-strong; +} + +.dock__meter { + flex: 1; +} + +.dock__guided { + flex: 1; + font-size: 26rpx; + font-weight: 600; + color: $ink-soft; +} + +.dock__pause { + width: 88rpx; + height: 88rpx; + flex-shrink: 0; + border-radius: 50%; + background: var(--accent); + display: flex; + align-items: center; + justify-content: center; + box-shadow: 0 8rpx 20rpx rgba(22, 29, 25, 0.18); +} + +.pause-icon { + width: 24rpx; + height: 30rpx; + border-left: 8rpx solid #fff; + border-right: 8rpx solid #fff; + box-sizing: border-box; +} + +// ───────── 暂停弹层 ───────── +.mask { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + z-index: 20; + background: rgba(0, 0, 0, 0.55); + display: flex; + align-items: center; + justify-content: center; +} + +.dialog { + width: 600rpx; + padding: 48rpx 40rpx 32rpx; + border-radius: 40rpx; + background: #fff; + display: flex; + flex-direction: column; + align-items: center; +} + +.dialog__title { + font-size: 40rpx; + font-weight: 800; + color: $ink; +} + +.dialog__sub { + margin: 12rpx 0 36rpx; + font-size: 26rpx; + color: #6c7a71; +} + +.dialog__btn { + width: 100%; + height: 92rpx; + border-radius: 46rpx; + display: flex; + align-items: center; + justify-content: center; + font-size: 30rpx; + font-weight: 700; + color: #3c4a42; + margin-top: 16rpx; + + &.is-primary { + background: linear-gradient(135deg, #10b981, #006c49); + color: #fff; + } +} + +// ───────── 示范视频 ───────── +.video-mask { + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + z-index: 100; + background: #000; +} + +.video { + width: 100%; + height: 100%; +} + +.video-close { + position: absolute; + left: 24rpx; + width: 64rpx; + height: 64rpx; + border-radius: 50%; + background: rgba(255, 255, 255, 0.2); + color: #fff; + font-size: 44rpx; + display: flex; + align-items: center; + justify-content: center; +} diff --git a/TUICallKit-Vue3/training/pages/components/pose-intro.vue b/TUICallKit-Vue3/training/pages/components/pose-intro.vue new file mode 100644 index 000000000..4404feb65 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/pose-intro.vue @@ -0,0 +1,433 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/components/pose-summary.vue b/TUICallKit-Vue3/training/pages/components/pose-summary.vue new file mode 100644 index 000000000..1d4fc8157 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/pose-summary.vue @@ -0,0 +1,410 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/components/rep-ring.vue b/TUICallKit-Vue3/training/pages/components/rep-ring.vue new file mode 100644 index 000000000..b3b998a44 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/rep-ring.vue @@ -0,0 +1,102 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/components/self-view.vue b/TUICallKit-Vue3/training/pages/components/self-view.vue new file mode 100644 index 000000000..9ac519f6d --- /dev/null +++ b/TUICallKit-Vue3/training/pages/components/self-view.vue @@ -0,0 +1,199 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/pose-coach.vue b/TUICallKit-Vue3/training/pages/pose-coach.vue new file mode 100644 index 000000000..125b02066 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/pose-coach.vue @@ -0,0 +1,718 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pages/pose-hub.vue b/TUICallKit-Vue3/training/pages/pose-hub.vue new file mode 100644 index 000000000..f5408eee4 --- /dev/null +++ b/TUICallKit-Vue3/training/pages/pose-hub.vue @@ -0,0 +1,352 @@ + + + + + diff --git a/TUICallKit-Vue3/training/pose/__tests__/bgm.test.ts b/TUICallKit-Vue3/training/pose/__tests__/bgm.test.ts new file mode 100644 index 000000000..ff672065f --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/bgm.test.ts @@ -0,0 +1,215 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { createBgm, type BgmAudio } from '../bgm.ts' + +const URL = 'https://cdn.example.com/music/calm.mp3' + +function fakeAudio() { + const calls: string[] = [] + let onError: ((err: { errMsg?: string; errCode?: number }) => void) | null = null + const audio: BgmAudio = { + src: '', + loop: false, + volume: 1, + play: () => calls.push('play'), + pause: () => calls.push('pause'), + stop: () => calls.push('stop'), + destroy: () => calls.push('destroy'), + onError: (cb) => { + onError = cb + }, + } + return { audio, calls, fail: () => onError?.({ errMsg: 'play:fail', errCode: 10002 }) } +} + +/** 手动推进的定时器:run() 执行到渐变结束,返回执行了几次 */ +function fakeClock() { + let fn: (() => void) | null = null + return { + every: (_ms: number, f: () => void) => { + fn = f + return () => { + fn = null + } + }, + tick() { + fn?.() + }, + run(limit = 1000) { + let n = 0 + while (fn && n < limit) { + fn() + n += 1 + } + return n + }, + get running() { + return fn !== null + }, + } +} + +function setup(url = URL) { + const clock = fakeClock() + const player = fakeAudio() + let created = 0 + const reasons: string[] = [] + const bgm = createBgm({ + url, + volume: 0.4, + every: clock.every, + create: () => { + created += 1 + return player.audio + }, + onUnavailable: (r) => reasons.push(r), + }) + return { bgm, clock, player, reasons, created: () => created } +} + +test('开始播放:循环、从静音淡入到设定音量', () => { + const { bgm, clock, player } = setup() + bgm.setState('playing') + assert.equal(player.audio.src, URL) + assert.equal(player.audio.loop, true) + assert.deepEqual(player.calls, ['play']) + assert.equal(player.audio.volume, 0) + clock.tick() + assert.ok(player.audio.volume > 0 && player.audio.volume < 0.4, '第一步只升一点') + const ticks = clock.run() + assert.equal(player.audio.volume, 0.4) + assert.ok(ticks >= 20, `淡入要持续一段时间,实际 ${ticks} 步`) +}) + +test('教练说话时压低、说完恢复;压得快、恢复得慢', () => { + const { bgm, clock, player } = setup() + bgm.setState('playing') + clock.run() + bgm.setDuck(0.35) + const down = clock.run() + assert.ok(Math.abs(player.audio.volume - 0.14) < 1e-9) + bgm.setDuck(1) + const up = clock.run() + assert.equal(player.audio.volume, 0.4) + assert.ok(down <= 6, `压低要快,实际 ${down} 步`) + assert.ok(up > down, '恢复比压低慢') + assert.deepEqual(player.calls, ['play'], '压低不会暂停播放器') +}) + +test('暂停:先淡出再暂停;继续:接着播', () => { + const { bgm, clock, player } = setup() + bgm.setState('playing') + clock.run() + bgm.setState('paused') + clock.tick() + assert.deepEqual(player.calls, ['play'], '淡出过程中还在播') + clock.run() + assert.equal(player.audio.volume, 0) + assert.deepEqual(player.calls, ['play', 'pause']) + bgm.setState('playing') + clock.run() + assert.deepEqual(player.calls, ['play', 'pause', 'play']) + assert.equal(player.audio.volume, 0.4) +}) + +test('停止:淡出后回到开头;暂停状态下停止也会回到开头', () => { + const a = setup() + a.bgm.setState('playing') + a.clock.run() + a.bgm.setState('stopped') + a.clock.run() + assert.deepEqual(a.player.calls, ['play', 'stop']) + + const b = setup() + b.bgm.setState('playing') + b.clock.run() + b.bgm.setState('paused') + b.clock.run() + b.bgm.setState('stopped') + assert.deepEqual(b.player.calls, ['play', 'pause', 'stop']) +}) + +test('中途改主意:淡出一半又继续播,不会先暂停再播', () => { + const { bgm, clock, player } = setup() + bgm.setState('playing') + clock.run() + bgm.setState('paused') + clock.tick() + clock.tick() + bgm.setState('playing') + clock.run() + assert.deepEqual(player.calls, ['play']) + assert.equal(player.audio.volume, 0.4) +}) + +test('关掉背景音乐:不出声;训练中重新打开,接着播', () => { + const { bgm, clock, player, created } = setup() + bgm.setEnabled(false) + bgm.prepare() + bgm.setState('playing') + clock.run() + assert.equal(created(), 0, '关着的时候不去加载音乐') + bgm.setEnabled(true) + clock.run() + assert.deepEqual(player.calls, ['play']) + assert.equal(player.audio.volume, 0.4) + bgm.setEnabled(false) + clock.run() + assert.deepEqual(player.calls, ['play', 'pause']) +}) + +test('提前准备:建好播放器开始缓冲,但不出声', () => { + const { bgm, player, created } = setup() + bgm.prepare() + bgm.prepare() + assert.equal(created(), 1) + assert.equal(player.audio.src, URL) + assert.deepEqual(player.calls, []) +}) + +test('加载失败:静默放弃、只通知一次,之后不再尝试', () => { + const { bgm, clock, player, reasons, created } = setup() + bgm.setState('playing') + player.fail() + player.fail() + assert.equal(bgm.available, false) + assert.equal(reasons.length, 1) + assert.ok(player.calls.includes('destroy')) + assert.equal(clock.running, false) + bgm.setState('paused') + bgm.setState('playing') + clock.run() + assert.equal(created(), 1, '不会重新创建播放器') +}) + +test('没有配置音乐地址:不可用,也不创建播放器', () => { + const { bgm, clock, created } = setup('') + assert.equal(bgm.available, false) + bgm.prepare() + bgm.setState('playing') + clock.run() + assert.equal(created(), 0) +}) + +test('切到后台:立即静音暂停(后台定时器可能不跑,不能靠淡出)', () => { + const { bgm, clock, player } = setup() + bgm.setState('playing') + clock.run() + bgm.hush() + assert.equal(player.audio.volume, 0) + assert.deepEqual(player.calls, ['play', 'pause']) + assert.equal(clock.running, false) + // 回到前台、训练继续时照常淡入 + bgm.setState('playing') + clock.run() + assert.deepEqual(player.calls, ['play', 'pause', 'play']) +}) + +test('销毁:释放播放器、停止定时器', () => { + const { bgm, clock, player } = setup() + bgm.setState('playing') + clock.tick() + bgm.destroy() + assert.ok(player.calls.includes('destroy')) + assert.equal(clock.running, false) +}) diff --git a/TUICallKit-Vue3/training/pose/__tests__/cameraPermission.test.ts b/TUICallKit-Vue3/training/pose/__tests__/cameraPermission.test.ts new file mode 100644 index 000000000..4724dd7b2 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/cameraPermission.test.ts @@ -0,0 +1,56 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { classifyAuthError, requestCameraPermission, type PermissionApi } from '../cameraPermission.ts' + +function fakeApi(setting: Record | null, authorizeError?: { errno?: number; errMsg: string }) { + const calls = { authorize: 0 } + const api: PermissionApi = { + getSetting({ success, fail }) { + if (setting) success?.({ authSetting: setting }) + else fail?.({ errMsg: 'getSetting:fail' }) + }, + authorize({ success, fail }) { + calls.authorize += 1 + if (authorizeError) fail?.(authorizeError) + else success?.() + }, + } + return { api, calls } +} + +test('失败原因分类:后台未声明 / 未同意隐私指引 / 用户拒绝', () => { + assert.equal(classifyAuthError({ errno: 112, errMsg: 'authorize:fail api scope is not declared in the privacy agreement' }), 'undeclared') + assert.equal(classifyAuthError({ errMsg: 'authorize:fail api scope is not declared in the privacy agreement' }), 'undeclared') + assert.equal(classifyAuthError({ errno: 104, errMsg: 'authorize:fail privacy permission is not authorized' }), 'privacy_refused') + assert.equal(classifyAuthError({ errMsg: 'authorize:fail auth deny' }), 'denied') + assert.equal(classifyAuthError({ errMsg: 'authorize:fail system error' }), 'error') + assert.equal(classifyAuthError(undefined), 'error') +}) + +test('设置里已授权:直接可用,不再弹授权框', async () => { + const { api, calls } = fakeApi({ 'scope.camera': true }) + assert.equal((await requestCameraPermission(api)).result, 'granted') + assert.equal(calls.authorize, 0) +}) + +test('设置里拒绝过:引导去设置页,不再弹授权框', async () => { + const { api, calls } = fakeApi({ 'scope.camera': false }) + assert.equal((await requestCameraPermission(api)).result, 'denied') + assert.equal(calls.authorize, 0) +}) + +test('从没问过:弹授权框,按结果返回', async () => { + assert.equal((await requestCameraPermission(fakeApi({}).api)).result, 'granted') + const undeclared = await requestCameraPermission( + fakeApi({}, { errno: 112, errMsg: 'authorize:fail api scope is not declared in the privacy agreement' }).api, + ) + assert.equal(undeclared.result, 'undeclared') + assert.match(undeclared.errMsg, /privacy agreement/) + assert.equal((await requestCameraPermission(fakeApi({}, { errMsg: 'authorize:fail auth deny' }).api)).result, 'denied') +}) + +test('读取设置失败时仍然尝试授权', async () => { + const { api, calls } = fakeApi(null) + assert.equal((await requestCameraPermission(api)).result, 'granted') + assert.equal(calls.authorize, 1) +}) diff --git a/TUICallKit-Vue3/training/pose/__tests__/coachFrames.test.ts b/TUICallKit-Vue3/training/pose/__tests__/coachFrames.test.ts new file mode 100644 index 000000000..1de4f9c0b --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/coachFrames.test.ts @@ -0,0 +1,108 @@ +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() + 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}`) + } +}) diff --git a/TUICallKit-Vue3/training/pose/__tests__/counters.test.ts b/TUICallKit-Vue3/training/pose/__tests__/counters.test.ts new file mode 100644 index 000000000..c2a88611f --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/counters.test.ts @@ -0,0 +1,132 @@ +/** + * 计数器单测:用示范动作(motions.ts)合成姿态序列来验证。 + * 运行:node --test training/pose/__tests__/ + */ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { createCounter, type CoachEvent } from '../counters.ts' +import { MISTAKE_POSES, MOTIONS, sampleMotion, type MoveId } from '../motions.ts' +import type { Pose } from '../keypoints.ts' + +const MOVES: MoveId[] = ['squat', 'march', 'jack', 'raise'] + +/** 固定种子的伪随机,模拟关键点抖动 */ +function rng(seed: number) { + let s = seed + return () => ((s = (s * 16807) % 2147483647) / 2147483647) - 0.5 +} + +function run(move: MoveId, frames: Pose[]) { + const c = createCounter(move) + const events: CoachEvent[] = [] + for (const f of frames) events.push(...c.update(f)) + return { count: c.count, events, reps: events.filter((e) => e.type === 'rep'), tips: events.map((e) => (e.type === 'tip' ? e.text : e.tip)).filter(Boolean) } +} + +function mistakeFrames(move: MoveId, seconds: number, fps = 12): Pose[] { + const spec = MOTIONS[move] + return Array.from({ length: Math.round(seconds * fps) }, (_, i) => { + const points = MISTAKE_POSES[move](i / fps / spec.cycleSeconds) + return { points, scores: points.map(() => 0.9), t: (i / fps) * 1000 } + }) +} + +/** 同一姿态放进 3:4 竖屏画面:x 归一化坐标被拉伸,aspect 修正后几何不变 */ +function toPortrait(frames: Pose[], aspect = 0.75): Pose[] { + return frames.map((f) => ({ ...f, aspect, points: f.points.map((p) => ({ x: 0.5 + (p.x - 0.5) / aspect, y: p.y })) })) +} + +for (const move of MOVES) { + const cycles = 5 + const expected = cycles * MOTIONS[move].repsPerCycle + const frames = sampleMotion(move, MOTIONS[move].cycleSeconds * cycles + 0.3) + + test(`${move}: 标准示范动作计数准确且评分高`, () => { + const r = run(move, frames) + assert.equal(r.count, expected) + for (const rep of r.reps) if (rep.type === 'rep') assert.ok(rep.score >= 85, `score ${rep.score}`) + assert.deepEqual(r.tips, []) + }) + + test(`${move}: 关键点抖动下计数仍准确`, () => { + const noise = rng(7) + const noisy = frames.map((f) => ({ ...f, points: f.points.map((p) => ({ x: p.x + noise() * 0.008, y: p.y + noise() * 0.008 })) })) + assert.equal(run(move, noisy).count, expected) + }) + + test(`${move}: 竖屏画面比例下结果一致`, () => { + assert.equal(run(move, toPortrait(frames)).count, expected) + }) + + test(`${move}: 低帧率(6fps)下不漏计`, () => { + assert.equal(run(move, sampleMotion(move, MOTIONS[move].cycleSeconds * cycles + 0.3, 6)).count, expected) + }) +} + +test('squat: 膝盖内扣会被提示', () => { + const r = run('squat', mistakeFrames('squat', MOTIONS.squat.cycleSeconds * 3 + 0.3)) + assert.equal(r.count, 3) + assert.ok(r.tips.some((t) => t!.includes('膝盖向外')), JSON.stringify(r.tips)) +}) + +test('march: 抬腿太低不计数并提示', () => { + const r = run('march', mistakeFrames('march', MOTIONS.march.cycleSeconds * 4)) + assert.equal(r.count, 0) + assert.ok(r.tips.some((t) => t!.includes('抬高')), JSON.stringify(r.tips)) +}) + +test('jack: 手臂没举过头不计数并提示', () => { + const r = run('jack', mistakeFrames('jack', MOTIONS.jack.cycleSeconds * 3)) + assert.equal(r.count, 0) + assert.ok(r.tips.some((t) => t!.includes('举过头顶')), JSON.stringify(r.tips)) +}) + +test('raise: 耸肩或抬太高会被提示', () => { + const r = run('raise', mistakeFrames('raise', MOTIONS.raise.cycleSeconds * 3 + 0.3)) + assert.equal(r.count, 3) + assert.ok(r.tips.some((t) => t!.includes('耸肩') || t!.includes('与肩同高')), JSON.stringify(r.tips)) +}) + +test('关键点不可见时不计数', () => { + const frames = sampleMotion('squat', 10).map((f) => ({ ...f, scores: f.scores.map(() => 0.1) })) + assert.equal(run('squat', frames).count, 0) +}) + +test('同一提示在冷却时间内不重复', () => { + const r = run('march', mistakeFrames('march', 3)) + const n = r.tips.filter((t) => t!.includes('抬高')).length + assert.ok(n <= 1, `tips ${n}`) +}) + +test('live 幅度跟随动作:到位时接近 1,站立时接近 0', () => { + for (const move of ['squat', 'march', 'jack', 'raise'] as MoveId[]) { + const c = createCounter(move) + let max = 0 + let min = 1 + for (const f of sampleMotion(move, MOTIONS[move].cycleSeconds * 2)) { + c.update(f) + max = Math.max(max, c.live.amount) + min = Math.min(min, c.live.amount) + } + assert.ok(max > 0.85, `${move} max ${max}`) + assert.ok(min < 0.12, `${move} min ${min}`) + } +}) + +test('live 左右侧判断正确(高抬腿)', () => { + const c = createCounter('march') + const spec = MOTIONS.march + const left = spec.poseAt(0.25) + c.update({ points: left, scores: left.map(() => 0.9), t: 0 }) + assert.equal(c.live.side, 'L') + const right = spec.poseAt(0.75) + c.update({ points: right, scores: right.map(() => 0.9), t: 100 }) + assert.equal(c.live.side, 'R') +}) + +test('幅度条目标线:到达目标幅度才计数', () => { + for (const m of ['squat', 'march', 'jack', 'raise'] as const) { + const c = createCounter(m) + assert.ok(c.goal > 0.2 && c.goal < 0.9, `${m} goal=${c.goal}`) + } +}) diff --git a/TUICallKit-Vue3/training/pose/__tests__/history.test.ts b/TUICallKit-Vue3/training/pose/__tests__/history.test.ts new file mode 100644 index 000000000..ad064fba3 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/history.test.ts @@ -0,0 +1,54 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { appendHistory, loadHistory, statsOf, dayOf, type Storage } from '../history.ts' +import type { SessionSummary } from '../session.ts' + +const memory = (): Storage => { + const m = new Map() + return { get: (k) => (m.has(k) ? m.get(k) : ''), set: (k, v) => m.set(k, v) } +} + +const summary = (reps: number): SessionSummary => ({ + move: 'squat', + reps, + targetReps: 12, + avgScore: 90, + bestCombo: 5, + durationSec: 120, + kcal: 8, + stars: 3, + scores: [], + advice: '', +}) + +const DAY = 86400000 +const now = new Date(2026, 8, 28, 20, 0, 0).getTime() + +test('空存储返回空数组', () => { + assert.deepEqual(loadHistory(memory()), []) +}) + +test('0 次的训练不记录', () => { + const s = memory() + appendHistory(s, summary(0), now) + assert.equal(loadHistory(s).length, 0) +}) + +test('今日统计与连续天数', () => { + const s = memory() + appendHistory(s, summary(10), now - 2 * DAY) + appendHistory(s, summary(10), now - DAY) + appendHistory(s, summary(12), now) + appendHistory(s, summary(8), now) + const st = statsOf(loadHistory(s), now) + assert.equal(st.todayReps, 20) + assert.equal(st.todayMinutes, 4) + assert.equal(st.streak, 3) +}) + +test('今天还没练时,连续天数从昨天算起', () => { + const s = memory() + appendHistory(s, summary(10), now - DAY) + assert.equal(statsOf(loadHistory(s), now).streak, 1) + assert.equal(dayOf(now), '2026-09-28') +}) diff --git a/TUICallKit-Vue3/training/pose/__tests__/session.test.ts b/TUICallKit-Vue3/training/pose/__tests__/session.test.ts new file mode 100644 index 000000000..35ac2cc78 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/session.test.ts @@ -0,0 +1,95 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { TrainingSession, summarize, type SessionEvent } from '../session.ts' +import { MOTIONS, sampleMotion } from '../motions.ts' +import type { Pose } from '../keypoints.ts' + +const standing = (t: number): Pose => { + const points = MOTIONS.squat.poseAt(0) + return { points, scores: points.map(() => 0.9), t } +} + +/** 站好 → 倒计时 → 开始训练,返回训练开始时刻 */ +function warmUp(s: TrainingSession, events: SessionEvent[]) { + let t = 0 + for (; t <= 5000; t += 100) events.push(...s.feed(standing(t), t)) + return t +} + +test('站好保持 1.5 秒后进入倒计时,3 秒后开始训练', () => { + const s = new TrainingSession('squat', 5) + const events: SessionEvent[] = [] + warmUp(s, events) + const phases = events.filter((e) => e.type === 'phase').map((e) => (e as any).phase) + assert.deepEqual(phases, ['countdown', 'training']) + const counts = events.filter((e) => e.type === 'countdown').map((e) => (e as any).n) + assert.deepEqual(counts, [3, 2, 1]) +}) + +test('没站进画面不会开始', () => { + const s = new TrainingSession('squat', 5) + for (let t = 0; t < 5000; t += 100) s.feed(null, t) + assert.equal(s.phase, 'framing') + assert.equal(s.framingIssue, 'no_body') +}) + +test('完成目标次数后结束,并统计时长与连击', () => { + const s = new TrainingSession('squat', 4) + const events: SessionEvent[] = [] + const t0 = warmUp(s, events) + for (const f of sampleMotion('squat', MOTIONS.squat.cycleSeconds * 5, 12, t0)) { + events.push(...s.feed(f, f.t)) + if (s.phase === 'done') break + } + assert.equal(s.phase, 'done') + assert.equal(s.count, 4) + assert.equal(s.bestCombo, 4) + assert.ok(events.some((e) => e.type === 'combo' && e.combo === 3)) + const sum = summarize(s, 4) + assert.equal(sum.stars, 3) + assert.equal(sum.adviceKind, 'praise') + assert.ok(sum.durationSec >= 9 && sum.durationSec <= 14, `duration ${sum.durationSec}`) + assert.ok(sum.kcal > 0) +}) + +test('离开画面自动进入 lost,回来后继续,离开期间不计时', () => { + const s = new TrainingSession('squat', 10) + const t0 = warmUp(s, []) + s.feed(standing(t0 + 100), t0 + 100) + const before = s.activeMs + let t = t0 + 200 + for (; t < t0 + 6000; t += 100) s.feed(null, t) + assert.equal(s.phase, 'lost') + assert.ok(s.activeMs - before < 2000, `active grew ${s.activeMs - before}`) + s.feed(standing(t), t) + assert.equal(s.phase, 'training') +}) + +test('暂停与恢复', () => { + const s = new TrainingSession('raise', 10) + const t0 = warmUp(s, []) + s.pause() + assert.equal(s.phase, 'paused') + const before = s.activeMs + s.tick(t0 + 10000) + assert.equal(s.activeMs, before) + s.resume(t0 + 10000) + assert.equal(s.phase, 'training') +}) + +test('领练模式:跳过站位、按节奏计数、不会因无人而暂停', () => { + const s = new TrainingSession('march', 3, { detectLost: false }) + s.startCountdown(0) + assert.equal(s.phase, 'countdown') + for (let t = 0; t <= 3200; t += 200) s.tick(t) + assert.equal(s.phase, 'training') + for (let t = 3400; t < 9000; t += 200) s.tick(t) + assert.equal(s.phase, 'training') + s.addGuidedRep() + s.addGuidedRep() + const last = s.addGuidedRep() + assert.equal(s.count, 3) + assert.equal(s.phase, 'done') + assert.ok(last.some((e) => e.type === 'phase' && e.phase === 'done')) + assert.equal(s.reps.length, 0) +}) diff --git a/TUICallKit-Vue3/training/pose/__tests__/viewport.test.ts b/TUICallKit-Vue3/training/pose/__tests__/viewport.test.ts new file mode 100644 index 000000000..5d6b6e208 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/__tests__/viewport.test.ts @@ -0,0 +1,26 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { toScreen } from '../viewport.ts' + +const near = (a: number, b: number) => Math.abs(a - b) < 1e-6 + +test('比例一致时直接按尺寸缩放', () => { + const p = toScreen({ x: 0.25, y: 0.5 }, { width: 300, height: 400, frameWidth: 480, frameHeight: 640, mirror: false }) + assert.ok(near(p.x, 75) && near(p.y, 200)) +}) + +test('全屏竖屏按 aspectFill 裁掉左右两边,画面中心仍在屏幕中心', () => { + const vp = { width: 390, height: 844, frameWidth: 480, frameHeight: 640, mirror: false } + const c = toScreen({ x: 0.5, y: 0.5 }, vp) + assert.ok(near(c.x, 195) && near(c.y, 422)) + const top = toScreen({ x: 0.5, y: 0 }, vp) + assert.ok(near(top.y, 0), '高度方向不裁剪') + const left = toScreen({ x: 0, y: 0.5 }, vp) + assert.ok(left.x < 0, '宽度方向被裁掉的部分落在屏幕外') +}) + +test('镜像预览左右翻转', () => { + const vp = { width: 300, height: 400, frameWidth: 480, frameHeight: 640, mirror: true } + const p = toScreen({ x: 0.2, y: 0.3 }, vp) + assert.ok(near(p.x, 240) && near(p.y, 120)) +}) diff --git a/TUICallKit-Vue3/training/pose/bgm.ts b/TUICallKit-Vue3/training/pose/bgm.ts new file mode 100644 index 000000000..f722b2578 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/bgm.ts @@ -0,0 +1,181 @@ +/** + * 训练背景音乐的播放控制(不依赖页面,便于测试) + * + * 一段首尾相接循环的舒缓音乐,音量比教练语音低一截,所有音量变化都是渐变: + * - 开始播放淡入;暂停 / 停止先淡出再真正暂停,不会突然出声或戛然而止; + * - 教练说话时压低(duck),压得快、恢复得慢,像电台主持人开口时压背景音乐; + * - 加载失败(地址不存在、断网)就当没有背景音乐:不再重试,也不打扰用户。 + */ + +export type BgmState = 'playing' | 'paused' | 'stopped' + +/** 播放器的最小接口:小程序 InnerAudioContext 的子集,测试时可换成假的 */ +export type BgmAudio = { + src: string + loop: boolean + volume: number + play(): void + pause(): void + stop(): void + destroy(): void + onError(cb: (err: { errMsg?: string; errCode?: number }) => void): void +} + +export type BgmOptions = { + /** 音乐地址;为空表示没有背景音乐 */ + url: string + create: () => BgmAudio + /** 正常播放时的音量(0~1,教练语音是 1) */ + volume?: number + /** 定时器(测试时替换),返回取消函数 */ + every?: (ms: number, fn: () => void) => () => void + /** 音乐不可用(加载失败)时回调,只回调一次 */ + onUnavailable?: (reason: string) => void +} + +const TICK_MS = 50 +/** 音量从 0 变到正常音量所需的时长(ms) */ +const RAMP_MS = { fade: 1600, duck: 250, unduck: 800 } + +function defaultEvery(ms: number, fn: () => void) { + const id = setInterval(fn, ms) + return () => clearInterval(id) +} + +export function createBgm(opts: BgmOptions) { + const base = opts.volume ?? 0.35 + const every = opts.every ?? defaultEvery + let audio: BgmAudio | null = null + let failed = !opts.url + let state: BgmState = 'stopped' + let enabled = true + let duck = 1 + let level = 0 + /** 播放器在出声(play 之后、pause / stop 之前) */ + let running = false + /** 播放位置在开头 */ + let rewound = true + /** 当前这次渐变是淡入淡出,还是压低 / 恢复 */ + let ramp: 'fade' | 'duck' = 'fade' + let cancel: (() => void) | null = null + + const target = () => (state === 'playing' && enabled && !failed ? base * duck : 0) + + function ensure(): BgmAudio | null { + if (failed) return null + if (!audio) { + const a = opts.create() + a.src = opts.url + a.loop = true + a.volume = 0 + a.onError((err) => fail(err?.errMsg || `errCode ${err?.errCode ?? '?'}`)) + audio = a + } + return audio + } + + function fail(reason: string) { + if (failed) return + failed = true + release() + opts.onUnavailable?.(reason) + } + + function stopTicker() { + cancel?.() + cancel = null + } + + function release() { + stopTicker() + try { + audio?.destroy() + } catch (e) {} + audio = null + running = false + rewound = true + level = 0 + } + + /** 音量降到 0 之后才真正暂停 / 停止(停止会回到开头,下次从头播) */ + function settle() { + if (!audio || level > 0) return + if (state === 'stopped') { + if (running || !rewound) audio.stop() + rewound = true + } else if (running) { + audio.pause() + } + running = false + } + + function step() { + const t = target() + const ms = ramp === 'fade' ? RAMP_MS.fade : t < level ? RAMP_MS.duck : RAMP_MS.unduck + const max = (base * TICK_MS) / ms + level = Math.abs(t - level) <= max ? t : level + Math.sign(t - level) * max + if (audio) audio.volume = level + if (level === t) { + stopTicker() + settle() + } + } + + function update(kind: 'fade' | 'duck') { + ramp = kind + const t = target() + if (t > 0) { + const a = ensure() + if (!a) return + if (!running) { + a.play() + running = true + rewound = false + } + } + if (level === t) { + stopTicker() + settle() + } else if (!cancel) { + cancel = every(TICK_MS, step) + } + } + + return { + /** 提前建好播放器、开始缓冲(不出声),开始训练时能立刻响起来 */ + prepare() { + if (enabled) ensure() + }, + setState(next: BgmState) { + if (next === state) return + state = next + update('fade') + }, + /** 音量压到正常音量的几成(1 = 不压) */ + setDuck(ratio: number) { + const r = Math.min(1, Math.max(0, ratio)) + if (r === duck) return + duck = r + update('duck') + }, + setEnabled(on: boolean) { + if (on === enabled) return + enabled = on + update('fade') + }, + /** 立即静音暂停:切到后台时定时器可能不再执行,不能靠淡出 */ + hush() { + if (state === 'playing') state = 'paused' + stopTicker() + level = 0 + if (audio) audio.volume = 0 + settle() + }, + destroy: release, + get available() { + return !failed + }, + } +} + +export type Bgm = ReturnType diff --git a/TUICallKit-Vue3/training/pose/cameraPermission.ts b/TUICallKit-Vue3/training/pose/cameraPermission.ts new file mode 100644 index 000000000..c67befc6f --- /dev/null +++ b/TUICallKit-Vue3/training/pose/cameraPermission.ts @@ -0,0 +1,51 @@ +/** + * 摄像头权限 + * + * 摄像头属于微信的隐私接口:小程序后台《用户隐私保护指引》没有声明“摄像头”时, + * wx.authorize 直接失败(errno 112),不弹授权框,设置页里也不会出现“摄像头”选项。 + * 所以授权失败后不能一律引导用户去设置页,要先分清原因: + * denied 用户拒绝过 → 设置页里有这一项,引导去开启 + * privacy_refused 用户没同意隐私保护指引 → 重新弹出隐私授权 + * undeclared 后台没声明摄像头 → 用户侧无解,只能改用领练模式(开发者需补充隐私指引) + */ + +export type CameraPermission = 'granted' | 'denied' | 'privacy_refused' | 'undeclared' | 'error' + +type AuthError = { errno?: number; errMsg?: string } | undefined + +/** 与 uni / wx 同名接口的最小子集,便于单测注入 */ +export type PermissionApi = { + getSetting(opts: { success?: (res: { authSetting?: Record }) => void; fail?: (err: AuthError) => void }): void + authorize(opts: { scope: string; success?: () => void; fail?: (err: AuthError) => void }): void +} + +const SCOPE = 'scope.camera' + +export function classifyAuthError(err: AuthError): CameraPermission { + const msg = String(err?.errMsg ?? '') + if (err?.errno === 112 || /not declared in the privacy agreement/i.test(msg)) return 'undeclared' + if (err?.errno === 104 || /privacy/i.test(msg)) return 'privacy_refused' + if (/deny|denied/i.test(msg)) return 'denied' + return 'error' +} + +/** 先看设置里的授权状态:授权过直接用,拒绝过去设置页,从没问过才弹授权框 */ +export function requestCameraPermission(api: PermissionApi): Promise<{ result: CameraPermission; errMsg: string }> { + return new Promise((resolve) => { + const authorize = () => + api.authorize({ + scope: SCOPE, + success: () => resolve({ result: 'granted', errMsg: '' }), + fail: (err) => resolve({ result: classifyAuthError(err), errMsg: String(err?.errMsg ?? '') }), + }) + api.getSetting({ + success: (res) => { + const v = res?.authSetting?.[SCOPE] + if (v === true) resolve({ result: 'granted', errMsg: '' }) + else if (v === false) resolve({ result: 'denied', errMsg: '' }) + else authorize() + }, + fail: () => authorize(), + }) + }) +} diff --git a/TUICallKit-Vue3/training/pose/coachFrames.ts b/TUICallKit-Vue3/training/pose/coachFrames.ts new file mode 100644 index 000000000..0f9146dec --- /dev/null +++ b/TUICallKit-Vue3/training/pose/coachFrames.ts @@ -0,0 +1,81 @@ +/** + * 教练“小甄”帧资源 + * + * 动作帧是 AI 画的原图加少量插帧(由 training-videos/scripts 生成并对齐),内置在 training/static/coach。 + * 每个动作的一侧 10~24 帧,出自同一张设定稿并统一了脸(前后帧一致,不会闪); + * 每帧带实测的动作幅度,按幅度查帧,帧间距不均匀时播放速度仍然均匀。 + */ +import type { MoveId, Side } from './motions.ts' +import { COACH_HD, COACH_SEQUENCES, type CoachSequence } from './coachSequences.ts' + +export type Gesture = 'idle' | 'wave' | 'cheer' | 'thumbs' + +const MOVE_SEQUENCE: Record> = { + squat: { L: 'squat', R: 'squat' }, + march: { L: 'march_l', R: 'march_r' }, + jack: { L: 'jack_l', R: 'jack_r' }, + raise: { L: 'raise', R: 'raise' }, +} + +/** 错误示范帧(示范视频对比用) */ +export const MISTAKE_FRAMES: Record = { + squat: 'wrong_squat', + march: 'wrong_march', + jack: 'wrong_jack', + raise: 'wrong_raise', +} + +function seqOf(move: MoveId, side: Side): CoachSequence { + return COACH_SEQUENCES[MOVE_SEQUENCE[move][side]] +} + +export function sequenceOf(move: MoveId, side: Side): string[] { + return seqOf(move, side).frames +} + +/** 动作到位的那一帧(封面、错误示范对比用) */ +export function peakFrame(move: MoveId): string { + const frames = sequenceOf(move, 'L') + return frames[frames.length - 1] +} + +export function frameUrl(name: string): string { + return `/training/static/coach/${name}.png` +} + +/** 高清帧地址(COS):带内容版本号,重新上传同名文件后缓存自动刷新 */ +export function hdFrameUrl(base: string, name: string): string { + return `${base}/${name}.webp?v=${COACH_HD.version}` +} + +/** 高清帧的裁切框:在整张画布上的比例 [左, 上, 右, 下],用来把裁过的图放回原位 */ +export function hdCropOf(move: MoveId): [number, number, number, number] { + return COACH_HD.crops[move] +} + +/** 播放某个动作需要加载的全部帧 */ +export function framesToLoad(move: MoveId): string[] { + return [...new Set([...sequenceOf(move, 'L'), ...sequenceOf(move, 'R')])] +} + +export type FramePair = { a: string; b: string; w: number } + +/** 动作幅度 → 相邻两帧与两帧之间的位置(0~1),按每帧的实测幅度插值 */ +export function framePair(move: MoveId, amount: number, side: Side): FramePair { + const { frames, amounts } = seqOf(move, side) + const x = Math.min(1, Math.max(0, amount)) + let i = 0 + while (i < frames.length - 2 && amounts[i + 1] <= x) i += 1 + const span = amounts[i + 1] - amounts[i] + const w = span > 0 ? Math.min(1, Math.max(0, (x - amounts[i]) / span)) : 1 + return { a: frames[i], b: frames[i + 1], w } +} + +/** + * 播放时的混合权重:大部分时间显示单张清晰帧,只在换帧的瞬间短暂过渡。 + * 相邻两帧一直按比例混合的话,移动中的手臂会出现半透明的双影。 + */ +export function crispBlend(w: number): number { + const t = Math.min(1, Math.max(0, (w - 0.44) / 0.12)) + return t * t * (3 - 2 * t) +} diff --git a/TUICallKit-Vue3/training/pose/coachSequences.ts b/TUICallKit-Vue3/training/pose/coachSequences.ts new file mode 100644 index 000000000..8a40fcfc3 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/coachSequences.ts @@ -0,0 +1,53 @@ +/** + * 教练动作帧清单(由 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 = { + squat: { + frames: ['sq_00', 'sq_01', 'sq_02', 'sq_03', 'sq_04', 'sq_05', 'sq_06', 'sq_07', 'sq_08', 'sq_09', 'sq_10', 'sq_11', 'sq_12', 'sq_13', 'sq_14', 'sq_15', 'sq_16', 'sq_17', 'sq_18', 'sq_19', 'sq_20', 'sq_21', 'sq_22', 'sq_23'], + amounts: [0, 0.057, 0.077, 0.133, 0.168, 0.266, 0.304, 0.342, 0.383, 0.424, 0.456, 0.488, 0.528, 0.569, 0.609, 0.648, 0.702, 0.729, 0.781, 0.834, 0.859, 0.92, 0.957, 1], + }, + raise: { + frames: ['ra_00', 'ra_01', 'ra_02', 'ra_03', 'ra_04', 'ra_05', 'ra_06', 'ra_07', 'ra_08', 'ra_09', 'ra_10', 'ra_11', 'ra_12', 'ra_13', 'ra_14', 'ra_15', 'ra_16', 'ra_17', 'ra_18', 'ra_19', 'ra_20', 'ra_21', 'ra_22', 'ra_23'], + amounts: [0, 0.118, 0.162, 0.206, 0.245, 0.285, 0.323, 0.36, 0.398, 0.437, 0.461, 0.485, 0.512, 0.579, 0.617, 0.645, 0.708, 0.743, 0.771, 0.821, 0.866, 0.909, 0.957, 1], + }, + jack_l: { + frames: ['jl_00', 'jl_01', 'jl_02', 'jl_03', 'jl_04', 'jl_05', 'jl_06', 'jl_07', 'jl_08', 'jl_09'], + amounts: [0, 0.105, 0.239, 0.316, 0.463, 0.539, 0.677, 0.782, 0.876, 1], + }, + jack_r: { + frames: ['jl_00', 'jr_01', 'jr_02', 'jr_03', 'jr_04', 'jr_05', 'jr_06', 'jr_07', 'jr_08', 'jr_09'], + amounts: [0, 0.105, 0.239, 0.316, 0.463, 0.539, 0.677, 0.782, 0.876, 1], + }, + march_l: { + frames: ['ml_00', 'ml_01', 'ml_02', 'ml_03', 'ml_04', 'ml_05', 'ml_06', 'ml_07', 'ml_08', 'ml_09', 'ml_10', 'ml_11', 'ml_12'], + amounts: [0, 0.025, 0.25, 0.303, 0.356, 0.427, 0.499, 0.576, 0.638, 0.728, 0.755, 0.877, 1], + }, + march_r: { + frames: ['ml_00', 'mr_01', 'mr_02', 'mr_03', 'mr_04', 'mr_05', 'mr_06', 'mr_07', 'mr_08', 'mr_09', 'mr_10', 'mr_11', 'mr_12'], + amounts: [0, 0.025, 0.25, 0.303, 0.356, 0.427, 0.499, 0.576, 0.638, 0.728, 0.755, 0.877, 1], + }, +} + +/** + * 高清帧(training-videos/out/coach 上传到 COS):每个动作裁出人物所在区域, + * crops 是裁切框在整张画布上的比例 [左, 上, 右, 下];version 随内容变化,用来刷新 CDN 缓存。 + */ +export const COACH_HD: { version: string; crops: Record } = { + version: '7ea1f74601', + crops: { + squat: [0.358, 0.219, 0.642, 0.962], + raise: [0.169, 0.218, 0.822, 0.962], + jack: [0.173, 0.13, 0.825, 0.962], + march: [0.362, 0.218, 0.64, 0.961], + }, +} diff --git a/TUICallKit-Vue3/training/pose/counters.ts b/TUICallKit-Vue3/training/pose/counters.ts new file mode 100644 index 000000000..ef95af188 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/counters.ts @@ -0,0 +1,366 @@ +/** + * 动作计数与纠错 + * + * 每个动作一个“单指标 + 滞回阈值”的状态机: + * 指标越过 enter 阈值 → 进入动作中,记录峰值与动作期间的姿态问题 + * 指标回落到 exit 阈值 → 结束一次,按峰值评分并给出提示 + * 阈值全部是身体比例或关节角度,与人离镜头远近无关。 + * + * 纯逻辑,不依赖 uni / wx,可直接用 Node 单测。 + */ +import { KP, angleAt, isVisible, mid, pt, shoulderWidth, torsoLength, type Pose } from './keypoints.ts' +import type { Amount, MoveId, Side } from './motions.ts' + +export type Grade = 'perfect' | 'good' | 'ok' + +export type CoachEvent = + | { type: 'rep'; count: number; score: number; grade: Grade; tip?: string; joints?: number[] } + | { type: 'tip'; text: string; joints?: number[] } + +export interface RepCounter { + readonly move: MoveId + readonly count: number + /** 当前帧的动作幅度(0~1),镜像模式用它驱动教练 */ + readonly live: Amount + /** 计一次所需的动作幅度(进入阈值换算成 0~1),界面幅度条上的目标线 */ + readonly goal: number + update(pose: Pose): CoachEvent[] + reset(): void +} + +/** 一次动作过程中收集的信息 */ +type RepTrace = { + peak: number + startT: number + /** 动作期间出现过的问题(取最严重的一次) */ + flags: Set +} + +type Rule = { + /** 当前帧指标;关键点不可见时返回 null */ + metric: (pose: Pose, ctx: Ctx) => number | null + enter: number + exit: number + /** 峰值达到 attempt 但没到 enter,回落时提示“幅度不够” */ + attempt: number + attemptTip: string + attemptJoints: number[] + /** 峰值 → 分数与提示 */ + judge: (peak: number, trace: RepTrace) => { score: number; tip?: string; joints?: number[] } + /** 动作进行中逐帧检查姿态 */ + checkForm?: (pose: Pose, ctx: Ctx, flags: Set) => void + /** 最短单次时长(ms),更快则提示放慢 */ + minRepMs?: number + /** 指标 → 动作幅度(0 = 起始姿势,1 = 标准到位) */ + amount: (v: number) => number + /** 左右交替的动作:当前是哪一侧 */ + side?: (pose: Pose) => Side +} + +type Ctx = { + /** 站立基线(腿长等),缓慢自适应 */ + baseline: Record +} + +/** 纠错提示文案(语音会在训练前预合成,改文案只需改这里) */ +export const TIPS = { + squat: { + shallowAttempt: '再蹲低一点,屁股向后坐', + valgus: '膝盖向外打开,对准脚尖', + shallow: '不错,下次再蹲深一点', + }, + march: { + lowAttempt: '膝盖再抬高一点', + low: '膝盖抬到腰的高度更有效', + }, + jack: { + armsAttempt: '双手举过头顶', + step: '手举起来时,脚向外迈一步', + }, + raise: { + lowAttempt: '手臂再抬高,与肩同高', + shrug: '肩膀下沉,不要耸肩', + high: '抬到与肩同高就好,不用更高', + asym: '两只手一起抬,保持平衡', + }, +} as const + +export const TEMPO_TIP = '慢一点,控制好节奏' + +/** 某个动作可能播报的全部提示 */ +export function tipTextsOf(move: MoveId): string[] { + return [...Object.values(TIPS[move]), TEMPO_TIP] +} + +const TIP_COOLDOWN_MS = 3500 + +const gradeOf = (score: number): Grade => (score >= 90 ? 'perfect' : score >= 75 ? 'good' : 'ok') + +const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v)) + +/** 线性映射到分数区间 */ +const scale = (v: number, v0: number, v1: number, s0: number, s1: number) => s0 + (s1 - s0) * clamp((v - v0) / (v1 - v0), 0, 1) + +function visibleAll(pose: Pose, idx: number[]) { + return idx.every((i) => isVisible(pose, i)) +} + +/** 肩外展角:躯干侧线(髋→肩)与手臂(肩→腕)的夹角 */ +function abduction(pose: Pose, side: 'L' | 'R'): number | null { + const [sh, hip, wr] = side === 'L' ? [KP.L_SHOULDER, KP.L_HIP, KP.L_WRIST] : [KP.R_SHOULDER, KP.R_HIP, KP.R_WRIST] + if (!visibleAll(pose, [sh, hip, wr])) return null + return angleAt(pt(pose, hip), pt(pose, sh), pt(pose, wr)) +} + +function avgAbduction(pose: Pose): number | null { + const l = abduction(pose, 'L') + const r = abduction(pose, 'R') + if (l === null && r === null) return null + if (l === null) return r + if (r === null) return l + return (l + r) / 2 +} + +// ─────────────────────────── 各动作规则 ─────────────────────────── + +/** 深蹲:髋部下降比例 = 1 - 当前(踝→髋高度) / 站立时(踝→髋高度) */ +function squatRule(): Rule { + const legSpan = (pose: Pose) => { + const hip = mid(pt(pose, KP.L_HIP), pt(pose, KP.R_HIP)) + const ankle = mid(pt(pose, KP.L_ANKLE), pt(pose, KP.R_ANKLE)) + return ankle.y - hip.y + } + return { + metric(pose, ctx) { + if (!visibleAll(pose, [KP.L_HIP, KP.R_HIP, KP.L_ANKLE, KP.R_ANKLE])) return null + const span = legSpan(pose) + const b = ctx.baseline + // 站立基线:取近期最大值,并缓慢衰减以适应前后走动 + b.legSpan = Math.max(span, (b.legSpan ?? span) * 0.998) + return clamp(1 - span / b.legSpan, 0, 1) + }, + enter: 0.18, + exit: 0.08, + attempt: 0.1, + attemptTip: TIPS.squat.shallowAttempt, + amount: (v) => clamp(v / 0.34, 0, 1), + attemptJoints: [KP.L_HIP, KP.R_HIP], + minRepMs: 1200, + checkForm(pose, _ctx, flags) { + if (!visibleAll(pose, [KP.L_KNEE, KP.R_KNEE, KP.L_ANKLE, KP.R_ANKLE])) return + const kneeGap = Math.abs(pt(pose, KP.L_KNEE).x - pt(pose, KP.R_KNEE).x) + const ankleGap = Math.abs(pt(pose, KP.L_ANKLE).x - pt(pose, KP.R_ANKLE).x) + const hipDrop = legSpanDropFlag(pose) + if (hipDrop && ankleGap > 0 && kneeGap / ankleGap < 0.8) flags.add('valgus') + }, + judge(peak, trace) { + let score = scale(peak, 0.18, 0.32, 70, 100) + if (trace.flags.has('valgus')) { + score = Math.min(score, 72) + return { score, tip: TIPS.squat.valgus, joints: [KP.L_KNEE, KP.R_KNEE] } + } + if (peak < 0.24) return { score, tip: TIPS.squat.shallow, joints: [KP.L_HIP, KP.R_HIP] } + return { score } + }, + } +} + +/** checkForm 只在蹲下时判断膝内扣,用躯干长度估计是否已明显下蹲 */ +function legSpanDropFlag(pose: Pose): boolean { + const hip = mid(pt(pose, KP.L_HIP), pt(pose, KP.R_HIP)) + const ankle = mid(pt(pose, KP.L_ANKLE), pt(pose, KP.R_ANKLE)) + const torso = torsoLength(pose) + return torso > 0 && (ankle.y - hip.y) / torso < 1.25 +} + +/** 高抬腿:抬起侧膝盖比支撑侧膝盖高出多少(按躯干长度归一) */ +function marchRule(): Rule { + return { + metric(pose) { + if (!visibleAll(pose, [KP.L_KNEE, KP.R_KNEE, KP.L_SHOULDER, KP.R_SHOULDER, KP.L_HIP, KP.R_HIP])) return null + const torso = torsoLength(pose) + if (torso <= 0) return null + const diff = Math.abs(pt(pose, KP.L_KNEE).y - pt(pose, KP.R_KNEE).y) + return diff / torso + }, + enter: 0.22, + exit: 0.08, + attempt: 0.09, + attemptTip: TIPS.march.lowAttempt, + amount: (v) => clamp(v / 0.55, 0, 1), + side: (pose) => (pt(pose, KP.L_KNEE).y < pt(pose, KP.R_KNEE).y ? 'L' : 'R'), + attemptJoints: [KP.L_KNEE, KP.R_KNEE], + minRepMs: 250, + judge(peak) { + const score = scale(peak, 0.22, 0.45, 72, 100) + if (peak < 0.3) return { score, tip: TIPS.march.low, joints: [KP.L_KNEE, KP.R_KNEE] } + return { score } + }, + } +} + +/** 低冲击开合:双臂外展角;同时检查脚是否向外迈开 */ +function jackRule(): Rule { + return { + metric: (pose) => avgAbduction(pose), + enter: 120, + exit: 50, + attempt: 65, + attemptTip: TIPS.jack.armsAttempt, + amount: (v) => clamp((v - 12) / 158, 0, 1), + side(pose) { + const c = mid(pt(pose, KP.L_HIP), pt(pose, KP.R_HIP)).x + const l = Math.abs(pt(pose, KP.L_ANKLE).x - c) + const r = Math.abs(pt(pose, KP.R_ANKLE).x - c) + return l >= r ? 'L' : 'R' + }, + attemptJoints: [KP.L_WRIST, KP.R_WRIST], + minRepMs: 500, + checkForm(pose, ctx, flags) { + if (!visibleAll(pose, [KP.L_ANKLE, KP.R_ANKLE, KP.L_SHOULDER, KP.R_SHOULDER])) return + const sw = shoulderWidth(pose) + if (sw <= 0) return + const spread = Math.abs(pt(pose, KP.L_ANKLE).x - pt(pose, KP.R_ANKLE).x) / sw + ctx.baseline.maxSpread = Math.max(ctx.baseline.maxSpread ?? 0, spread) + if (spread >= 1.05) flags.add('stepped') + }, + judge(peak, trace) { + let score = scale(peak, 120, 155, 72, 100) + if (!trace.flags.has('stepped')) { + score = Math.min(score, 78) + return { score, tip: TIPS.jack.step, joints: [KP.L_ANKLE, KP.R_ANKLE] } + } + return { score } + }, + } +} + +/** 侧平举:双臂外展角,理想峰值 80~100 度;同时检查耸肩与左右不对称 */ +function raiseRule(): Rule { + const neckRatio = (pose: Pose) => { + const sh = mid(pt(pose, KP.L_SHOULDER), pt(pose, KP.R_SHOULDER)) + const sw = shoulderWidth(pose) + return sw > 0 ? (sh.y - pt(pose, KP.NOSE).y) / sw : null + } + return { + metric(pose, ctx) { + const a = avgAbduction(pose) + if (a !== null && a < 30 && isVisible(pose, KP.NOSE)) { + // 手臂放下时记录颈部基线,用于判断耸肩 + const r = neckRatio(pose) + if (r !== null) ctx.baseline.neck = ctx.baseline.neck === undefined ? r : ctx.baseline.neck * 0.9 + r * 0.1 + } + return a + }, + enter: 68, + exit: 30, + attempt: 42, + attemptTip: TIPS.raise.lowAttempt, + amount: (v) => clamp((v - 10) / 78, 0, 1), + attemptJoints: [KP.L_WRIST, KP.R_WRIST], + minRepMs: 1200, + checkForm(pose, ctx, flags) { + const l = abduction(pose, 'L') + const r = abduction(pose, 'R') + if (l !== null && r !== null && Math.abs(l - r) > 28) flags.add('asym') + const base = ctx.baseline.neck + const cur = isVisible(pose, KP.NOSE) ? neckRatio(pose) : null + if (base && cur !== null && cur < base * 0.8) flags.add('shrug') + }, + judge(peak, trace) { + if (trace.flags.has('shrug')) return { score: 70, tip: TIPS.raise.shrug, joints: [KP.L_SHOULDER, KP.R_SHOULDER] } + if (peak > 112) return { score: 75, tip: TIPS.raise.high, joints: [KP.L_WRIST, KP.R_WRIST] } + if (trace.flags.has('asym')) return { score: 78, tip: TIPS.raise.asym, joints: [KP.L_WRIST, KP.R_WRIST] } + const score = peak >= 80 ? 100 : scale(peak, 68, 80, 75, 95) + return { score } + }, + } +} + +const RULES: Record Rule> = { + squat: squatRule, + march: marchRule, + jack: jackRule, + raise: raiseRule, +} + +// ─────────────────────────── 通用状态机 ─────────────────────────── + +export function createCounter(move: MoveId): RepCounter { + const rule = RULES[move]() + let ctx: Ctx = { baseline: {} } + let count = 0 + let active: RepTrace | null = null + let attemptPeak = 0 + let lastRepEnd = 0 + let live: Amount = { amount: 0, side: 'L' } + const goal = rule.amount(rule.enter) + const lastTipAt = new Map() + + const allowTip = (text: string, t: number) => { + const last = lastTipAt.get(text) ?? -Infinity + if (t - last < TIP_COOLDOWN_MS) return false + lastTipAt.set(text, t) + return true + } + + return { + move, + get count() { + return count + }, + get live() { + return live + }, + goal, + reset() { + ctx = { baseline: {} } + count = 0 + active = null + attemptPeak = 0 + lastRepEnd = 0 + live = { amount: 0, side: 'L' } + lastTipAt.clear() + }, + update(pose) { + const events: CoachEvent[] = [] + const v = rule.metric(pose, ctx) + if (v === null) return events + live = { amount: rule.amount(v), side: rule.side?.(pose) ?? 'L' } + + if (!active) { + if (v >= rule.enter) { + active = { peak: v, startT: pose.t, flags: new Set() } + attemptPeak = 0 + } else if (v >= rule.attempt) { + attemptPeak = Math.max(attemptPeak, v) + } else if (attemptPeak > 0 && v <= rule.exit) { + // 做了动作但幅度不够,回到起始位 + if (allowTip(rule.attemptTip, pose.t)) events.push({ type: 'tip', text: rule.attemptTip, joints: rule.attemptJoints }) + attemptPeak = 0 + } + } + + if (active) { + active.peak = Math.max(active.peak, v) + rule.checkForm?.(pose, ctx, active.flags) + if (v <= rule.exit) { + count += 1 + const judged = rule.judge(active.peak, active) + let { tip, joints } = judged + const duration = pose.t - (lastRepEnd || active.startT) + if (!tip && rule.minRepMs && lastRepEnd && duration < rule.minRepMs) { + tip = TEMPO_TIP + joints = undefined + } + const score = Math.round(judged.score) + const showTip = tip && allowTip(tip, pose.t) ? tip : undefined + events.push({ type: 'rep', count, score, grade: gradeOf(score), tip: showTip, joints: showTip ? joints : undefined }) + lastRepEnd = pose.t + active = null + } + } + return events + }, + } +} diff --git a/TUICallKit-Vue3/training/pose/exercises.ts b/TUICallKit-Vue3/training/pose/exercises.ts new file mode 100644 index 000000000..d131a43d4 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/exercises.ts @@ -0,0 +1,133 @@ +/** + * 跟练动作的展示文案与训练参数 + * + * 纯数据,小程序页面与 Remotion 示范视频共用。 + */ +import type { MoveId } from './motions.ts' + +export type ExerciseInfo = { + id: MoveId + name: string + /** 一句话卖点 */ + tagline: string + /** 锻炼部位 */ + targets: string[] + /** 强度:1 轻松 ~ 3 较累 */ + level: 1 | 2 | 3 + /** 默认目标次数 */ + targetReps: number + /** 目标次数可调范围与步长 */ + repRange: [number, number] + repStep: number + /** 估算代谢当量(净 MET,已扣除静息 1 MET) */ + netMet: number + /** 动作要领(示范视频逐条出现) */ + keyPoints: string[] + /** 常见错误 → 正确做法 */ + mistake: { wrong: string; right: string } + /** 主题色 */ + color: string + /** 示范视频地址;上传 COS 后填入,留空时正式环境不显示“完整示范”按钮 */ + videoUrl: string +} + +export const EXERCISES: Record = { + squat: { + id: 'squat', + name: '徒手深蹲', + tagline: '练大腿和臀部,大肌群最耗糖', + targets: ['大腿', '臀部', '核心'], + level: 2, + targetReps: 12, + repRange: [6, 30], + repStep: 2, + netMet: 4, + keyPoints: ['双脚与肩同宽,脚尖微微向外', '屁股向后坐,像要坐椅子', '膝盖对准脚尖,不要内扣', '蹲到大腿接近水平,脚跟发力站起'], + mistake: { wrong: '膝盖向内扣', right: '膝盖朝脚尖方向打开' }, + color: '#10b981', + videoUrl: 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/yundong/video/squat.mp4', + }, + march: { + id: 'march', + name: '原地高抬腿', + tagline: '不出门的有氧,饭后 10 分钟正合适', + targets: ['下肢', '心肺'], + level: 1, + targetReps: 40, + repRange: [20, 100], + repStep: 10, + netMet: 3, + keyPoints: ['上身挺直,目视前方', '左右交替抬膝', '膝盖尽量抬到腰的高度', '手臂自然前后摆动'], + mistake: { wrong: '膝盖只抬一点点', right: '大腿抬到接近水平' }, + color: '#f59e0b', + videoUrl: 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/yundong/video/march.mp4', + }, + jack: { + id: 'jack', + name: '低冲击开合', + tagline: '不用跳,对膝盖友好的全身燃脂', + targets: ['全身', '肩部', '心肺'], + level: 2, + targetReps: 20, + repRange: [8, 40], + repStep: 4, + netMet: 3.5, + keyPoints: ['一只脚向侧面迈出一步', '同时双手从两侧举过头顶', '收脚时手臂放下', '左右交替,保持节奏'], + mistake: { wrong: '手臂只抬到胸口', right: '双手完全举过头顶' }, + color: '#6366f1', + videoUrl: 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/yundong/video/jack.mp4', + }, + raise: { + id: 'raise', + name: '站姿侧平举', + tagline: '坐久了肩颈僵?练练肩膀', + targets: ['肩部', '上背'], + level: 1, + targetReps: 15, + repRange: [9, 30], + repStep: 3, + netMet: 2, + keyPoints: ['双脚与肩同宽,微收腹', '手臂伸直,从两侧慢慢抬起', '抬到与肩同高停一秒', '慢慢放下,肩膀不要耸起'], + mistake: { wrong: '耸肩、手臂抬过头', right: '肩膀下沉,抬到与肩平齐' }, + color: '#0ea5e9', + videoUrl: 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/yundong/video/raise.mp4', + }, +} + +export const EXERCISE_ORDER: MoveId[] = ['squat', 'march', 'jack', 'raise'] + +/** + * 示范视频地址:线上用 COS 地址(上传后填到 EXERCISES[x].videoUrl); + * 开发环境回退到本机 training-videos 静态服务(npm run serve,端口 5179), + * 真机调试时把 DEV_VIDEO_HOST 改成电脑的局域网 IP。 + */ +export const DEV_VIDEO_HOST = 'http://127.0.0.1:5179' + +export function videoUrlOf(ex: ExerciseInfo, isDev: boolean): string { + if (ex.videoUrl) return ex.videoUrl + return isDev ? `${DEV_VIDEO_HOST}/${ex.id}.mp4` : '' +} + +/** + * 教练高清帧的 COS 目录:把 training-videos/out/coach 整个上传后填入, + * 例如 'https://xxx.cos.ap-guangzhou.myqcloud.com/training/coach'(末尾不带斜杠)。 + * 留空只用包内帧;开发环境留空时读本机 npm run serve(真机访问不到会自动退回包内帧)。 + */ +export const COACH_CDN_BASE = 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/yundong/coach' + +export function coachCdnBase(isDev: boolean): string { + if (COACH_CDN_BASE) return COACH_CDN_BASE + return isDev ? `${DEV_VIDEO_HOST}/coach` : '' +} + +/** + * 跟练背景音乐:training-videos 里 npm run bgm 生成的 out/music/calm.mp3 上传到 COS 后填入; + * 换音乐时换个文件名(避免缓存)。留空时开发环境读本机 npm run serve,正式环境不放音乐; + * 地址打不开时自动不放、隐藏开关,不影响训练。 + */ +export const BGM_URL = 'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/uploads/file/20260930/20260930085356d4f920988.mp3' + +export function bgmUrlOf(isDev: boolean): string { + if (BGM_URL) return BGM_URL + return isDev ? `${DEV_VIDEO_HOST}/music/calm.mp3` : '' +} diff --git a/TUICallKit-Vue3/training/pose/history.ts b/TUICallKit-Vue3/training/pose/history.ts new file mode 100644 index 000000000..628a77df9 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/history.ts @@ -0,0 +1,92 @@ +/** + * 跟练记录(本地存储) + * + * 只保存汇总数据,不含任何画面。纯函数部分可单测,存取通过 storage 适配器注入。 + */ +import type { MoveId } from './motions.ts' +import type { SessionSummary } from './session.ts' + +export type HistoryRecord = { + move: MoveId + reps: number + avgScore: number + durationSec: number + kcal: number + /** 本地日期 YYYY-MM-DD */ + day: string + at: number +} + +export type Storage = { + get: (key: string) => unknown + set: (key: string, value: unknown) => void +} + +const KEY = 'pose_coach_history_v1' +const MAX_RECORDS = 200 + +export function dayOf(ts: number): string { + const d = new Date(ts) + const mm = String(d.getMonth() + 1).padStart(2, '0') + const dd = String(d.getDate()).padStart(2, '0') + return `${d.getFullYear()}-${mm}-${dd}` +} + +export function loadHistory(store: Storage): HistoryRecord[] { + try { + const raw = store.get(KEY) + return Array.isArray(raw) ? (raw as HistoryRecord[]) : [] + } catch (e) { + return [] + } +} + +export function appendHistory(store: Storage, s: SessionSummary, now = Date.now()): HistoryRecord[] { + if (s.reps <= 0) return loadHistory(store) + const rec: HistoryRecord = { + move: s.move, + reps: s.reps, + avgScore: s.avgScore, + durationSec: s.durationSec, + kcal: s.kcal, + day: dayOf(now), + at: now, + } + const next = [rec, ...loadHistory(store)].slice(0, MAX_RECORDS) + try { + store.set(KEY, next) + } catch (e) {} + return next +} + +export type HistoryStats = { + todayReps: number + todayMinutes: number + /** 连续训练天数(含今天;今天没练则从昨天算起) */ + streak: number + total: number +} + +export function statsOf(records: HistoryRecord[], now = Date.now()): HistoryStats { + const today = dayOf(now) + const todays = records.filter((r) => r.day === today) + const days = new Set(records.map((r) => r.day)) + let streak = 0 + let cursor = days.has(today) ? now : now - 86400000 + while (days.has(dayOf(cursor))) { + streak += 1 + cursor -= 86400000 + } + return { + todayReps: todays.reduce((a, r) => a + r.reps, 0), + todayMinutes: Math.round(todays.reduce((a, r) => a + r.durationSec, 0) / 60), + streak, + total: records.length, + } +} + +/** uni 本地存储适配器 */ +export const uniStorage: Storage = { + get: (k) => uni.getStorageSync(k), + set: (k, v) => uni.setStorageSync(k, v), +} diff --git a/TUICallKit-Vue3/training/pose/keypoints.ts b/TUICallKit-Vue3/training/pose/keypoints.ts new file mode 100644 index 000000000..df73c502d --- /dev/null +++ b/TUICallKit-Vue3/training/pose/keypoints.ts @@ -0,0 +1,212 @@ +/** + * 人体关键点定义与几何工具 + * + * 点位遵循微信 VisionKit body 检测的 23 点定义(基础库 >= 2.28.0): + * 0 鼻子 1/2 左/右眼 3/4 左/右耳 + * 5/6 左/右肩 7/8 左/右肘 9/10 左/右腕 + * 11/12 左/右髋 13/14 左/右膝 15/16 左/右踝 + * 17/18 左/右手尖 19/20 左/右脚外侧 21/22 左/右脚内侧 + * “左/右”指被拍摄者自身的左右。 + * + * 本文件不依赖 uni / wx,可在 Node 单测与 Remotion 中直接复用。 + */ + +export type Point = { x: number; y: number } + +/** 一帧姿态:23 个点,坐标归一化到 [0,1],score 为该点置信度(0~1) */ +export type Pose = { + points: Point[] + scores: number[] + /** 采集时间戳(ms) */ + t: number + /** 画面宽高比(宽/高),用于把 x 换算到与 y 同一尺度;缺省为 1 */ + aspect?: number +} + +/** 取某个点,并把 x 换算到与 y 相同的尺度(角度、距离计算必须用它) */ +export function pt(pose: Pose, idx: number): Point { + const p = pose.points[idx] + return { x: p.x * (pose.aspect ?? 1), y: p.y } +} + +export const KP = { + NOSE: 0, + L_EYE: 1, + R_EYE: 2, + L_EAR: 3, + R_EAR: 4, + L_SHOULDER: 5, + R_SHOULDER: 6, + L_ELBOW: 7, + R_ELBOW: 8, + L_WRIST: 9, + R_WRIST: 10, + L_HIP: 11, + R_HIP: 12, + L_KNEE: 13, + R_KNEE: 14, + L_ANKLE: 15, + R_ANKLE: 16, + L_HAND: 17, + R_HAND: 18, + L_FOOT_OUT: 19, + R_FOOT_OUT: 20, + L_FOOT_IN: 21, + R_FOOT_IN: 22, +} as const + +export const KEYPOINT_COUNT = 23 + +/** 画骨架用的连线(左右成对,便于左右分色) */ +export const BONES: Array<[number, number]> = [ + [KP.L_SHOULDER, KP.R_SHOULDER], + [KP.L_HIP, KP.R_HIP], + [KP.L_SHOULDER, KP.L_HIP], + [KP.R_SHOULDER, KP.R_HIP], + [KP.L_SHOULDER, KP.L_ELBOW], + [KP.L_ELBOW, KP.L_WRIST], + [KP.L_WRIST, KP.L_HAND], + [KP.R_SHOULDER, KP.R_ELBOW], + [KP.R_ELBOW, KP.R_WRIST], + [KP.R_WRIST, KP.R_HAND], + [KP.L_HIP, KP.L_KNEE], + [KP.L_KNEE, KP.L_ANKLE], + [KP.L_ANKLE, KP.L_FOOT_OUT], + [KP.L_ANKLE, KP.L_FOOT_IN], + [KP.R_HIP, KP.R_KNEE], + [KP.R_KNEE, KP.R_ANKLE], + [KP.R_ANKLE, KP.R_FOOT_OUT], + [KP.R_ANKLE, KP.R_FOOT_IN], +] + +/** 判定“全身入镜”需要看得到的点 */ +export const FULL_BODY_POINTS = [ + KP.NOSE, + KP.L_SHOULDER, + KP.R_SHOULDER, + KP.L_HIP, + KP.R_HIP, + KP.L_KNEE, + KP.R_KNEE, + KP.L_ANKLE, + KP.R_ANKLE, +] + +export const MIN_POINT_SCORE = 0.3 + +export function dist(a: Point, b: Point): number { + return Math.hypot(a.x - b.x, a.y - b.y) +} + +export function mid(a: Point, b: Point): Point { + return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 } +} + +/** 以 b 为顶点的夹角 ∠abc,单位度,范围 [0,180] */ +export function angleAt(a: Point, b: Point, c: Point): number { + const v1x = a.x - b.x + const v1y = a.y - b.y + const v2x = c.x - b.x + const v2y = c.y - b.y + const n = Math.hypot(v1x, v1y) * Math.hypot(v2x, v2y) + if (n === 0) return 180 + const cos = Math.min(1, Math.max(-1, (v1x * v2x + v1y * v2y) / n)) + return (Math.acos(cos) * 180) / Math.PI +} + +export function isVisible(pose: Pose, idx: number, min = MIN_POINT_SCORE): boolean { + return (pose.scores[idx] ?? 0) >= min +} + +/** 肩宽,作为身体尺度基准(对远近不敏感) */ +export function shoulderWidth(pose: Pose): number { + return dist(pt(pose, KP.L_SHOULDER), pt(pose, KP.R_SHOULDER)) +} + +/** 躯干长度:肩中点到髋中点 */ +export function torsoLength(pose: Pose): number { + const s = mid(pt(pose, KP.L_SHOULDER), pt(pose, KP.R_SHOULDER)) + const h = mid(pt(pose, KP.L_HIP), pt(pose, KP.R_HIP)) + return dist(s, h) +} + +export type FramingIssue = 'none' | 'no_body' | 'too_close' | 'too_far' | 'off_center' | 'partial' + +/** + * 站位检查:是否全身入镜、远近是否合适、是否居中。 + * 返回第一个需要纠正的问题。 + */ +export function checkFraming(pose: Pose | null): FramingIssue { + if (!pose) return 'no_body' + const missing = FULL_BODY_POINTS.filter((i) => !isVisible(pose, i)) + if (missing.length >= FULL_BODY_POINTS.length - 2) return 'no_body' + + const top = pose.points[KP.NOSE].y + const bottom = Math.max(pose.points[KP.L_ANKLE].y, pose.points[KP.R_ANKLE].y) + const height = bottom - top + + const anklesMissing = !isVisible(pose, KP.L_ANKLE) || !isVisible(pose, KP.R_ANKLE) + if (anklesMissing || bottom > 0.98 || top < 0.02) return 'too_close' + if (missing.length > 0) return 'partial' + if (height < 0.35) return 'too_far' + + const cx = (pose.points[KP.L_HIP].x + pose.points[KP.R_HIP].x) / 2 + if (cx < 0.25 || cx > 0.75) return 'off_center' + return 'none' +} + +export const FRAMING_TIPS: Record = { + none: '站位很好,保持住', + no_body: '请站到镜头前', + too_close: '请往后退,让头和脚都进入画面', + too_far: '请再往前走一点', + off_center: '请站到画面中间', + partial: '请让全身都进入画面', +} + +/** + * 指数平滑:抑制关键点抖动。alpha 越大越跟手,越小越稳。 + * 置信度低的点保留上一帧位置,避免骨架乱跳。 + */ +export function smoothPose(prev: Pose | null, next: Pose, alpha = 0.55): Pose { + if (!prev) return next + const points = next.points.map((p, i) => { + const q = prev.points[i] + if ((next.scores[i] ?? 0) < MIN_POINT_SCORE) return q + return { x: q.x + (p.x - q.x) * alpha, y: q.y + (p.y - q.y) * alpha } + }) + const scores = next.scores.map((s, i) => Math.max(s, (prev.scores[i] ?? 0) * 0.7)) + return { points, scores, t: next.t, aspect: next.aspect } +} + +/** 水平镜像(前置摄像头预览是镜像的) */ +export function mirrorPose(pose: Pose): Pose { + return { ...pose, points: pose.points.map((p) => ({ x: 1 - p.x, y: p.y })) } +} + +type RawPoint = { x: number; y: number } | [number, number] + +/** + * 把 VisionKit 的 anchor 转成归一化 Pose。 + * 官方示例里 points 已是 0~1 的 {x,y};文档描述为“图像中的坐标”, + * 这里两种都兼容:发现坐标 > 1.5 即按像素处理。 + */ +export function anchorToPose( + anchor: { points?: RawPoint[]; confidence?: number[]; score?: number }, + frameWidth: number, + frameHeight: number, + t: number, +): Pose | null { + const raw = anchor?.points + if (!raw || raw.length < 17) return null + const pts: Point[] = raw.map((p) => (Array.isArray(p) ? { x: p[0], y: p[1] } : { x: p.x, y: p.y })) + const isPixel = pts.some((p) => p.x > 1.5 || p.y > 1.5) + const points = pts.map((p) => (isPixel ? { x: p.x / frameWidth, y: p.y / frameHeight } : p)) + while (points.length < KEYPOINT_COUNT) points.push({ x: 0, y: 0 }) + const fallback = typeof anchor.score === 'number' ? anchor.score : 1 + const scores = points.map((_, i) => { + const c = anchor.confidence?.[i] + return typeof c === 'number' ? c : i < raw.length ? fallback : 0 + }) + return { points, scores, t, aspect: frameWidth / frameHeight } +} diff --git a/TUICallKit-Vue3/training/pose/motions.ts b/TUICallKit-Vue3/training/pose/motions.ts new file mode 100644 index 000000000..7079bfc85 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/motions.ts @@ -0,0 +1,341 @@ +/** + * 示范动作骨架动画(正面视角) + * + * 用途: + * 1. 示范节奏:amountAt 决定教练逐帧动画与示范视频的节拍 + * 2. 单测与开发者工具模拟:用示范动作合成姿态,验证识别计数逻辑 + * + * 坐标系与 VisionKit 一致:x/y 归一化到 [0,1],y 向下; + * 被拍摄者的“左侧”在画面右侧(未镜像的正面视角)。 + */ +import { KEYPOINT_COUNT, type Point, type Pose } from './keypoints.ts' + +export type MoveId = 'squat' | 'march' | 'jack' | 'raise' + +type ArmParams = { + /** 外展角:0 = 手臂自然下垂,90 = 侧平举,180 = 举过头顶 */ + abd: number + /** 前平举程度 0~1(正面看手臂“缩短”) */ + fwd: number +} + +type LegParams = { + /** 抬膝程度 0~1(1 = 大腿抬平) */ + lift: number + /** 脚向外侧迈出的距离 */ + step: number +} + +type BodyParams = { + /** 下蹲深度 0~1(1 ≈ 大腿与地面平行) */ + squat: number + /** 整体上下起伏(踏步时的弹动) */ + bounce: number + left: { arm: ArmParams; leg: LegParams } + right: { arm: ArmParams; leg: LegParams } +} + +const CX = 0.5 +const GROUND = 0.93 +const THIGH = 0.2 +const SHIN = 0.2 +const TORSO = 0.27 +const HIP_HALF = 0.058 +const SHOULDER_HALF = 0.098 +const UPPER_ARM = 0.15 +const FOREARM = 0.135 +const HAND = 0.045 + +const rad = (d: number) => (d * Math.PI) / 180 + +function buildArm(shoulder: Point, side: 1 | -1, arm: ArmParams) { + // 前举时手臂在画面上投影变短 + const shrink = 1 - 0.82 * arm.fwd + const a = rad(arm.abd) + const dx = side * Math.sin(a) + const dy = Math.cos(a) + const elbow = { x: shoulder.x + dx * UPPER_ARM * shrink, y: shoulder.y + dy * UPPER_ARM * shrink } + const wrist = { x: elbow.x + dx * FOREARM * shrink, y: elbow.y + dy * FOREARM * shrink } + const hand = { x: wrist.x + dx * HAND * shrink, y: wrist.y + dy * HAND * shrink } + return { elbow, wrist, hand } +} + +function buildLeg(hip: Point, side: 1 | -1, leg: LegParams, squat: number) { + const ankleX = hip.x + side * (0.008 + leg.step) + if (leg.lift > 0) { + // 抬膝:大腿向前抬起,正面投影变短;小腿自然下垂 + const thighProj = THIGH * Math.cos(rad(leg.lift * 90)) + const knee = { x: hip.x + side * 0.004, y: hip.y + thighProj } + const ankle = { x: ankleX, y: knee.y + SHIN * 0.92 } + return { knee, ankle } + } + const ankle = { x: ankleX, y: GROUND } + // 下蹲时膝盖向外打开(膝盖对准脚尖) + const knee = { + x: (hip.x + ankle.x) / 2 + side * 0.035 * squat, + y: hip.y + (ankle.y - hip.y) * (0.5 - 0.06 * squat), + } + return { knee, ankle } +} + +export function buildPose(p: BodyParams): Point[] { + const pts: Point[] = new Array(KEYPOINT_COUNT) + // 下蹲:髋部下降,大腿越接近水平,正面投影越短 + const hipY = GROUND - SHIN - THIGH * Math.cos(rad(p.squat * 80)) - p.bounce + const lHip = { x: CX + HIP_HALF, y: hipY } + const rHip = { x: CX - HIP_HALF, y: hipY } + // 下蹲时上身前倾,正面看躯干略微变短 + const torso = TORSO * (1 - 0.14 * p.squat) + const shoulderY = hipY - torso + const lSh = { x: CX + SHOULDER_HALF, y: shoulderY } + const rSh = { x: CX - SHOULDER_HALF, y: shoulderY } + const noseY = shoulderY - 0.1 + + pts[0] = { x: CX, y: noseY } + pts[1] = { x: CX + 0.018, y: noseY - 0.018 } + pts[2] = { x: CX - 0.018, y: noseY - 0.018 } + pts[3] = { x: CX + 0.042, y: noseY - 0.008 } + pts[4] = { x: CX - 0.042, y: noseY - 0.008 } + pts[5] = lSh + pts[6] = rSh + pts[11] = lHip + pts[12] = rHip + + const la = buildArm(lSh, 1, p.left.arm) + const ra = buildArm(rSh, -1, p.right.arm) + pts[7] = la.elbow + pts[9] = la.wrist + pts[17] = la.hand + pts[8] = ra.elbow + pts[10] = ra.wrist + pts[18] = ra.hand + + const ll = buildLeg(lHip, 1, p.left.leg, p.squat) + const rl = buildLeg(rHip, -1, p.right.leg, p.squat) + pts[13] = ll.knee + pts[15] = ll.ankle + pts[14] = rl.knee + pts[16] = rl.ankle + pts[19] = { x: ll.ankle.x + 0.035, y: ll.ankle.y + 0.022 } + pts[21] = { x: ll.ankle.x - 0.008, y: ll.ankle.y + 0.026 } + pts[20] = { x: rl.ankle.x - 0.035, y: rl.ankle.y + 0.022 } + pts[22] = { x: rl.ankle.x + 0.008, y: rl.ankle.y + 0.026 } + return pts +} + +const ease = (t: number) => 0.5 - 0.5 * Math.cos(Math.PI * t) +const wrap = (p: number) => ((p % 1) + 1) % 1 + +/** 平滑的 0→1→0 往返曲线,带顶部/底部停顿 */ +function pingPong(phase: number, holdTop = 0.1, holdBottom = 0.1): number { + const moving = (1 - holdTop - holdBottom) / 2 + const p = wrap(phase) + if (p < moving) return ease(p / moving) + if (p < moving + holdTop) return 1 + if (p < moving * 2 + holdTop) return 1 - ease((p - moving - holdTop) / moving) + return 0 +} + +export type Side = 'L' | 'R' + +/** 动作幅度:0 = 起始姿势,1 = 标准到位;side 用于左右交替的动作 */ +export type Amount = { amount: number; side: Side } + +export type MotionSpec = { + id: MoveId + /** 一个完整动作周期(秒),即示范节奏 */ + cycleSeconds: number + /** 一个周期计几次(踏步、开合左右各算一次) */ + repsPerCycle: number + /** 相位 → 动作幅度 */ + amountAt: (phase: number) => Amount + /** 动作幅度 → 骨架(镜像模式直接用用户的实际幅度驱动) */ + poseFor: (a: Amount) => Point[] + /** 相位 → 骨架 */ + poseAt: (phase: number) => Point[] + /** 相位 → 当前阶段提示(示范字幕) */ + phaseLabel: (phase: number) => string +} + +type SpecInput = Omit + +const spec = (s: SpecInput): MotionSpec => ({ ...s, poseAt: (phase) => s.poseFor(s.amountAt(phase)) }) + +/** 左右交替动作:前半周期左侧,后半周期右侧 */ +function alternating(phase: number, shape: (half: number) => number): Amount { + const p = wrap(phase) + const side: Side = p < 0.5 ? 'L' : 'R' + const half = p < 0.5 ? p / 0.5 : (p - 0.5) / 0.5 + return { amount: shape(half), side } +} + +export const MOTIONS: Record = { + squat: spec({ + id: 'squat', + cycleSeconds: 3.2, + repsPerCycle: 1, + amountAt: (phase) => ({ amount: pingPong(phase, 0.12, 0.16), side: 'L' }), + poseFor({ amount: d }) { + const arm = { abd: 8 + 4 * d, fwd: d } + return buildPose({ + squat: d * 0.95, + bounce: 0, + left: { arm, leg: { lift: 0, step: 0.045 } }, + right: { arm, leg: { lift: 0, step: 0.045 } }, + }) + }, + phaseLabel(phase) { + const p = wrap(phase) + if (p < 0.36) return '吸气 · 屁股向后坐' + if (p < 0.48) return '停一下 · 膝盖对准脚尖' + if (p < 0.84) return '呼气 · 脚跟发力站起' + return '站直 · 准备下一次' + }, + }), + march: spec({ + id: 'march', + cycleSeconds: 1.6, + repsPerCycle: 2, + amountAt: (phase) => alternating(phase, (h) => Math.sin(Math.PI * h)), + poseFor({ amount: a, side }) { + // 抬起一侧膝盖,对侧手臂前摆 + const l = side === 'L' ? a : 0 + const r = side === 'R' ? a : 0 + return buildPose({ + squat: 0, + bounce: 0.012 * a, + left: { arm: { abd: 10, fwd: 0.75 * r }, leg: { lift: 0.85 * l, step: 0 } }, + right: { arm: { abd: 10, fwd: 0.75 * l }, leg: { lift: 0.85 * r, step: 0 } }, + }) + }, + phaseLabel(phase) { + return wrap(phase) < 0.5 ? '抬左膝 · 右手前摆' : '抬右膝 · 左手前摆' + }, + }), + jack: spec({ + id: 'jack', + cycleSeconds: 2.4, + repsPerCycle: 2, + amountAt: (phase) => alternating(phase, (h) => pingPong(h, 0.14, 0.14)), + poseFor({ amount: o, side }) { + // 低冲击版:不跳,左右交替向侧面迈步,同时双臂上举 + const arm = { abd: 12 + 158 * o, fwd: 0 } + return buildPose({ + squat: 0.06 * o, + bounce: 0, + left: { arm, leg: { lift: 0, step: side === 'L' ? 0.11 * o : 0 } }, + right: { arm, leg: { lift: 0, step: side === 'R' ? 0.11 * o : 0 } }, + }) + }, + phaseLabel(phase) { + return wrap(phase) < 0.5 ? '左脚向外迈 · 双手举过头' : '右脚向外迈 · 双手举过头' + }, + }), + raise: spec({ + id: 'raise', + cycleSeconds: 3, + repsPerCycle: 1, + amountAt: (phase) => ({ amount: pingPong(phase, 0.14, 0.14), side: 'L' }), + poseFor({ amount: o }) { + const arm = { abd: 10 + 78 * o, fwd: 0 } + return buildPose({ + squat: 0, + bounce: 0, + left: { arm, leg: { lift: 0, step: 0.035 } }, + right: { arm, leg: { lift: 0, step: 0.035 } }, + }) + }, + phaseLabel(phase) { + const p = wrap(phase) + if (p < 0.36) return '呼气 · 手臂向两侧抬起' + if (p < 0.5) return '停一下 · 与肩同高' + if (p < 0.86) return '吸气 · 慢慢放下' + return '放松 · 肩膀下沉' + }, + }), +} + +/** + * 常见错误动作(示范视频“错误 vs 正确”对比用,也用于单测验证纠错提示) + */ +export const MISTAKE_POSES: Record Point[]> = { + // 膝盖内扣:下蹲时膝盖向身体中线靠拢 + squat(phase) { + const d = pingPong(phase, 0.12, 0.16) + const pts = MOTIONS.squat.poseAt(phase) + const pull = 0.075 * d + pts[13] = { x: pts[13].x - pull, y: pts[13].y } + pts[14] = { x: pts[14].x + pull, y: pts[14].y } + return pts + }, + // 膝盖抬得太低 + march(phase) { + const p = wrap(phase) + const l = p < 0.5 ? Math.sin(Math.PI * (p / 0.5)) : 0 + const r = p >= 0.5 ? Math.sin(Math.PI * ((p - 0.5) / 0.5)) : 0 + return buildPose({ + squat: 0, + bounce: 0.006 * Math.max(l, r), + left: { arm: { abd: 8, fwd: 0.2 * r }, leg: { lift: 0.32 * l, step: 0 } }, + right: { arm: { abd: 8, fwd: 0.2 * l }, leg: { lift: 0.32 * r, step: 0 } }, + }) + }, + // 手臂只抬到胸口 + jack(phase) { + const p = wrap(phase) + const half = p < 0.5 ? p / 0.5 : (p - 0.5) / 0.5 + const o = pingPong(half, 0.14, 0.14) + const arm = { abd: 12 + 70 * o, fwd: 0 } + const leftOut = p < 0.5 + return buildPose({ + squat: 0, + bounce: 0, + left: { arm, leg: { lift: 0, step: leftOut ? 0.05 * o : 0 } }, + right: { arm, leg: { lift: 0, step: leftOut ? 0 : 0.05 * o } }, + }) + }, + // 耸肩且手臂抬得过高 + raise(phase) { + const o = pingPong(phase, 0.14, 0.14) + const arm = { abd: 10 + 125 * o, fwd: 0 } + const pts = buildPose({ + squat: 0, + bounce: 0, + left: { arm, leg: { lift: 0, step: 0.035 } }, + right: { arm, leg: { lift: 0, step: 0.035 } }, + }) + const shrug = 0.03 * o + for (const i of [5, 6, 7, 8, 9, 10, 17, 18]) pts[i] = { x: pts[i].x, y: pts[i].y - shrug } + return pts + }, +} + +/** 把示范动作按时间采样成 Pose 序列(单测用) */ +export function sampleMotion(id: MoveId, seconds: number, fps = 12, startMs = 0): Pose[] { + const spec = MOTIONS[id] + const frames: Pose[] = [] + const n = Math.round(seconds * fps) + for (let i = 0; i < n; i++) { + const tSec = i / fps + const points = spec.poseAt(tSec / spec.cycleSeconds) + frames.push({ points, scores: points.map(() => 0.9), t: startMs + tSec * 1000 }) + } + return frames +} + +/** + * 示范动作“完成一次”的检测:幅度到过顶点、再回落到起始附近,算完成一次。 + * 领练模式按教练动画计数,报数和画面同步(按时钟计数会比教练做完晚半拍)。 + */ +export function createRepEdge(peak = 0.9, exit = 0.25) { + let peaked = false + return (amount: number): boolean => { + if (amount >= peak) { + peaked = true + } else if (peaked && amount <= exit) { + peaked = false + return true + } + return false + } +} diff --git a/TUICallKit-Vue3/training/pose/session.ts b/TUICallKit-Vue3/training/pose/session.ts new file mode 100644 index 000000000..5355988be --- /dev/null +++ b/TUICallKit-Vue3/training/pose/session.ts @@ -0,0 +1,232 @@ +/** + * 一次跟练的状态机(纯逻辑,可单测) + * + * framing ──站好并保持 1.5s──▶ countdown ──3s──▶ training ──达到目标──▶ done + * │ ▲ + * 离开画面 >1.5s ▼ │ 回到画面 + * lost + * 任意训练阶段可 pause / resume,finish 提前结束。 + */ +import { checkFraming, type FramingIssue, type Pose } from './keypoints.ts' +import { createCounter, type CoachEvent, type Grade, type RepCounter } from './counters.ts' +import type { Amount, MoveId } from './motions.ts' + +export type SessionPhase = 'framing' | 'countdown' | 'training' | 'lost' | 'paused' | 'done' + +export type SessionEvent = + | { type: 'phase'; phase: SessionPhase } + | { type: 'countdown'; n: number } + | { type: 'combo'; combo: number } + | CoachEvent + +export type RepRecord = { score: number; grade: Grade; tip?: string } + +export const FRAMING_HOLD_MS = 1500 +export const COUNTDOWN_MS = 3000 +export const LOST_AFTER_MS = 1500 +/** 连击:连续得分不低于该值 */ +export const COMBO_SCORE = 80 +const COMBO_MILESTONES = [3, 5, 10, 15, 20, 30, 40, 50] + +export class TrainingSession { + readonly move: MoveId + readonly targetReps: number + readonly counter: RepCounter + + phase: SessionPhase = 'framing' + framingIssue: FramingIssue = 'no_body' + /** 站位保持进度 0~1 */ + holdProgress = 0 + /** 倒计时剩余整数秒 */ + countdown = 0 + combo = 0 + bestCombo = 0 + reps: RepRecord[] = [] + /** 有效训练时长(不含站位、倒计时、离开画面与暂停) */ + activeMs = 0 + + /** 领练模式(无识别)按节奏累计的次数 */ + private guidedReps = 0 + private readonly detectLost: boolean + private okSince = 0 + private countdownStart = 0 + private lastSeen = 0 + private lastTick = 0 + private beforePause: SessionPhase = 'training' + + constructor(move: MoveId, targetReps: number, opts: { detectLost?: boolean } = {}) { + this.move = move + this.targetReps = targetReps + this.counter = createCounter(move) + this.detectLost = opts.detectLost ?? true + } + + get count() { + return this.counter.count + this.guidedReps + } + + get live(): Amount { + return this.counter.live + } + + get progress() { + return Math.min(1, this.count / this.targetReps) + } + + /** 每收到一帧检测结果调用(pose 为 null 表示画面里没人) */ + feed(pose: Pose | null, now: number): SessionEvent[] { + const events = this.tick(now) + const issue = checkFraming(pose) + this.framingIssue = issue + if (issue !== 'no_body') this.lastSeen = now + + switch (this.phase) { + case 'framing': + if (issue === 'none') { + if (!this.okSince) this.okSince = now + this.holdProgress = Math.min(1, (now - this.okSince) / FRAMING_HOLD_MS) + if (now - this.okSince >= FRAMING_HOLD_MS) { + this.countdownStart = now + this.countdown = 3 + events.push(...this.setPhase('countdown'), { type: 'countdown', n: 3 }) + } + } else { + this.okSince = 0 + this.holdProgress = 0 + } + break + case 'training': + if (pose && issue !== 'no_body') { + for (const e of this.counter.update(pose)) events.push(...this.onCoach(e)) + } + break + case 'lost': + if (issue === 'none' || issue === 'partial') events.push(...this.setPhase('training')) + break + } + return events + } + + /** 驱动计时与倒计时;没有新帧时也应定时调用 */ + tick(now: number): SessionEvent[] { + const events: SessionEvent[] = [] + const dt = this.lastTick ? now - this.lastTick : 0 + this.lastTick = now + + if (this.phase === 'training') { + this.activeMs += dt + if (this.detectLost && this.lastSeen && now - this.lastSeen > LOST_AFTER_MS) events.push(...this.setPhase('lost')) + } else if (this.phase === 'countdown') { + const left = Math.ceil((COUNTDOWN_MS - (now - this.countdownStart)) / 1000) + if (left <= 0) { + this.lastSeen = now + events.push(...this.setPhase('training')) + } else if (left !== this.countdown) { + this.countdown = left + events.push({ type: 'countdown', n: left }) + } + } + return events + } + + /** 跳过站位直接倒计时(领练模式) */ + startCountdown(now: number): SessionEvent[] { + if (this.phase !== 'framing') return [] + this.countdownStart = now + this.countdown = 3 + this.lastTick = now + return [...this.setPhase('countdown'), { type: 'countdown', n: 3 }] + } + + /** 领练模式:按示范节奏计一次(不评分,不计入动作质量) */ + addGuidedRep(): SessionEvent[] { + if (this.phase !== 'training') return [] + this.guidedReps += 1 + const out: SessionEvent[] = [{ type: 'rep', count: this.count, score: 0, grade: 'ok' }] + if (this.count >= this.targetReps) out.push(...this.setPhase('done')) + return out + } + + pause(): SessionEvent[] { + if (this.phase === 'paused' || this.phase === 'done') return [] + this.beforePause = this.phase === 'lost' ? 'training' : this.phase + return this.setPhase('paused') + } + + resume(now: number): SessionEvent[] { + if (this.phase !== 'paused') return [] + this.lastTick = now + this.lastSeen = now + // 倒计时中被暂停,恢复后重新站位 + const next = this.beforePause === 'countdown' ? 'framing' : this.beforePause + this.okSince = 0 + return this.setPhase(next) + } + + finish(): SessionEvent[] { + return this.phase === 'done' ? [] : this.setPhase('done') + } + + private setPhase(p: SessionPhase): SessionEvent[] { + if (this.phase === p) return [] + this.phase = p + return [{ type: 'phase', phase: p }] + } + + private onCoach(e: CoachEvent): SessionEvent[] { + if (e.type !== 'rep') return [e] + const out: SessionEvent[] = [e] + this.reps.push({ score: e.score, grade: e.grade, tip: e.tip }) + if (e.score >= COMBO_SCORE && !e.joints) { + this.combo += 1 + this.bestCombo = Math.max(this.bestCombo, this.combo) + if (COMBO_MILESTONES.includes(this.combo)) out.push({ type: 'combo', combo: this.combo }) + } else { + this.combo = 0 + } + if (this.count >= this.targetReps) out.push(...this.setPhase('done')) + return out + } +} + +export type SessionSummary = { + move: MoveId + reps: number + targetReps: number + avgScore: number + bestCombo: number + durationSec: number + kcal: number + stars: 1 | 2 | 3 + scores: number[] + /** 出现最多的纠正提示,作为下次训练建议;没有纠正时为鼓励语 */ + advice: string + /** advice 是纠正建议(tip)还是鼓励(praise) */ + adviceKind: 'tip' | 'praise' +} + +/** 汇总结果;kcal = 净 MET × 体重 × 小时 */ +export function summarize(s: TrainingSession, netMet: number, weightKg = 60): SessionSummary { + const scores = s.reps.map((r) => r.score) + const avg = scores.length ? Math.round(scores.reduce((a, b) => a + b, 0) / scores.length) : 0 + const durationSec = Math.round(s.activeMs / 1000) + const kcal = Math.round(netMet * weightKg * (durationSec / 3600) * 10) / 10 + const tips = new Map() + for (const r of s.reps) if (r.tip) tips.set(r.tip, (tips.get(r.tip) ?? 0) + 1) + const top = [...tips.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] + const done = s.count >= s.targetReps + const stars: 1 | 2 | 3 = done && avg >= 88 ? 3 : done && avg >= 75 ? 2 : 1 + return { + move: s.move, + reps: s.count, + targetReps: s.targetReps, + avgScore: avg, + bestCombo: s.bestCombo, + durationSec, + kcal, + stars, + scores, + advice: top ?? (avg >= 90 ? '动作非常标准,继续保持!' : '保持节奏,每次都做到位'), + adviceKind: top ? 'tip' : 'praise', + } +} diff --git a/TUICallKit-Vue3/training/pose/useBgm.ts b/TUICallKit-Vue3/training/pose/useBgm.ts new file mode 100644 index 000000000..0be55baa2 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/useBgm.ts @@ -0,0 +1,64 @@ +/** + * 训练背景音乐(页面用) + * + * 播放控制在 bgm.ts;这里负责创建播放器、把开关记在本地。 + * 音乐地址为空或加载失败时 available 为 false,页面隐藏开关,训练照常进行。 + */ +import { onUnmounted, ref } from 'vue' +import { createBgm, type BgmAudio, type BgmState } from './bgm' + +const STORAGE_KEY = 'pose_coach_bgm_enabled' +/** 教练语音音量是 1,音乐压在它下面约 9dB,报数、口令都听得清 */ +const BGM_VOLUME = 0.35 + +export function useBgm(url: string) { + const enabled = ref(readEnabled()) + const available = ref(Boolean(url)) + const player = createBgm({ + url, + volume: BGM_VOLUME, + create: createAudio, + onUnavailable: (reason) => { + available.value = false + if (process.env.NODE_ENV === 'development') console.warn('[背景音乐] 加载失败,本次不播放:', reason, url) + }, + }) + player.setEnabled(enabled.value) + + function toggle() { + enabled.value = !enabled.value + try { + uni.setStorageSync(STORAGE_KEY, enabled.value) + } catch (e) {} + player.setEnabled(enabled.value) + } + + onUnmounted(() => player.destroy()) + + return { + enabled, + available, + toggle, + prepare: () => player.prepare(), + setState: (s: BgmState) => player.setState(s), + setDuck: (ratio: number) => player.setDuck(ratio), + hush: () => player.hush(), + } +} + +function readEnabled() { + try { + const v = uni.getStorageSync(STORAGE_KEY) + return v === '' ? true : Boolean(v) + } catch (e) { + return true + } +} + +/** + * 长音频用默认的系统播放器边下边播(教练语音用的 WebAudio 实现要把整段解码进内存)。 + * 静音键:跟练页进入时统一设置了不跟随静音键(setInnerAudioOption),这里不用单独设。 + */ +function createAudio(): BgmAudio { + return uni.createInnerAudioContext() as unknown as BgmAudio +} diff --git a/TUICallKit-Vue3/training/pose/useCoachVoice.ts b/TUICallKit-Vue3/training/pose/useCoachVoice.ts new file mode 100644 index 000000000..95096a3bc --- /dev/null +++ b/TUICallKit-Vue3/training/pose/useCoachVoice.ts @@ -0,0 +1,223 @@ +/** + * 教练语音播报 + * + * 报数要和动作同步,关键是“即时出声”: + * - 语音由 WechatSI 同声传译插件合成(需要联网),进入页面就把报数、口令、纠错预合成好; + * - 每句合成后立刻建好一个 WebAudio 驱动的播放器(useWebAudioImplement,短音频延迟低), + * 播报时从头播放,不再临时创建播放器、下载音频; + * - 报数没准备好就跳过这一次,不在几百毫秒后“补报”,免得越报越落后。 + * 优先级:high(纠错、倒计时)> count(报数)> low(鼓励)。 + * high 打断一切;count 打断 count / low,不打断 high;low 只在空闲时播。 + * 其它端:H5 用 speechSynthesis,都不支持时静默。 + * speaking 是正在播的语句的优先级(没在说话时为 null),背景音乐据此压低音量。 + */ +import { ref, onUnmounted } from 'vue' + +export type Priority = 'low' | 'count' | 'high' + +const STORAGE_KEY = 'pose_coach_voice_enabled' +/** 没预合成的 high 语句:合成耗时超过这个值就不播了,避免提示和动作对不上 */ +const HIGH_STALE_MS = 1500 +const RANK: Record = { low: 0, count: 1, high: 2 } +/** 单句都很短:超过这个时间还没收到播完回调(个别机型不触发),视为已播完 */ +const MAX_UTTERANCE_MS = 4000 + +type Player = UniApp.InnerAudioContext + +export function useCoachVoice() { + const enabled = ref(readEnabled()) + const players = new Map() + const inflight = new Map>() + let current: { player: Player; priority: Priority; at: number } | null = null + const speaking = ref(null) + let utterance = 0 + let speakingTimer: ReturnType | null = null + + function readEnabled() { + try { + const v = uni.getStorageSync(STORAGE_KEY) + return v === '' ? true : Boolean(v) + } catch (e) { + return true + } + } + + function toggle() { + enabled.value = !enabled.value + try { + uni.setStorageSync(STORAGE_KEY, enabled.value) + } catch (e) {} + if (!enabled.value) stop() + } + + /** 标记开始 / 结束说话,返回这句话的编号;个别机型不触发播完回调,最长按 MAX_UTTERANCE_MS 算 */ + function markSpeaking(priority: Priority | null) { + utterance += 1 + const id = utterance + speaking.value = priority + if (speakingTimer) clearTimeout(speakingTimer) + speakingTimer = priority ? setTimeout(() => endSpeaking(id), MAX_UTTERANCE_MS) : null + return id + } + + function endSpeaking(id: number) { + if (id === utterance) markSpeaking(null) + } + + /** 合成一句语音,返回音频地址 */ + function synth(text: string): Promise { + return new Promise((resolve) => { + // #ifdef MP-WEIXIN + try { + // eslint-disable-next-line no-undef + const plugin = requirePlugin('WechatSI') + plugin.textToSpeech({ + lang: 'zh_CN', + tts: true, + content: text, + success: (res: { filename?: string }) => resolve(res?.filename || null), + fail: () => resolve(null), + }) + } catch (e) { + resolve(null) + } + // #endif + // #ifndef MP-WEIXIN + resolve(null) + // #endif + }) + } + + function createPlayer(src: string): Player { + let p: Player + // #ifdef MP-WEIXIN + p = (wx as any).createInnerAudioContext({ useWebAudioImplement: true }) + // #endif + // #ifndef MP-WEIXIN + p = uni.createInnerAudioContext() + // #endif + p.src = src + ;(p as any).obeyMuteSwitch = false + p.onEnded(() => { + if (current?.player !== p) return + current = null + markSpeaking(null) + }) + return p + } + + /** 合成并建好播放器(同一句只做一次) */ + function prepare(text: string): Promise { + const ready = players.get(text) + if (ready) return Promise.resolve(ready) + const running = inflight.get(text) + if (running) return running + const p = synth(text) + .then((src) => { + if (!src) return null + const player = createPlayer(src) + player.onError(() => { + // 音频地址过期或加载失败:丢掉,下次重新合成 + if (players.get(text) === player) players.delete(text) + if (current?.player === player) { + current = null + markSpeaking(null) + } + }) + players.set(text, player) + return player + }) + .finally(() => inflight.delete(text)) + inflight.set(text, p) + return p + } + + /** 预合成(并发 2),不阻塞页面 */ + async function preload(texts: string[]) { + const queue = [...new Set(texts)].filter((t) => t && !players.has(t)) + const worker = async () => { + while (queue.length) { + const t = queue.shift() + if (t) await prepare(t) + } + } + await Promise.all([worker(), worker()]) + } + + function canPlay(priority: Priority) { + if (current && Date.now() - current.at > MAX_UTTERANCE_MS) current = null + if (!current) return true + if (priority === 'low') return false + return RANK[priority] >= RANK[current.priority] + } + + function playNow(player: Player, priority: Priority) { + if (current && current.player !== player) { + try { + current.player.stop() + } catch (e) {} + } + try { + player.seek(0) + } catch (e) {} + player.play() + current = { player, priority, at: Date.now() } + markSpeaking(priority) + } + + async function speak(text: string, priority: Priority = 'low') { + if (!enabled.value || !text || !canPlay(priority)) return + // #ifdef H5 + speakWeb(text, priority) + return + // #endif + const ready = players.get(text) + if (ready) return playNow(ready, priority) + if (priority !== 'high') { + // 报数、鼓励没准备好:这次跳过,合成好留给下次 + void prepare(text) + return + } + const t0 = Date.now() + const player = await prepare(text) + if (player && enabled.value && Date.now() - t0 <= HIGH_STALE_MS && canPlay(priority)) playNow(player, priority) + } + + /** H5:浏览器自带语音合成 */ + function speakWeb(text: string, priority: Priority) { + try { + const synthApi = (window as any).speechSynthesis + if (!synthApi) return + if (priority !== 'low') synthApi.cancel() + const u = new (window as any).SpeechSynthesisUtterance(text) + u.lang = 'zh-CN' + u.rate = 1.1 + const id = markSpeaking(priority) + u.onend = () => endSpeaking(id) + synthApi.speak(u) + } catch (e) {} + } + + function stop() { + if (current) { + try { + current.player.stop() + } catch (e) {} + current = null + } + markSpeaking(null) + } + + onUnmounted(() => { + stop() + if (speakingTimer) clearTimeout(speakingTimer) + for (const p of players.values()) { + try { + p.destroy() + } catch (e) {} + } + players.clear() + }) + + return { enabled, toggle, speak, preload, stop, speaking } +} diff --git a/TUICallKit-Vue3/training/pose/usePoseCamera.ts b/TUICallKit-Vue3/training/pose/usePoseCamera.ts new file mode 100644 index 000000000..cff8939d8 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/usePoseCamera.ts @@ -0,0 +1,182 @@ +/** + * 摄像头取帧 + VisionKit 人体关键点检测 + * + * 方案: 负责预览(原生、流畅),CameraContext.onCameraFrame 拿 RGBA 帧, + * 节流后交给 VKSession.detectBody(mode 2,手动传图),结果通过 updateAnchors 回调。 + * 这样不需要自己用 WebGL 渲染相机画面; 放在哪个组件里都可以(一个页面只有一个摄像头)。 + * + * 要求:微信基础库 >= 2.28.0;非微信端或不支持的机型会返回 unsupported。 + */ +import { ref, shallowRef, onUnmounted } from 'vue' +import { anchorToPose, smoothPose, type Pose } from './keypoints.ts' + +export type CameraStatus = 'idle' | 'starting' | 'running' | 'unsupported' | 'denied' | 'error' + +/** 检测间隔:约 10fps,兼顾流畅与发热 */ +const DETECT_INTERVAL_MS = 90 +/** 检测请求超时:没有回调视为画面里没人 */ +const DETECT_TIMEOUT_MS = 600 +/** 连续多久没检测到人,清空姿态 */ +const LOST_AFTER_MS = 800 + +type FrameInfo = { width: number; height: number } + +export function usePoseCamera(onPose?: (pose: Pose | null) => void) { + const status = ref('idle') + const errorText = ref('') + const pose = shallowRef(null) + const frame = ref({ width: 480, height: 640 }) + /** 实际检测帧率,调试用 */ + const fps = ref(0) + + let session: any = null + let listener: any = null + let pending = false + let pendingTimer: ReturnType | null = null + let lastSent = 0 + let lastSeen = 0 + let sentFrame: FrameInfo = { width: 0, height: 0 } + let fpsCount = 0 + let fpsTimer: ReturnType | null = null + + function emit(next: Pose | null) { + pose.value = next + onPose?.(next) + } + + function clearPending() { + pending = false + if (pendingTimer) { + clearTimeout(pendingTimer) + pendingTimer = null + } + } + + function handleAnchors(anchors: any[]) { + clearPending() + fpsCount += 1 + const now = Date.now() + // 画面里多人时取检测框最大的那个 + const best = (anchors || []).slice().sort((a, b) => area(b) - area(a))[0] + const next = best ? anchorToPose(best, sentFrame.width, sentFrame.height, now) : null + if (next) { + lastSeen = now + emit(smoothPose(pose.value, next)) + } else if (now - lastSeen > LOST_AFTER_MS) { + emit(null) + } + } + + function area(a: any) { + const s = a?.size + if (!s) return 0 + return Array.isArray(s) ? s[0] * s[1] : (s.width || 0) * (s.height || 0) + } + + /** 相机组件 initdone 后调用 */ + function start() { + if (status.value === 'running' || status.value === 'starting') return + status.value = 'starting' + // #ifdef MP-WEIXIN + startVisionKit() + // #endif + // #ifndef MP-WEIXIN + status.value = 'unsupported' + // #endif + } + + function startVisionKit() { + try { + const w: any = wx + if (typeof w.createVKSession !== 'function' || (w.isVKSupport && !w.isVKSupport('v1'))) { + status.value = 'unsupported' + return + } + session = w.createVKSession({ track: { body: { mode: 2 } }, version: 'v1' }) + session.on('updateAnchors', (anchors: any[]) => handleAnchors(anchors)) + session.on('removeAnchors', () => handleAnchors([])) + session.start((err: any) => { + if (err) { + status.value = 'unsupported' + errorText.value = String(err?.errMsg || err) + return + } + startFrames() + }) + } catch (e: any) { + status.value = 'unsupported' + errorText.value = String(e?.message || e) + } + } + + function startFrames() { + const cam: any = uni.createCameraContext() + if (!cam?.onCameraFrame) { + status.value = 'unsupported' + return + } + listener = cam.onCameraFrame((f: { data: ArrayBuffer; width: number; height: number }) => { + const now = Date.now() + if (pending || now - lastSent < DETECT_INTERVAL_MS || !session) return + if (frame.value.width !== f.width || frame.value.height !== f.height) { + frame.value = { width: f.width, height: f.height } + } + pending = true + lastSent = now + sentFrame = { width: f.width, height: f.height } + pendingTimer = setTimeout(() => handleAnchors([]), DETECT_TIMEOUT_MS) + try { + session.detectBody({ + frameBuffer: f.data, + width: f.width, + height: f.height, + scoreThreshold: 0.5, + // 0 表示输入来自视频连续帧,算法会做帧间跟踪 + sourceType: 0, + }) + } catch (e) { + clearPending() + } + }) + listener.start({ + success: () => { + status.value = 'running' + fpsTimer = setInterval(() => { + fps.value = fpsCount + fpsCount = 0 + }, 1000) + }, + fail: (e: any) => { + status.value = 'error' + errorText.value = String(e?.errMsg || e) + }, + }) + } + + function stop() { + clearPending() + try { + listener?.stop?.() + } catch (e) {} + listener = null + try { + session?.stop?.() + session?.destroy?.() + } catch (e) {} + session = null + if (fpsTimer) clearInterval(fpsTimer) + fpsTimer = null + if (status.value === 'running' || status.value === 'starting') status.value = 'idle' + pose.value = null + } + + /** :通常是用户拒绝了摄像头权限 */ + function onCameraError(e: any) { + status.value = 'denied' + errorText.value = String(e?.detail?.errMsg || '') + } + + onUnmounted(stop) + + return { status, errorText, pose, frame, fps, start, stop, onCameraError } +} diff --git a/TUICallKit-Vue3/training/pose/viewport.ts b/TUICallKit-Vue3/training/pose/viewport.ts new file mode 100644 index 000000000..58fe08353 --- /dev/null +++ b/TUICallKit-Vue3/training/pose/viewport.ts @@ -0,0 +1,26 @@ +/** + * 摄像头帧坐标 → 屏幕坐标 + * + * 识别出的关键点是相对摄像头帧归一化的 [0,1] 坐标; 预览按 aspectFill 铺满显示区域(裁掉多出的部分), + * 前置摄像头的预览是镜像的。纠正动作时在画面上标出关节,需要用它换算位置。 + */ +import type { Point } from './keypoints.ts' + +export type Viewport = { + /** 显示区域尺寸(px) */ + width: number + height: number + /** 摄像头帧尺寸 */ + frameWidth: number + frameHeight: number + /** 预览是否镜像 */ + mirror: boolean +} + +export function toScreen(p: Point, vp: Viewport): Point { + const scale = Math.max(vp.width / vp.frameWidth, vp.height / vp.frameHeight) + const ox = (vp.width - vp.frameWidth * scale) / 2 + const oy = (vp.height - vp.frameHeight * scale) / 2 + const x = vp.mirror ? 1 - p.x : p.x + return { x: ox + x * vp.frameWidth * scale, y: oy + p.y * vp.frameHeight * scale } +} diff --git a/TUICallKit-Vue3/training/static/coach/cheer.png b/TUICallKit-Vue3/training/static/coach/cheer.png new file mode 100644 index 000000000..2cf74761d Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/cheer.png differ diff --git a/TUICallKit-Vue3/training/static/coach/idle.png b/TUICallKit-Vue3/training/static/coach/idle.png new file mode 100644 index 000000000..b96165583 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/idle.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_00.png b/TUICallKit-Vue3/training/static/coach/jl_00.png new file mode 100644 index 000000000..4fffd9b04 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_00.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_01.png b/TUICallKit-Vue3/training/static/coach/jl_01.png new file mode 100644 index 000000000..c9612b3c5 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_01.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_02.png b/TUICallKit-Vue3/training/static/coach/jl_02.png new file mode 100644 index 000000000..9e6d55525 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_02.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_03.png b/TUICallKit-Vue3/training/static/coach/jl_03.png new file mode 100644 index 000000000..8408b1b0a Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_03.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_04.png b/TUICallKit-Vue3/training/static/coach/jl_04.png new file mode 100644 index 000000000..e4ad1fbc1 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_04.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_05.png b/TUICallKit-Vue3/training/static/coach/jl_05.png new file mode 100644 index 000000000..420cc2938 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_05.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_06.png b/TUICallKit-Vue3/training/static/coach/jl_06.png new file mode 100644 index 000000000..9671ab0fe Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_06.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_07.png b/TUICallKit-Vue3/training/static/coach/jl_07.png new file mode 100644 index 000000000..656e8e605 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_07.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_08.png b/TUICallKit-Vue3/training/static/coach/jl_08.png new file mode 100644 index 000000000..61307b425 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_08.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jl_09.png b/TUICallKit-Vue3/training/static/coach/jl_09.png new file mode 100644 index 000000000..e258d3eea Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jl_09.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_01.png b/TUICallKit-Vue3/training/static/coach/jr_01.png new file mode 100644 index 000000000..e4df254eb Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_01.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_02.png b/TUICallKit-Vue3/training/static/coach/jr_02.png new file mode 100644 index 000000000..f1ef94dbf Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_02.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_03.png b/TUICallKit-Vue3/training/static/coach/jr_03.png new file mode 100644 index 000000000..51e2fe70d Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_03.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_04.png b/TUICallKit-Vue3/training/static/coach/jr_04.png new file mode 100644 index 000000000..d9ede4e03 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_04.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_05.png b/TUICallKit-Vue3/training/static/coach/jr_05.png new file mode 100644 index 000000000..0a7dbda93 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_05.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_06.png b/TUICallKit-Vue3/training/static/coach/jr_06.png new file mode 100644 index 000000000..1f125c6c6 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_06.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_07.png b/TUICallKit-Vue3/training/static/coach/jr_07.png new file mode 100644 index 000000000..b3180ef6d Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_07.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_08.png b/TUICallKit-Vue3/training/static/coach/jr_08.png new file mode 100644 index 000000000..b0681e48d Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_08.png differ diff --git a/TUICallKit-Vue3/training/static/coach/jr_09.png b/TUICallKit-Vue3/training/static/coach/jr_09.png new file mode 100644 index 000000000..19c17afc4 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/jr_09.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_00.png b/TUICallKit-Vue3/training/static/coach/ml_00.png new file mode 100644 index 000000000..346d79b26 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_00.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_01.png b/TUICallKit-Vue3/training/static/coach/ml_01.png new file mode 100644 index 000000000..91bcecc1b Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_01.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_02.png b/TUICallKit-Vue3/training/static/coach/ml_02.png new file mode 100644 index 000000000..a5ace4ed4 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_02.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_03.png b/TUICallKit-Vue3/training/static/coach/ml_03.png new file mode 100644 index 000000000..cf7939824 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_03.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_04.png b/TUICallKit-Vue3/training/static/coach/ml_04.png new file mode 100644 index 000000000..90872dc27 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_04.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_05.png b/TUICallKit-Vue3/training/static/coach/ml_05.png new file mode 100644 index 000000000..e2d287ac6 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_05.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_06.png b/TUICallKit-Vue3/training/static/coach/ml_06.png new file mode 100644 index 000000000..bfa647af6 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_06.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_07.png b/TUICallKit-Vue3/training/static/coach/ml_07.png new file mode 100644 index 000000000..17a324b79 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_07.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_08.png b/TUICallKit-Vue3/training/static/coach/ml_08.png new file mode 100644 index 000000000..71b5d33db Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_08.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_09.png b/TUICallKit-Vue3/training/static/coach/ml_09.png new file mode 100644 index 000000000..d37a512ff Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_09.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_10.png b/TUICallKit-Vue3/training/static/coach/ml_10.png new file mode 100644 index 000000000..ce8c2a950 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_10.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_11.png b/TUICallKit-Vue3/training/static/coach/ml_11.png new file mode 100644 index 000000000..39bd0d7a5 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_11.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ml_12.png b/TUICallKit-Vue3/training/static/coach/ml_12.png new file mode 100644 index 000000000..e0aa62fdd Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ml_12.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_01.png b/TUICallKit-Vue3/training/static/coach/mr_01.png new file mode 100644 index 000000000..977a198e0 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_01.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_02.png b/TUICallKit-Vue3/training/static/coach/mr_02.png new file mode 100644 index 000000000..8f3fd329f Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_02.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_03.png b/TUICallKit-Vue3/training/static/coach/mr_03.png new file mode 100644 index 000000000..545a62fef Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_03.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_04.png b/TUICallKit-Vue3/training/static/coach/mr_04.png new file mode 100644 index 000000000..a1b3b8145 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_04.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_05.png b/TUICallKit-Vue3/training/static/coach/mr_05.png new file mode 100644 index 000000000..34262e0db Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_05.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_06.png b/TUICallKit-Vue3/training/static/coach/mr_06.png new file mode 100644 index 000000000..1e3d705d5 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_06.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_07.png b/TUICallKit-Vue3/training/static/coach/mr_07.png new file mode 100644 index 000000000..bfbf41153 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_07.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_08.png b/TUICallKit-Vue3/training/static/coach/mr_08.png new file mode 100644 index 000000000..776fe9ae6 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_08.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_09.png b/TUICallKit-Vue3/training/static/coach/mr_09.png new file mode 100644 index 000000000..7267b93a8 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_09.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_10.png b/TUICallKit-Vue3/training/static/coach/mr_10.png new file mode 100644 index 000000000..f7393b44c Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_10.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_11.png b/TUICallKit-Vue3/training/static/coach/mr_11.png new file mode 100644 index 000000000..e2d758ff9 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_11.png differ diff --git a/TUICallKit-Vue3/training/static/coach/mr_12.png b/TUICallKit-Vue3/training/static/coach/mr_12.png new file mode 100644 index 000000000..1dd3b24c0 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/mr_12.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_00.png b/TUICallKit-Vue3/training/static/coach/ra_00.png new file mode 100644 index 000000000..395fcc91b Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_00.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_01.png b/TUICallKit-Vue3/training/static/coach/ra_01.png new file mode 100644 index 000000000..600eea84a Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_01.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_02.png b/TUICallKit-Vue3/training/static/coach/ra_02.png new file mode 100644 index 000000000..283f4c62f Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_02.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_03.png b/TUICallKit-Vue3/training/static/coach/ra_03.png new file mode 100644 index 000000000..5d0fad404 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_03.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_04.png b/TUICallKit-Vue3/training/static/coach/ra_04.png new file mode 100644 index 000000000..f5f7e1286 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_04.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_05.png b/TUICallKit-Vue3/training/static/coach/ra_05.png new file mode 100644 index 000000000..e23c0f8d8 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_05.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_06.png b/TUICallKit-Vue3/training/static/coach/ra_06.png new file mode 100644 index 000000000..54f59f2f3 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_06.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_07.png b/TUICallKit-Vue3/training/static/coach/ra_07.png new file mode 100644 index 000000000..3eb3e8833 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_07.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_08.png b/TUICallKit-Vue3/training/static/coach/ra_08.png new file mode 100644 index 000000000..7d3e23aff Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_08.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_09.png b/TUICallKit-Vue3/training/static/coach/ra_09.png new file mode 100644 index 000000000..efdca464a Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_09.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_10.png b/TUICallKit-Vue3/training/static/coach/ra_10.png new file mode 100644 index 000000000..24892f481 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_10.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_11.png b/TUICallKit-Vue3/training/static/coach/ra_11.png new file mode 100644 index 000000000..e8a0addac Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_11.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_12.png b/TUICallKit-Vue3/training/static/coach/ra_12.png new file mode 100644 index 000000000..f1fc1ebb3 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_12.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_13.png b/TUICallKit-Vue3/training/static/coach/ra_13.png new file mode 100644 index 000000000..305894082 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_13.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_14.png b/TUICallKit-Vue3/training/static/coach/ra_14.png new file mode 100644 index 000000000..82365d9ed Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_14.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_15.png b/TUICallKit-Vue3/training/static/coach/ra_15.png new file mode 100644 index 000000000..1415654ef Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_15.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_16.png b/TUICallKit-Vue3/training/static/coach/ra_16.png new file mode 100644 index 000000000..94fa2662d Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_16.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_17.png b/TUICallKit-Vue3/training/static/coach/ra_17.png new file mode 100644 index 000000000..10d1721c1 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_17.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_18.png b/TUICallKit-Vue3/training/static/coach/ra_18.png new file mode 100644 index 000000000..f32368f16 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_18.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_19.png b/TUICallKit-Vue3/training/static/coach/ra_19.png new file mode 100644 index 000000000..43edc6d9f Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_19.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_20.png b/TUICallKit-Vue3/training/static/coach/ra_20.png new file mode 100644 index 000000000..7ec2e0840 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_20.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_21.png b/TUICallKit-Vue3/training/static/coach/ra_21.png new file mode 100644 index 000000000..ee831057a Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_21.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_22.png b/TUICallKit-Vue3/training/static/coach/ra_22.png new file mode 100644 index 000000000..bc2288ea7 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_22.png differ diff --git a/TUICallKit-Vue3/training/static/coach/ra_23.png b/TUICallKit-Vue3/training/static/coach/ra_23.png new file mode 100644 index 000000000..b5f6b7256 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/ra_23.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_00.png b/TUICallKit-Vue3/training/static/coach/sq_00.png new file mode 100644 index 000000000..444e6ed48 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_00.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_01.png b/TUICallKit-Vue3/training/static/coach/sq_01.png new file mode 100644 index 000000000..0bce77dd2 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_01.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_02.png b/TUICallKit-Vue3/training/static/coach/sq_02.png new file mode 100644 index 000000000..0af29b668 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_02.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_03.png b/TUICallKit-Vue3/training/static/coach/sq_03.png new file mode 100644 index 000000000..d84301c90 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_03.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_04.png b/TUICallKit-Vue3/training/static/coach/sq_04.png new file mode 100644 index 000000000..b2102c735 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_04.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_05.png b/TUICallKit-Vue3/training/static/coach/sq_05.png new file mode 100644 index 000000000..d643bb28e Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_05.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_06.png b/TUICallKit-Vue3/training/static/coach/sq_06.png new file mode 100644 index 000000000..d5695e780 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_06.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_07.png b/TUICallKit-Vue3/training/static/coach/sq_07.png new file mode 100644 index 000000000..fb2cfd90f Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_07.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_08.png b/TUICallKit-Vue3/training/static/coach/sq_08.png new file mode 100644 index 000000000..c32cc45e8 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_08.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_09.png b/TUICallKit-Vue3/training/static/coach/sq_09.png new file mode 100644 index 000000000..39059e111 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_09.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_10.png b/TUICallKit-Vue3/training/static/coach/sq_10.png new file mode 100644 index 000000000..86526713d Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_10.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_11.png b/TUICallKit-Vue3/training/static/coach/sq_11.png new file mode 100644 index 000000000..ed290b310 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_11.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_12.png b/TUICallKit-Vue3/training/static/coach/sq_12.png new file mode 100644 index 000000000..512c7be3c Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_12.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_13.png b/TUICallKit-Vue3/training/static/coach/sq_13.png new file mode 100644 index 000000000..04dfa3253 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_13.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_14.png b/TUICallKit-Vue3/training/static/coach/sq_14.png new file mode 100644 index 000000000..471dd644f Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_14.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_15.png b/TUICallKit-Vue3/training/static/coach/sq_15.png new file mode 100644 index 000000000..8106e34c7 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_15.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_16.png b/TUICallKit-Vue3/training/static/coach/sq_16.png new file mode 100644 index 000000000..4171d032b Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_16.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_17.png b/TUICallKit-Vue3/training/static/coach/sq_17.png new file mode 100644 index 000000000..fb8f85ada Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_17.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_18.png b/TUICallKit-Vue3/training/static/coach/sq_18.png new file mode 100644 index 000000000..2c55e9092 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_18.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_19.png b/TUICallKit-Vue3/training/static/coach/sq_19.png new file mode 100644 index 000000000..0e2084a34 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_19.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_20.png b/TUICallKit-Vue3/training/static/coach/sq_20.png new file mode 100644 index 000000000..4b8d86130 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_20.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_21.png b/TUICallKit-Vue3/training/static/coach/sq_21.png new file mode 100644 index 000000000..338692706 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_21.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_22.png b/TUICallKit-Vue3/training/static/coach/sq_22.png new file mode 100644 index 000000000..53180e44b Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_22.png differ diff --git a/TUICallKit-Vue3/training/static/coach/sq_23.png b/TUICallKit-Vue3/training/static/coach/sq_23.png new file mode 100644 index 000000000..762eccca8 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/sq_23.png differ diff --git a/TUICallKit-Vue3/training/static/coach/thumbs.png b/TUICallKit-Vue3/training/static/coach/thumbs.png new file mode 100644 index 000000000..f45dcf012 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/thumbs.png differ diff --git a/TUICallKit-Vue3/training/static/coach/wave.png b/TUICallKit-Vue3/training/static/coach/wave.png new file mode 100644 index 000000000..5e0de07c9 Binary files /dev/null and b/TUICallKit-Vue3/training/static/coach/wave.png differ