|
1 | 1 | class PoseEstimator { |
2 | | - constructor(videoElement, canvasElement) { |
3 | | - this.video = videoElement; |
| 2 | + constructor(canvasElement) { |
4 | 3 | this.canvas = canvasElement; |
5 | 4 | this.ctx = this.canvas.getContext('2d'); |
| 5 | + this.video = null; // 内部创建的video元素,不显示 |
6 | 6 | this.detector = null; |
7 | 7 | this.poseFilters = null; |
8 | 8 | this.lastFilteredPose = null; // 用于存储上一帧的滤波结果 |
@@ -111,6 +111,14 @@ class PoseEstimator { |
111 | 111 |
|
112 | 112 | // 设置摄像头 |
113 | 113 | async _setupCamera() { |
| 114 | + // 创建隐藏的video元素用于获取摄像头流 |
| 115 | + this.video = document.createElement('video'); |
| 116 | + this.video.style.display = 'none'; |
| 117 | + this.video.autoplay = true; |
| 118 | + this.video.playsInline = true; |
| 119 | + this.video.muted = true; |
| 120 | + document.body.appendChild(this.video); |
| 121 | + |
114 | 122 | const stream = await navigator.mediaDevices.getUserMedia({ 'video': true }); |
115 | 123 | this.video.srcObject = stream; |
116 | 124 | await new Promise((resolve) => { |
@@ -324,9 +332,8 @@ class PoseEstimator { |
324 | 332 |
|
325 | 333 | // --- Main Execution --- |
326 | 334 | async function main() { |
327 | | - const video = document.getElementById('video'); |
328 | 335 | const canvas = document.getElementById('canvas'); |
329 | | - const estimator = new PoseEstimator(video, canvas); |
| 336 | + const estimator = new PoseEstimator(canvas); |
330 | 337 | await estimator.start(); |
331 | 338 | } |
332 | 339 |
|
|
0 commit comments