-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
574 lines (476 loc) · 22.4 KB
/
Copy pathscript.js
File metadata and controls
574 lines (476 loc) · 22.4 KB
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
document.addEventListener("DOMContentLoaded", () => {
class PoseDetector {
constructor() {
this.video = document.getElementById('videoElement');
this.canvas = document.getElementById('canvasElement');
this.ctx = this.canvas.getContext('2d');
this.startBtn = document.getElementById('startBtn');
this.stopBtn = document.getElementById('stopBtn');
this.switchBtn = document.getElementById('switchBtn');
this.status = document.getElementById('status');
this.poseInfo = document.getElementById('poseInfo');
this.keypointCount = document.getElementById('keypointCount');
this.fpsCounter = document.getElementById('fpsCounter');
this.neckAngle = document.getElementById('neckAngle');
this.postureDisplay = document.getElementById('postureDisplay');
this.postureStatusLarge = document.getElementById('postureStatusLarge');
this.statusIcon = document.getElementById('statusIcon');
this.statusTitle = document.getElementById('statusTitle');
this.statusSubtitle = document.getElementById('statusSubtitle');
this.postureTimer = document.getElementById('postureTimer');
this.timerValue = document.getElementById('timerValue');
this.timerFill = document.getElementById('timerFill');
this.postureAlert = document.getElementById('postureAlert');
this.stream = null;
this.currentCamera = 0;
this.cameras = [];
this.pose = null;
this.isModelLoaded = false;
// posture analysis variables: may consider calibrating or adding adjustablility
this.postureThresholds = {
perfect: 180, // theoretically perfect alignment
good: 168, // yellow range
warning: 160 // warning threshold: bad posture
};
this.currentNeckAngle = 180;
// warning tracking
this.poorPostureStartTime = null;
this.poorPostureDuration = 0;
this.warningDurationThreshold = 10000; // 10 seconds in milliseconds
this.alertShown = false;
this.alertTimeout = null;
// fps tracking
this.frameCount = 0;
this.lastTime = performance.now();
this.fps = 0;
this.initializePoseDetection();
this.bindEvents();
}
async initializePoseDetection() {
try {
// check mediapipe pose availability
if (typeof Pose === 'undefined') {
throw new Error('MediaPipe Pose not loaded');
}
this.updateStatus('<div class="loading"></div>Initializing pose detection...');
// init pose model
this.pose = new Pose({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/pose/${file}`;
}
});
// pose model configuration
this.pose.setOptions({
modelComplexity: 1,
smoothLandmarks: true,
enableSegmentation: false,
smoothSegmentation: false,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
this.pose.onResults((results) => {
this.onResults(results);
});
this.isModelLoaded = true;
this.updateStatus('✅ Pose detection ready - Click "Start Camera"');
this.startBtn.disabled = false;
} catch (error) {
console.error('MediaPipe initialization failed:', error);
this.initializeFallbackDetection();
}
}
initializeFallbackDetection() {
// fallback to basic camera mode without pose detection
this.updateStatus('⚠️ Using basic camera mode (pose detection unavailable)');
this.startBtn.disabled = false;
this.isModelLoaded = false;
}
bindEvents() {
this.startBtn.addEventListener('click', () => this.startCamera());
this.stopBtn.addEventListener('click', () => this.stopCamera());
this.switchBtn.addEventListener('click', () => this.switchCamera());
window.addEventListener('resize', () => this.resizeCanvas());
}
async getCameras() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
this.cameras = devices.filter(device => device.kind === 'videoinput');
return this.cameras.length > 0;
} catch (error) {
console.error('Error getting cameras:', error);
return false;
}
}
async startCamera() {
this.updateStatus('<div class="loading"></div>Starting camera...');
try {
const hasCameras = await this.getCameras();
if (!hasCameras) {
this.updateStatus('❌ No cameras found');
return;
}
const constraints = {
video: {
deviceId: this.cameras[this.currentCamera]?.deviceId,
width: { ideal: 640 },
height: { ideal: 480 },
facingMode: this.cameras.length > 1 ? undefined : 'user'
}
};
this.stream = await navigator.mediaDevices.getUserMedia(constraints);
this.video.srcObject = this.stream;
this.video.addEventListener('loadedmetadata', () => {
this.resizeCanvas();
if (this.isModelLoaded) {
this.startPoseDetection();
} else {
this.startBasicMode();
}
});
this.startBtn.disabled = true;
this.stopBtn.disabled = false;
this.switchBtn.disabled = this.cameras.length <= 1;
const status = this.isModelLoaded ?
'Detecting poses...' :
'Camera active (basic mode)';
this.updateStatus(status);
this.poseInfo.style.display = 'flex';
this.postureDisplay.style.display = 'block';
} catch (error) {
console.error('Error starting camera:', error);
this.updateStatus('❌ Camera access denied');
}
}
startBasicMode() {
// Just show the camera feed without pose detection
const animate = () => {
if (this.stream) {
this.updateFPS();
requestAnimationFrame(animate);
}
};
animate();
}
async startPoseDetection() {
const detectPose = async () => {
if (!this.stream || this.video.readyState !== 4) {
if (this.stream) requestAnimationFrame(detectPose);
return;
}
try {
await this.pose.send({ image: this.video });
} catch (error) {
console.warn('Pose detection error:', error);
}
if (this.stream) {
requestAnimationFrame(detectPose);
}
};
detectPose();
}
onResults(results) {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (results.poseLandmarks && results.poseLandmarks.length > 0) {
this.drawPose(results.poseLandmarks);
this.analyzePosePosture(results.poseLandmarks);
this.updatePoseStats(results.poseLandmarks);
}
this.updateFPS();
}
analyzePosePosture(landmarks) {
// pose landmark indices:
// 7: left ear, 8: right ear
// 11: left shoulder, 12: right shoulder
const leftEar = landmarks[7];
const rightEar = landmarks[8];
const leftShoulder = landmarks[11];
const rightShoulder = landmarks[12];
// check required landmarks visibility
const requiredLandmarks = [leftEar, rightEar, leftShoulder, rightShoulder];
const hasGoodVisibility = requiredLandmarks.every(landmark =>
landmark && (landmark.visibility || 1) > 0.6
);
if (!hasGoodVisibility) {
this.updatePostureStatus('unknown', '?', 'Keypoints not visible', 'Position yourself within the frame');
this.resetPoorPostureTimer();
return;
}
// calculate average positions (for better stability)
const avgEar = {
x: (leftEar.x + rightEar.x) / 2,
y: (leftEar.y + rightEar.y) / 2
};
const avgShoulder = {
x: (leftShoulder.x + rightShoulder.x) / 2,
y: (leftShoulder.y + rightShoulder.y) / 2
};
// calculate neck angle
const deltaX = avgEar.x - avgShoulder.x;
const deltaY = avgShoulder.y - avgEar.y; // inverted because screen coordinates
// calculate angle in degrees
let angle = Math.atan2(Math.abs(deltaX), deltaY) * (180 / Math.PI);
// convert to scale where 180° is perfect
this.currentNeckAngle = 180 - Math.abs(angle);
// determine posture quality and handle persistence
this.handlePosturePersistence();
// draw posture analysis lines
this.drawPostureAnalysis(avgEar, avgShoulder);
}
handlePosturePersistence() {
const currentTime = Date.now();
let postureQuality, title, subtitle;
// determine and display current posture quality
if (this.currentNeckAngle >= this.postureThresholds.good) {
postureQuality = 'good';
title = 'Excellent Posture!';
subtitle = `${Math.round(this.currentNeckAngle)}° - Keep it up!`;
this.resetPoorPostureTimer();
} else if (this.currentNeckAngle >= this.postureThresholds.warning) {
postureQuality = 'fair';
title = 'Fair Posture';
subtitle = `${Math.round(this.currentNeckAngle)}° - Could be better`;
this.resetPoorPostureTimer();
} else {
// poor posture detected
postureQuality = 'poor';
if (this.poorPostureStartTime === null) {
// first detection of poor posture
this.poorPostureStartTime = currentTime;
this.poorPostureDuration = 0;
} else {
// update duration
this.poorPostureDuration = currentTime - this.poorPostureStartTime;
}
const remainingTime = Math.max(0, this.warningDurationThreshold - this.poorPostureDuration);
const secondsRemaining = Math.ceil(remainingTime / 1000);
if (this.poorPostureDuration >= this.warningDurationThreshold) {
title = 'Poor Posture Alert!';
subtitle = `${Math.round(this.currentNeckAngle)}° - Straighten up now!`;
this.showPostureAlert();
} else {
title = 'Poor Posture Detected';
subtitle = `${Math.round(this.currentNeckAngle)}° - Warning in ${secondsRemaining}s`;
this.hidePostureAlert();
}
this.updatePostureTimer();
}
this.updatePostureStatus(postureQuality, this.currentNeckAngle, title, subtitle);
}
resetPoorPostureTimer() {
this.poorPostureStartTime = null;
this.poorPostureDuration = 0;
this.postureTimer.style.display = 'none';
this.hidePostureAlert();
}
updatePostureTimer() {
if (this.poorPostureStartTime === null) return;
this.postureTimer.style.display = 'block';
const seconds = Math.floor(this.poorPostureDuration / 1000);
this.timerValue.textContent = `${seconds}s`;
const progress = Math.min(this.poorPostureDuration / this.warningDurationThreshold, 1);
this.timerFill.style.width = `${progress * 100}%`;
}
drawPostureAnalysis(avgEar, avgShoulder) {
const earX = avgEar.x * this.canvas.width;
const earY = avgEar.y * this.canvas.height;
const shoulderX = avgShoulder.x * this.canvas.width;
const shoulderY = avgShoulder.y * this.canvas.height;
// draw vertical reference line from shoulders
this.ctx.strokeStyle = 'rgba(107, 207, 127, 0.8)';
this.ctx.lineWidth = 2;
this.ctx.setLineDash([5, 5]);
this.ctx.beginPath();
this.ctx.moveTo(shoulderX, shoulderY - 100);
this.ctx.lineTo(shoulderX, shoulderY + 50);
this.ctx.stroke();
// draw line from shoulder to ear
const color = this.currentNeckAngle >= this.postureThresholds.good ?
'rgba(107, 207, 127, 0.8)' :
this.currentNeckAngle >= this.postureThresholds.warning ?
'rgba(255, 217, 61, 0.8)' :
'rgba(255, 107, 107, 0.8)';
this.ctx.strokeStyle = color;
this.ctx.lineWidth = 4;
this.ctx.setLineDash([]);
this.ctx.beginPath();
this.ctx.moveTo(shoulderX, shoulderY);
this.ctx.lineTo(earX, earY);
this.ctx.stroke();
// draw angle visualization
this.drawAngleVisualization(shoulderX, shoulderY, earX, earY);
}
drawAngleVisualization(shoulderX, shoulderY, earX, earY) {
const radius = 50;
// calculate the angle from vertical
const deltaX = earX - shoulderX;
const deltaY = earY - shoulderY;
const angleFromVertical = Math.atan2(deltaX, deltaY);
// draw angle arc from vertical to neck line
this.ctx.strokeStyle = 'rgba(255, 255, 255, 0.9)';
this.ctx.lineWidth = 3;
this.ctx.beginPath();
this.ctx.arc(shoulderX, shoulderY, radius, -Math.PI/2, angleFromVertical, deltaX > 0);
this.ctx.stroke();
// draw angle measurement text
this.ctx.fillStyle = 'white';
this.ctx.font = 'bold 16px Arial';
this.ctx.textAlign = 'center';
this.ctx.strokeStyle = 'rgba(0,0,0,0.7)';
this.ctx.lineWidth = 3;
const textX = shoulderX + (deltaX > 0 ? radius + 30 : -radius - 30);
const textY = shoulderY - 20;
this.ctx.strokeText(`${Math.round(this.currentNeckAngle)}°`, textX, textY);
this.ctx.fillText(`${Math.round(this.currentNeckAngle)}°`, textX, textY);
// add perfect value reference
//this.ctx.font = 'bold 12px Arial';
//this.ctx.fillStyle = 'rgba(107, 207, 127, 0.9)';
//this.ctx.fillText('Perfect: 180°', textX, textY + 20);
}
updatePostureStatus(quality, angle, title, subtitle) {
// update angle display
this.neckAngle.textContent = `${Math.round(angle)}°`;
// update large posture display
this.statusTitle.textContent = title;
this.statusSubtitle.textContent = subtitle;
// update display styling and icon
this.postureDisplay.className = `posture-display ${quality}`;
// update status icon based on quality
const icons = {
good: '✅',
fair: '⚠️',
poor: '❌',
unknown: '👤'
};
this.statusIcon.textContent = icons[quality] || '👤';
}
showPostureAlert() {
if (!this.alertShown) {
this.postureAlert.style.display = 'block';
this.alertShown = true;
const audio = new Audio('resources/alert.mp3');
audio.play();
// auto hide after 5 seconds if posture improves
if (this.alertTimeout) clearTimeout(this.alertTimeout);
this.alertTimeout = setTimeout(() => {
if (this.currentNeckAngle <= this.postureThresholds.fair) {
this.hidePostureAlert();
}
}, 5000);
}
}
hidePostureAlert() {
if (this.alertShown) {
this.postureAlert.style.display = 'none';
this.alertShown = false;
if (this.alertTimeout) {
clearTimeout(this.alertTimeout);
this.alertTimeout = null;
}
}
}
drawPose(landmarks) {
// draw connections
this.drawConnections(landmarks);
// draw keypoints (only major ones)
landmarks.forEach((landmark, index) => {
// select only ears and shoulders
if (index === 7 || index === 8 || index === 11 || index === 12) {
const x = landmark.x * this.canvas.width;
const y = landmark.y * this.canvas.height;
const visibility = landmark.visibility || 1;
if (visibility > 0.5) {
this.ctx.beginPath();
this.ctx.arc(x, y, 5, 0, 2 * Math.PI);
this.ctx.fillStyle = this.getKeypointColor(index);
this.ctx.fill();
this.ctx.strokeStyle = 'white';
this.ctx.lineWidth = 2;
this.ctx.stroke();
}
}
});
}
drawConnections(landmarks) {
const connections = [
[11, 12] // shows only shoulders, can be updated to include more if needed
];
this.ctx.strokeStyle = 'rgba(0, 255, 255, 0.6)';
this.ctx.lineWidth = 3;
connections.forEach(([start, end]) => {
const startPoint = landmarks[start];
const endPoint = landmarks[end];
if (startPoint && endPoint &&
(startPoint.visibility || 1) > 0.5 &&
(endPoint.visibility || 1) > 0.5) {
this.ctx.beginPath();
this.ctx.moveTo(startPoint.x * this.canvas.width, startPoint.y * this.canvas.height);
this.ctx.lineTo(endPoint.x * this.canvas.width, endPoint.y * this.canvas.height);
this.ctx.stroke();
}
});
}
getKeypointColor(index) {
const colors = [
'#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
'#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9'
];
return colors[index % colors.length];
}
updatePoseStats(landmarks) {
// filtered for only ears and shoulders
const visibleLandmarks = landmarks
.map((l, i) => ({ ...l, index: i }))
.filter(l => [7, 8, 11, 12].includes(l.index) && (l.visibility || 1) > 0.5);
// CONST USED AS LANDMARKS.LENGTH = 4, TO REFER TO ONLY 4 KPs
this.keypointCount.textContent = `${visibleLandmarks.length}/4`;
}
updateFPS() {
this.frameCount++;
const currentTime = performance.now();
if (currentTime - this.lastTime >= 1000) {
this.fps = Math.round((this.frameCount * 1000) / (currentTime - this.lastTime));
this.fpsCounter.textContent = this.fps;
this.frameCount = 0;
this.lastTime = currentTime;
}
}
stopCamera() {
if (this.stream) {
this.stream.getTracks().forEach(track => track.stop());
this.stream = null;
}
this.video.srcObject = null;
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.startBtn.disabled = false;
this.stopBtn.disabled = true;
this.switchBtn.disabled = true;
this.updateStatus('📷 Camera stopped');
this.poseInfo.style.display = 'none';
this.postureDisplay.style.display = 'none';
this.resetPoorPostureTimer();
}
async switchCamera() {
if (this.cameras.length <= 1) return;
this.currentCamera = (this.currentCamera + 1) % this.cameras.length;
this.stopCamera();
setTimeout(() => this.startCamera(), 500);
}
resizeCanvas() {
this.canvas.width = this.video.videoWidth;
this.canvas.height = this.video.videoHeight;
this.canvas.style.width = this.video.offsetWidth + 'px';
this.canvas.style.height = this.video.offsetHeight + 'px';
}
updateStatus(message) {
this.status.innerHTML = message;
}
}
// initialize when page loads
window.addEventListener('load', () => {
setTimeout(() => {
console.log('Checking MediaPipe availability...');
console.log('Pose available:', typeof Pose !== 'undefined');
new PoseDetector();
}, 1000);
});
});