File size: 28,295 Bytes
c81cd3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
import DeviceDetector from "https://cdn.skypack.dev/[email protected]";
// 用法: testSupport({client?: string, os?: string}[])
// Client 和 os 是正则表达式。
// 参见: https://cdn.jsdelivr.net/npm/[email protected]/README.md
// 了解 client 和 os 的合法值
// 导入必要的库

// 初始化速度和加速度图表
let speedChart, accelerationChart;
let previousPoseData = null;
let lastTimestamp = 0;
testSupport([
    { client: 'Chrome' },
]);

// 图表相关的常量配置
const CHART_CONFIG = {
    maxDataPoints: 50,
    updateInterval: 100, // 图表更新间隔(ms)
    colors: {
        speed: {
            primary: 'rgba(75, 192, 192, 1)',
            background: 'rgba(75, 192, 192, 0.1)'
        },
        acceleration: {
            primary: 'rgba(255, 99, 132, 1)',
            background: 'rgba(255, 99, 132, 0.1)'
        }
    }
};

// 初始化图表
function initCharts() {
    // 创建图表容器
    const chartsContainer = document.createElement('div');
    chartsContainer.className = 'charts-container';
    document.querySelector('.container').appendChild(chartsContainer);

    // 速度图表容器
    const speedChartContainer = document.createElement('div');
    speedChartContainer.className = 'chart-card';
    const speedCanvas = document.createElement('canvas');
    speedCanvas.id = 'speedChart';
    speedChartContainer.appendChild(speedCanvas);
    chartsContainer.appendChild(speedChartContainer);

    // 加速度图表容器
    const accelerationChartContainer = document.createElement('div');
    accelerationChartContainer.className = 'chart-card';
    const accelerationCanvas = document.createElement('canvas');
    accelerationCanvas.id = 'accelerationChart';
    accelerationChartContainer.appendChild(accelerationCanvas);
    chartsContainer.appendChild(accelerationChartContainer);

    // 速度图表配置
    speedChart = new Chart(speedCanvas.getContext('2d'), {
        type: 'line',
        data: {
            labels: [],
            datasets: [{
                label: '运动速度 (像素/秒)',
                data: [],
                borderColor: CHART_CONFIG.colors.speed.primary,
                backgroundColor: CHART_CONFIG.colors.speed.background,
                tension: 0.4,
                borderWidth: 2,
                fill: true,
                pointRadius: 0,
                pointHitRadius: 10
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            animation: {
                duration: 0
            },
            interaction: {
                intersect: false,
                mode: 'index'
            },
            plugins: {
                legend: {
                    position: 'top',
                    labels: {
                        font: {
                            family: '"Titillium Web", sans-serif',
                            size: 14
                        },
                        color: '#333'
                    }
                },
                tooltip: {
                    enabled: true,
                    backgroundColor: 'rgba(0, 0, 0, 0.7)',
                    titleFont: {
                        family: '"Titillium Web", sans-serif'
                    },
                    bodyFont: {
                        family: '"Titillium Web", sans-serif'
                    }
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    grid: {
                        color: 'rgba(0, 0, 0, 0.1)'
                    },
                    ticks: {
                        font: {
                            family: '"Titillium Web", sans-serif'
                        }
                    }
                },
                x: {
                    grid: {
                        display: false
                    },
                    ticks: {
                        maxRotation: 0,
                        maxTicksLimit: 5,
                        font: {
                            family: '"Titillium Web", sans-serif'
                        }
                    }
                }
            }
        }
    });

    // 加速度图表配置
    accelerationChart = new Chart(accelerationCanvas.getContext('2d'), {
        type: 'line',
        data: {
            labels: [],
            datasets: [{
                label: '加速度 (像素/秒²)',
                data: [],
                borderColor: CHART_CONFIG.colors.acceleration.primary,
                backgroundColor: CHART_CONFIG.colors.acceleration.background,
                tension: 0.4,
                borderWidth: 2,
                fill: true,
                pointRadius: 0,
                pointHitRadius: 10
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            animation: {
                duration: 0
            },
            interaction: {
                intersect: false,
                mode: 'index'
            },
            plugins: {
                legend: {
                    position: 'top',
                    labels: {
                        font: {
                            family: '"Titillium Web", sans-serif',
                            size: 14
                        },
                        color: '#333'
                    }
                },
                tooltip: {
                    enabled: true,
                    backgroundColor: 'rgba(0, 0, 0, 0.7)',
                    titleFont: {
                        family: '"Titillium Web", sans-serif'
                    },
                    bodyFont: {
                        family: '"Titillium Web", sans-serif'
                    }
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    grid: {
                        color: 'rgba(0, 0, 0, 0.1)'
                    },
                    ticks: {
                        font: {
                            family: '"Titillium Web", sans-serif'
                        }
                    }
                },
                x: {
                    grid: {
                        display: false
                    },
                    ticks: {
                        maxRotation: 0,
                        maxTicksLimit: 5,
                        font: {
                            family: '"Titillium Web", sans-serif'
                        }
                    }
                }
            }
        }
    });

    // 添加响应式处理
    window.addEventListener('resize', () => {
        speedChart.resize();
        accelerationChart.resize();
    });
}

// 计算姿态变化的速度和加速度
function calculateMotionMetrics(results, timestamp) {
    // 基本验证
    if (!results || !results.poseLandmarks || !Array.isArray(results.poseLandmarks)) {
        return { speed: 0, acceleration: 0 };
    }

    // 初始化状态
    if (!previousPoseData || !previousPoseData.poseLandmarks) {
        previousPoseData = {
            poseLandmarks: [...results.poseLandmarks]
        };
        lastTimestamp = timestamp;
        return { speed: 0, acceleration: 0 };
    }

    const deltaTime = (timestamp - lastTimestamp) / 1000; // 转换为秒
    if (deltaTime === 0) {
        return { speed: 0, acceleration: 0 };
    }

    // 计算关键点的平均位移
    let totalDisplacement = 0;
    let validPoints = 0;
    
    try {
        // 只使用有效的关键点进行计算
        results.poseLandmarks.forEach((landmark, index) => {
            const prevLandmark = previousPoseData.poseLandmarks[index];
            
            // 确保当前和前一帧的关键点都存在且有效
            if (landmark && prevLandmark && 
                typeof landmark.x === 'number' && 
                typeof landmark.y === 'number' &&
                typeof prevLandmark.x === 'number' && 
                typeof prevLandmark.y === 'number' &&
                // 可选:检查可见性阈值
                (!landmark.visibility || landmark.visibility > 0.5) &&
                (!prevLandmark.visibility || prevLandmark.visibility > 0.5)) {
                
                const dx = landmark.x - prevLandmark.x;
                const dy = landmark.y - prevLandmark.y;
                const displacement = Math.sqrt(dx * dx + dy * dy);
                
                // 过滤掉异常大的位移
                if (displacement < 1.0) { // 根据需要调整阈值
                    totalDisplacement += displacement;
                    validPoints++;
                }
            }
        });
    } catch (error) {
        console.warn('Error calculating displacement:', error);
        return { speed: 0, acceleration: 0 };
    }

    // 如果没有有效点,返回零值
    if (validPoints === 0) {
        return { speed: 0, acceleration: 0 };
    }

    // 计算平均位移和速度
    const averageDisplacement = totalDisplacement / validPoints;
    const currentSpeed = averageDisplacement / deltaTime;

    // 获取上一次的速度,如果不存在则使用0
    let previousSpeed = 0;
    try {
        previousSpeed = speedChart.data.datasets[0].data[speedChart.data.datasets[0].data.length - 1] || 0;
    } catch (error) {
        console.warn('Error accessing previous speed:', error);
    }

    // 计算加速度
    const acceleration = (currentSpeed - previousSpeed) / deltaTime;

    // 更新先前数据用于下一帧计算
    previousPoseData = {
        poseLandmarks: [...results.poseLandmarks]
    };
    lastTimestamp = timestamp;

    // 添加一些基本的数值验证
    const metrics = {
        speed: isFinite(currentSpeed) ? Math.min(Math.max(currentSpeed, 0), 1000) : 0,
        acceleration: isFinite(acceleration) ? Math.min(Math.max(acceleration, -1000), 1000) : 0
    };

    return metrics;
}

// 更新图表数据的函数也需要添加错误处理
function updateCharts(metrics) {
    if (!metrics || typeof metrics.speed !== 'number' || typeof metrics.acceleration !== 'number') {
        console.warn('Invalid metrics data:', metrics);
        return;
    }

    try {
        const timestamp = new Date().toLocaleTimeString('zh-CN', { 
            hour: '2-digit', 
            minute: '2-digit', 
            second: '2-digit' 
        });

        // 检查图表对象是否存在且正确初始化
        if (!speedChart || !speedChart.data || !speedChart.data.labels) {
            console.warn('Speed chart not properly initialized');
            return;
        }
        if (!accelerationChart || !accelerationChart.data || !accelerationChart.data.labels) {
            console.warn('Acceleration chart not properly initialized');
            return;
        }

        // 更新速度图表
        speedChart.data.labels.push(timestamp);
        speedChart.data.datasets[0].data.push(metrics.speed);
        if (speedChart.data.labels.length > CHART_CONFIG.maxDataPoints) {
            speedChart.data.labels.shift();
            speedChart.data.datasets[0].data.shift();
        }

        // 更新加速度图表
        accelerationChart.data.labels.push(timestamp);
        accelerationChart.data.datasets[0].data.push(metrics.acceleration);
        if (accelerationChart.data.labels.length > CHART_CONFIG.maxDataPoints) {
            accelerationChart.data.labels.shift();
            accelerationChart.data.datasets[0].data.shift();
        }

        // 使用 requestAnimationFrame 优化图表更新
        requestAnimationFrame(() => {
            try {
                speedChart.update('none');
                accelerationChart.update('none');
            } catch (error) {
                console.warn('Error updating charts:', error);
            }
        });
    } catch (error) {
        console.warn('Error in updateCharts:', error);
    }
}


function testSupport(supportedDevices) {
    const deviceDetector = new DeviceDetector();
    const detectedDevice = deviceDetector.parse(navigator.userAgent);
    let isSupported = false;
    for (const device of supportedDevices) {
        if (device.client !== undefined) {
            const re = new RegExp(`^${device.client}$`);
            if (!re.test(detectedDevice.client.name)) {
                continue;
            }
        }
        if (device.os !== undefined) {
            const re = new RegExp(`^${device.os}$`);
            if (!re.test(detectedDevice.os.name)) {
                continue;
            }
        }
        isSupported = true;
        break;
    }
    if (!isSupported) {
        alert(`此演示在 ${detectedDevice.client.name}/${detectedDevice.os.name} 上运行时 ` +
            `目前不能很好地支持,继续使用需自担风险。`);
    }
}

const controls = window;
const mpHolistic = window;
const drawingUtils = window;
const config = { locateFile: (file) => {
        return `https://cdn.jsdelivr.net/npm/@mediapipe/holistic@` +
            `${mpHolistic.VERSION}/${file}`;
    } };

// 我们的输入帧将来自这里。
const videoElement = document.getElementsByClassName('input_video')[0];
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const controlsElement = document.getElementsByClassName('control-panel')[0];
const canvasCtx = canvasElement.getContext('2d');

// 我们稍后会将这个添加到控制面板中,但我们会在这里保存它,
// 以便每次图形运行时都可以调用 tick()。
const fpsControl = new controls.FPS();

// 优化:在隐藏动画完成后关闭动画旋转器。
const spinner = document.querySelector('.loading');
spinner.ontransitionend = () => {
    spinner.style.display = 'none';
};

function removeElements(landmarks, elements) {
    for (const element of elements) {
        delete landmarks[element];
    }
}

function removeLandmarks(results) {
    if (results.poseLandmarks) {
        removeElements(results.poseLandmarks, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 18, 19, 20, 21, 22]);
    }
}

function connect(ctx, connectors) {
    const canvas = ctx.canvas;
    for (const connector of connectors) {
        const from = connector[0];
        const to = connector[1];
        if (from && to) {
            if (from.visibility && to.visibility &&
                (from.visibility < 0.1 || to.visibility < 0.1)) {
                continue;
            }
            ctx.beginPath();
            ctx.moveTo(from.x * canvas.width, from.y * canvas.height);
            ctx.lineTo(to.x * canvas.width, to.y * canvas.height);
            ctx.stroke();
        }
    }
}

let activeEffect = 'mask';

    
function onResults(results) {
    // 隐藏旋转器
    document.body.classList.add('loaded');

    // 移除我们不想绘制的关键点
    removeLandmarks(results);

    // 更新帧率
    fpsControl.tick();

    // 绘制叠加层
    canvasCtx.save();
    canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);

    if (results.segmentationMask) {
        canvasCtx.drawImage(results.segmentationMask, 0, 0, canvasElement.width, canvasElement.height);

        // 仅覆盖现有像素
        if (activeEffect === 'mask' || activeEffect === 'both') {
            canvasCtx.globalCompositeOperation = 'source-in';
            canvasCtx.fillStyle = '#00FF007F';
            canvasCtx.fillRect(0, 0, canvasElement.width, canvasElement.height);
        } else {
            canvasCtx.globalCompositeOperation = 'source-out';
            canvasCtx.fillStyle = '#0000FF7F';
            canvasCtx.fillRect(0, 0, canvasElement.width, canvasElement.height);
        }

        // 仅覆盖缺失的像素
        canvasCtx.globalCompositeOperation = 'destination-atop';
        canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
        canvasCtx.globalCompositeOperation = 'source-over';
    } else {
        canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
    }

    // 计算并更新动作指标
    const metrics = calculateMotionMetrics(results, performance.now());
    updateCharts(metrics);

    // 添加安全检查
    if (!results.poseLandmarks || !mpHolistic.POSE_LANDMARKS) {
        canvasCtx.restore();
        return;
    }

    // 连接肘部到手部
    canvasCtx.lineWidth = 5;
    if (results.poseLandmarks) {
        if (results.rightHandLandmarks && results.poseLandmarks[mpHolistic.POSE_LANDMARKS.RIGHT_ELBOW]) {
            canvasCtx.strokeStyle = 'white';
            connect(canvasCtx, [[
                results.poseLandmarks[mpHolistic.POSE_LANDMARKS.RIGHT_ELBOW],
                results.rightHandLandmarks[0]
            ]]);
        }
        if (results.leftHandLandmarks && results.poseLandmarks[mpHolistic.POSE_LANDMARKS.LEFT_ELBOW]) {
            canvasCtx.strokeStyle = 'white';
            connect(canvasCtx, [[
                results.poseLandmarks[mpHolistic.POSE_LANDMARKS.LEFT_ELBOW],
                results.leftHandLandmarks[0]
            ]]);
        }
    }

    // 绘制姿态连接点
    if (results.poseLandmarks && mpHolistic.POSE_CONNECTIONS) {
        drawingUtils.drawConnectors(canvasCtx, results.poseLandmarks, mpHolistic.POSE_CONNECTIONS, { color: 'white' });
    }

    // 绘制左侧姿态关键点
    if (results.poseLandmarks && mpHolistic.POSE_LANDMARKS_LEFT) {
        const leftLandmarks = Object.values(mpHolistic.POSE_LANDMARKS_LEFT)
            .map(index => results.poseLandmarks[index])
            .filter(landmark => landmark !== undefined);
        
        if (leftLandmarks.length > 0) {
            drawingUtils.drawLandmarks(canvasCtx, leftLandmarks, 
                { visibilityMin: 0.65, color: 'white', fillColor: 'rgb(255,138,0)' });
        }
    }

    // 绘制右侧姿态关键点
    if (results.poseLandmarks && mpHolistic.POSE_LANDMARKS_RIGHT) {
        const rightLandmarks = Object.values(mpHolistic.POSE_LANDMARKS_RIGHT)
            .map(index => results.poseLandmarks[index])
            .filter(landmark => landmark !== undefined);
        
        if (rightLandmarks.length > 0) {
            drawingUtils.drawLandmarks(canvasCtx, rightLandmarks,
                { visibilityMin: 0.65, color: 'white', fillColor: 'rgb(0,217,231)' });
        }
    }

    // 绘制手部
    if (results.rightHandLandmarks && mpHolistic.HAND_CONNECTIONS) {
        drawingUtils.drawConnectors(canvasCtx, results.rightHandLandmarks, mpHolistic.HAND_CONNECTIONS, { color: 'white' });
        drawingUtils.drawLandmarks(canvasCtx, results.rightHandLandmarks, {
            color: 'white',
            fillColor: 'rgb(0,217,231)',
            lineWidth: 2,
            radius: (data) => {
                return drawingUtils.lerp(data.from.z, -0.15, .1, 10, 1);
            }
        });
    }

    if (results.leftHandLandmarks && mpHolistic.HAND_CONNECTIONS) {
        drawingUtils.drawConnectors(canvasCtx, results.leftHandLandmarks, mpHolistic.HAND_CONNECTIONS, { color: 'white' });
        drawingUtils.drawLandmarks(canvasCtx, results.leftHandLandmarks, {
            color: 'white',
            fillColor: 'rgb(255,138,0)',
            lineWidth: 2,
            radius: (data) => {
                return drawingUtils.lerp(data.from.z, -0.15, .1, 10, 1);
            }
        });
    }

    // 绘制面部
    if (results.faceLandmarks && mpHolistic.FACEMESH_TESSELATION) {
        drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_TESSELATION, 
            { color: '#C0C0C070', lineWidth: 1 });
        
        if (mpHolistic.FACEMESH_RIGHT_EYE) {
            drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_RIGHT_EYE, 
                { color: 'rgb(0,217,231)' });
        }
        if (mpHolistic.FACEMESH_RIGHT_EYEBROW) {
            drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_RIGHT_EYEBROW, 
                { color: 'rgb(0,217,231)' });
        }
        if (mpHolistic.FACEMESH_LEFT_EYE) {
            drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_LEFT_EYE, 
                { color: 'rgb(255,138,0)' });
        }
        if (mpHolistic.FACEMESH_LEFT_EYEBROW) {
            drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_LEFT_EYEBROW, 
                { color: 'rgb(255,138,0)' });
        }
        if (mpHolistic.FACEMESH_FACE_OVAL) {
            drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_FACE_OVAL, 
                { color: '#E0E0E0', lineWidth: 5 });
        }
        if (mpHolistic.FACEMESH_LIPS) {
            drawingUtils.drawConnectors(canvasCtx, results.faceLandmarks, mpHolistic.FACEMESH_LIPS, 
                { color: '#E0E0E0', lineWidth: 5 });
        }
    }

    canvasCtx.restore();
}
// 视频上传处理
function handleVideoUpload(file) {
    // 创建视频 URL
    const videoUrl = URL.createObjectURL(file);
    
    // 重置图表数据
    speedChart.data.labels = [];
    speedChart.data.datasets[0].data = [];
    accelerationChart.data.labels = [];
    accelerationChart.data.datasets[0].data = [];
    previousPoseData = null;
    lastTimestamp = 0;

    // 重置姿态检测
    holistic.reset();

    // 更新视频源
    videoElement.src = videoUrl;
    
    // 设置视频加载完成后的处理
    videoElement.onloadedmetadata = () => {
        // 调整画布大小以匹配视频尺寸
        const aspect = videoElement.videoHeight / videoElement.videoWidth;
        let width, height;
        if (window.innerWidth > window.innerHeight) {
            height = window.innerHeight;
            width = height / aspect;
        } else {
            width = window.innerWidth;
            height = width * aspect;
        }
        canvasElement.width = width;
        canvasElement.height = height;

        // 创建用于视频处理的动画帧
        let animationId;
        
        async function processFrame() {
            if (videoElement.paused || videoElement.ended) {
                cancelAnimationFrame(animationId);
                return;
            }

            // 发送当前帧到姿态检测
            await holistic.send({
                image: videoElement
            });

            // 继续处理下一帧
            animationId = requestAnimationFrame(processFrame);
        }

        // 视频播放事件处理
        videoElement.onplay = () => {
            processFrame();
        };

        // 添加视频控制按钮事件监听
        const playPauseBtn = document.createElement('button');
        playPauseBtn.textContent = '播放/暂停';
        playPauseBtn.className = 'control-button';
        playPauseBtn.onclick = () => {
            if (videoElement.paused) {
                videoElement.play();
            } else {
                videoElement.pause();
            }
        };

        const restartBtn = document.createElement('button');
        restartBtn.textContent = '重新开始';
        restartBtn.className = 'control-button';
        restartBtn.onclick = () => {
            videoElement.currentTime = 0;
            if (videoElement.paused) {
                videoElement.play();
            }
        };

        // 添加控制按钮到界面
        const controlsContainer = document.createElement('div');
        controlsContainer.className = 'video-controls';
        controlsContainer.appendChild(playPauseBtn);
        controlsContainer.appendChild(restartBtn);
        
        // 找到合适的位置插入控制按钮
        const container = document.querySelector('.container') || document.body;
        container.appendChild(controlsContainer);
    };

    // 添加错误处理
    videoElement.onerror = () => {
        console.error('视频加载失败');
        alert('视频加载失败,请尝试其他视频文件');
    };
}

// 添加一些基本的样式
const style = document.createElement('style');
style.textContent = `
    .video-controls {
        position: fixed;
        bottom: 20px;
        left: 50%;
        transform: translateX(-50%);
        z-index: 1000;
        display: flex;
        gap: 10px;
    }
    
    .control-button {
        padding: 10px 20px;
        background-color: rgba(0, 0, 0, 0.7);
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 14px;
    }
    
    .control-button:hover {
        background-color: rgba(0, 0, 0, 0.9);
    }
`;
document.head.appendChild(style);

const holistic = new mpHolistic.Holistic(config);
holistic.onResults(onResults);

// 呈现一个控制面板,用户可以通过它操作解决方案选项。
new controls
    .ControlPanel(controlsElement, {
    selfieMode: true,
    modelComplexity: 1,
    smoothLandmarks: true,
    enableSegmentation: false,
    smoothSegmentation: true,
    minDetectionConfidence: 0.5,
    minTrackingConfidence: 0.5,
    effect: 'background',
})
    .add([
    new controls.StaticText({ title: 'MediaPipe 全身姿态检测' }),
    fpsControl,
    new controls.Toggle({ title: '自拍模式', field: 'selfieMode' }),
    new controls.SourcePicker({
        onSourceChanged: () => {
            // 重置,因为在源更改之间重置时,姿势会给出更好的结果。
            holistic.reset();
        },
        onFrame: async (input, size) => {
            const aspect = size.height / size.width;
            let width, height;
            if (window.innerWidth > window.innerHeight) {
                height = window.innerHeight;
                width = height / aspect;
            }
            else {
                width = window.innerWidth;
                height = width * aspect;
            }
            canvasElement.width = width;
            canvasElement.height = height;
            await holistic.send({ image: input });
        },
    }),
    new controls.Slider({
        title: '模型复杂度',
        field: 'modelComplexity',
        discrete: ['轻量', '完整', '重度'],
    }),
    new controls.Toggle({ title: '平滑关键点', field: 'smoothLandmarks' }),
    new controls.Toggle({ title: '启用分割', field: 'enableSegmentation' }),
    new controls.Toggle({ title: '平滑分割', field: 'smoothSegmentation' }),
    new controls.Slider({
        title: '最小检测置信度',
        field: 'minDetectionConfidence',
        range: [0, 1],
        step: 0.01
    }),
    new controls.Slider({
        title: '最小跟踪置信度',
        field: 'minTrackingConfidence',
        range: [0, 1],
        step: 0.01
    }),
    new controls.Slider({
        title: '效果',
        field: 'effect',
        discrete: { 'background': '背景', 'mask': '前景' },
    }),
])
    .on(x => {
    const options = x;
    videoElement.classList.toggle('selfie', options.selfieMode);
    activeEffect = x['effect'];
    holistic.setOptions(options);
});
// 初始化函数
function initialize() {
    // 初始化图表
    initCharts();

    // 设置视频上传处理
    const videoUploadInput = document.querySelector('#video-upload');
    if (videoUploadInput) {
        videoUploadInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                handleVideoUpload(e.target.files[0]);
            }
        });
    }

    // 初始化姿态检测
    const holistic = new mpHolistic.Holistic(config);
    holistic.onResults(onResults);

    // ... 保持其他原有的初始化逻辑 ...
}

// 启动应用
window.addEventListener('load', initialize);

// 保持原有的窗口大小调整逻辑
window.addEventListener('resize', () => {
    const aspect = videoElement.videoHeight / videoElement.videoWidth;
    let width, height;
    if (window.innerWidth > window.innerHeight) {
        height = window.innerHeight;
        width = height / aspect;
    } else {
        width = window.innerWidth;
        height = width * aspect;
    }
    canvasElement.width = width;
    canvasElement.height = height;
    
    // 重新调整图表大小
    speedChart.resize();
    accelerationChart.resize();
});