forked from averma205/stepsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1290 lines (1098 loc) · 46.5 KB
/
script.js
File metadata and controls
1290 lines (1098 loc) · 46.5 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
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Global variables
let currentStep = 1;
const totalSteps = 8;
let isRecording = false;
let stream = null;
let referenceData = [];
let userSkeletonData = null;
let animationFrame = null;
let currentFrameIndex = 0;
let poseDetector = null;
let isPlaying = false;
let selectedParticipant = null;
let participantMetadata = [];
// Global variables for OpenCV processing
let lastFrame = null;
let lastValidPose = null;
let motionHistory = [];
const MOTION_HISTORY_SIZE = 5;
// Skeleton connections (pairs of joints that should be connected)
const connections = [
['head', 'neck'],
['neck', 't8'],
['t8', 't12'],
['t12', 'l3'],
['l3', 'l5'],
['l5', 'pelvis'],
['neck', 'right_shoulder'],
['right_shoulder', 'right_upper_arm'],
['right_upper_arm', 'right_forearm'],
['right_forearm', 'right_hand'],
['neck', 'left_shoulder'],
['left_shoulder', 'left_upper_arm'],
['left_upper_arm', 'left_forearm'],
['left_forearm', 'left_hand'],
['pelvis', 'right_upper_leg'],
['right_upper_leg', 'right_lower_leg'],
['right_lower_leg', 'right_foot'],
['right_foot', 'right_toe'],
['pelvis', 'left_upper_leg'],
['left_upper_leg', 'left_lower_leg'],
['left_lower_leg', 'left_foot'],
['left_foot', 'left_toe']
];
// Focus regions for each step - defines which joints to focus on for each step
const focusRegions = {
1: null, // Full body for introduction
2: ['right_foot', 'left_foot', 'right_toe', 'left_toe', 'right_lower_leg', 'left_lower_leg'], // Feet
3: ['right_upper_leg', 'left_upper_leg', 'right_lower_leg', 'left_lower_leg'], // Knees
4: ['pelvis', 'l5', 'l3', 'right_upper_leg', 'left_upper_leg'], // Hips
5: ['t12', 't8', 'l3', 'l5', 'pelvis'], // Core
6: ['right_shoulder', 'left_shoulder', 'right_upper_arm', 'left_upper_arm',
'right_forearm', 'left_forearm', 'right_hand', 'left_hand', 'neck'], // Upper body
7: null // Full body for balance and coordination
};
// Initialize D3 visualizations
function initializeVisualizations() {
// Update layout to place visualizations side by side
const container = document.querySelector('.visualization-container');
container.style.display = 'flex';
container.style.flexDirection = 'row';
container.style.justifyContent = 'space-between';
container.style.gap = '20px';
// Set fixed dimensions for visualizations
const width = 500; // Fixed width
const height = 400; // Fixed height
// Create titles and containers for both visualizations
['reference', 'user'].forEach(type => {
const visContainer = document.getElementById(`${type}-vis`);
visContainer.style.flex = '1';
visContainer.style.minWidth = `${width}px`;
visContainer.style.display = 'flex';
visContainer.style.flexDirection = 'column';
visContainer.style.alignItems = 'center';
// Add title
const title = document.createElement('h3');
// title.textContent = type === 'reference' ? 'Reference Movement' : 'Your Movement';
title.style.textAlign = 'center';
title.style.marginBottom = '15px';
title.style.color = '#3498db';
visContainer.appendChild(title);
const svg = d3.select(`#${type}-vis`)
.append('svg')
.attr('width', width)
.attr('height', height)
.style('display', 'block')
.style('margin', '0 auto');
svg.append('g')
.attr('class', 'skeleton')
.attr('transform', `translate(${width/2}, ${height/2})`);
});
}
// Animation function
function startAnimation() {
if (animationFrame) {
cancelAnimationFrame(animationFrame);
}
const frameDelay = 20; // 100ms delay between frames (10 frames per second)
let lastFrameTime = 0;
function animate(currentTime) {
if (!lastFrameTime) lastFrameTime = currentTime;
const elapsed = currentTime - lastFrameTime;
if (elapsed > frameDelay) {
updateVisualization(currentFrameIndex);
currentFrameIndex = (currentFrameIndex + 1) % referenceData.length;
lastFrameTime = currentTime;
}
if (isPlaying) {
animationFrame = requestAnimationFrame(animate);
}
}
animate(0);
}
// Update visualization based on current frame and step
function updateVisualization(frameIndex = 0) {
if (!referenceData.length) return;
const width = document.querySelector('.skeleton-vis').clientWidth;
const height = document.querySelector('.skeleton-vis').clientHeight;
// Get current frame data
const data = referenceData[frameIndex];
// Focus on specific region based on current step
const focusJoints = focusRegions[currentStep];
// Calculate bounds for visualization
const bounds = getBounds(data, focusJoints);
// Calculate scale
const dataWidth = bounds.maxX - bounds.minX;
const dataHeight = bounds.maxY - bounds.minY;
// Add padding to the bounds
const padding = focusJoints ? 0.2 : 0.1; // More padding for zoomed views
const scale = 0.6 * Math.min(
(width * (1 - padding*2)) / dataWidth,
(height * (1 - padding*2)) / dataHeight
);
const centerX = -((bounds.maxX + bounds.minX) / 2) * scale;
const centerY = -((bounds.maxY + bounds.minY) / 2) * scale;
// Update reference visualization
updateSkeletonVisualization('#reference-vis', data, scale, centerX, centerY, focusJoints);
// If user data exists, update user visualization
if (userSkeletonData) {
updateSkeletonVisualization('#user-vis', userSkeletonData, scale, centerX, centerY, focusJoints);
}
// Update frame counter display
document.getElementById('frame-counter').textContent = `Frame ${frameIndex + 1} of ${referenceData.length}`;
}
// Helper function to calculate data bounds
function getBounds(data, focusJoints = null) {
let minX = Infinity, maxX = -Infinity;
let minY = Infinity, maxY = -Infinity;
Object.keys(data).forEach(key => {
if (key.endsWith('_x')) {
const joint = key.replace('_x', '');
// Skip joints not in focus if we have a focus region
if (focusJoints && !focusJoints.includes(joint)) {
return;
}
const x = parseFloat(data[key]);
const y = parseFloat(data[key.replace('_x', '_y')]);
if (!isNaN(x) && !isNaN(y)) {
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
}
});
// If we didn't find any valid points, or focus region bounds are too small
// (can happen with single-joint focus), use full skeleton bounds
if (minX === Infinity || maxX === -Infinity || maxX - minX < 50 || maxY - minY < 50) {
minX = Infinity, maxX = -Infinity;
minY = Infinity, maxY = -Infinity;
Object.keys(data).forEach(key => {
if (key.endsWith('_x')) {
const x = parseFloat(data[key]);
const y = parseFloat(data[key.replace('_x', '_y')]);
if (!isNaN(x) && !isNaN(y)) {
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
}
});
}
return { minX, maxX, minY, maxY };
}
// Draw skeleton visualization
function updateSkeletonVisualization(selector, data, scale, centerX, centerY, focusJoints = null) {
const svg = d3.select(`${selector} svg .skeleton`);
// Clear previous frame
svg.selectAll('*').remove();
// Use selected participant's measurements
const measurements = selectedParticipant ? {
height: parseFloat(selectedParticipant['Body height (m)']),
shoulderWidth: parseFloat(selectedParticipant['Shoulder Width']),
armSpan: parseFloat(selectedParticipant['Arm Span']),
hipWidth: parseFloat(selectedParticipant['Hip Width']),
kneeHeight: parseFloat(selectedParticipant['Knee Height']),
ankleHeight: parseFloat(selectedParticipant['Ankle Height'])
} : {
height: 1.8,
shoulderWidth: 0.31,
armSpan: 1.74,
hipWidth: 0.27,
kneeHeight: 0.53,
ankleHeight: 0.1
};
// Calculate scaling factors based on measurements
const heightScale = measurements.height / 2;
const shoulderScale = measurements.shoulderWidth / 0.4;
const hipScale = measurements.hipWidth / 0.3;
// Use the average of the scaling factors
const measurementScale = (heightScale + shoulderScale + hipScale) / 3;
// Draw connections
connections.forEach(([joint1, joint2]) => {
// Skip connections not involving focus joints
if (focusJoints &&
!focusJoints.includes(joint1) &&
!focusJoints.includes(joint2)) {
return;
}
const x1 = parseFloat(data[`${joint1}_x`]) * scale;
const y1 = parseFloat(data[`${joint1}_y`]) * scale;
const x2 = parseFloat(data[`${joint2}_x`]) * scale;
const y2 = parseFloat(data[`${joint2}_y`]) * scale;
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
return; // Skip invalid data
}
svg.append('line')
.attr('x1', x1 + centerX)
.attr('y1', y1 + centerY)
.attr('x2', x2 + centerX)
.attr('y2', y2 + centerY)
.attr('stroke', '#2c3e50')
.attr('stroke-width', 2)
.attr('opacity', getFocusOpacity(joint1, joint2, focusJoints));
});
// Draw joints
Object.keys(data).forEach(key => {
if (key.endsWith('_x')) {
const joint = key.replace('_x', '');
const x = parseFloat(data[`${joint}_x`]) * scale;
const y = parseFloat(data[`${joint}_y`]) * scale;
if (isNaN(x) || isNaN(y)) {
return; // Skip invalid data
}
// Skip joints not in focus
if (focusJoints && !focusJoints.includes(joint)) {
return;
}
svg.append('circle')
.attr('cx', x + centerX)
.attr('cy', y + centerY)
.attr('r', joint === 'head' ? 20 : 4)
.attr('fill', getJointColor(joint, currentStep))
.attr('stroke', '#333')
.attr('stroke-width', 1);
// Add joint labels when zoomed in
if (focusJoints) {
svg.append('text')
.attr('x', x + centerX + 8)
.attr('y', y + centerY - 8)
.attr('font-size', '10px')
.attr('fill', '#333')
.text(formatJointName(joint));
}
}
});
}
// Format joint name for display
function formatJointName(joint) {
return joint.replace(/_/g, ' ');
}
// Get joint color based on current step
function getJointColor(joint, step) {
const highlightColor = '#e74c3c';
const defaultColor = '#3498db';
const stepJoints = {
2: ['right_foot', 'left_foot', 'right_toe', 'left_toe'],
3: ['right_lower_leg', 'left_lower_leg', 'right_upper_leg', 'left_upper_leg'],
4: ['pelvis', 'l5', 'l3'],
5: ['t12', 't8'],
6: ['right_shoulder', 'left_shoulder', 'right_hand', 'left_hand'],
7: ['head', 'neck']
};
return stepJoints[step]?.includes(joint) ? highlightColor : defaultColor;
}
// Determine opacity for connections based on focus
function getFocusOpacity(joint1, joint2, focusJoints) {
if (!focusJoints) return 1.0;
const isJoint1Focus = focusJoints.includes(joint1);
const isJoint2Focus = focusJoints.includes(joint2);
if (isJoint1Focus && isJoint2Focus) {
return 1.0; // Full opacity for connections between focus joints
} else if (isJoint1Focus || isJoint2Focus) {
return 0.5; // Medium opacity for connections to one focus joint
} else {
return 0.0; // Hide connections not involving focus joints
}
}
// Handle step navigation
function updateStep(direction) {
// If trying to go to next step without selecting a participant, prevent it
if (direction > 0 && !selectedParticipant) {
alert('Please select a matching participant first');
return;
}
const prevStep = currentStep;
currentStep = Math.max(1, Math.min(totalSteps, currentStep + direction));
// Update button states
document.getElementById('back-btn').disabled = currentStep === 1;
document.getElementById('next-btn').disabled = currentStep === totalSteps || (currentStep === 1 && !selectedParticipant);
// Update step visibility
document.getElementById(`step${prevStep}`).style.display = 'none';
document.getElementById(`step${currentStep}`).style.display = 'block';
// Handle visualization and try-it button based on step
const tryItBtn = document.getElementById('try-it-btn');
const visualizationContainer = document.querySelector('.visualization-container');
const participantForm = document.getElementById('participant-selection');
const mainContent = document.querySelector('.main-content');
if (currentStep === 1) {
// First page: show participant form, hide everything else
tryItBtn.style.display = 'none';
mainContent.style.display = 'none';
participantForm.style.display = 'block';
participantForm.style.opacity = '1';
// Stop camera if recording
if (isRecording) {
stopCamera();
}
// Stop animation if playing
if (animationFrame) {
cancelAnimationFrame(animationFrame);
isPlaying = false;
}
// Reset visualization container state
visualizationContainer.style.justifyContent = 'space-between';
document.getElementById('reference-vis').style.display = 'none';
document.getElementById('user-vis').style.display = 'none';
} else if (currentStep === 8) {
// Last page: hide everything except step 8 content
tryItBtn.style.display = 'none';
mainContent.style.display = 'none';
participantForm.style.display = 'none';
// Stop camera if recording
if (isRecording) {
stopCamera();
}
// Stop animation if playing
if (animationFrame) {
cancelAnimationFrame(animationFrame);
isPlaying = false;
}
// Reset visualization container state
visualizationContainer.style.display = 'none';
document.getElementById('reference-vis').style.display = 'none';
document.getElementById('user-vis').style.display = 'none';
} else {
// Other pages (2-7): show visualization, try-it button, and main content
tryItBtn.style.display = 'inline-block';
mainContent.style.display = 'flex';
visualizationContainer.style.display = 'flex';
participantForm.style.display = 'none';
// Show reference visualization
document.getElementById('reference-vis').style.display = 'flex';
// Initialize visualization if not already done
if (!document.querySelector('#reference-vis svg')) {
initializeVisualizations();
updateVisualization(currentFrameIndex);
}
// Start camera for steps 2-7 if not already recording
if (!isRecording) {
toggleCamera();
}
}
// Update visualization focus
if (currentStep > 1 && currentStep < 8) {
updateVisualization(currentFrameIndex);
}
}
// Camera handling
async function toggleCamera() {
const cameraContainer = document.querySelector('.camera-container');
const tryItBtn = document.getElementById('try-it-btn');
const visualizationContainer = document.querySelector('.visualization-container');
if (!isRecording) {
try {
// First check if we have camera permissions
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
if (videoDevices.length === 0) {
throw new Error('No camera devices found');
}
// Update layout for camera mode
visualizationContainer.style.display = 'grid';
visualizationContainer.style.gridTemplateColumns = '1fr 1fr';
visualizationContainer.style.gap = '20px';
visualizationContainer.style.alignItems = 'start';
// Move camera container inside visualization container
if (cameraContainer.parentElement !== visualizationContainer) {
visualizationContainer.appendChild(cameraContainer);
}
cameraContainer.style.display = 'flex';
// Get camera stream with more specific constraints
stream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: 640 },
height: { ideal: 480 },
facingMode: 'user',
frameRate: { ideal: 30 }
},
audio: false
});
const videoElement = document.getElementById('camera-feed');
videoElement.srcObject = stream;
videoElement.onloadedmetadata = () => {
videoElement.play();
cameraContainer.style.gridColumn = '2';
tryItBtn.textContent = 'Stop Recording';
isRecording = true;
// Start skeleton tracking only after video is ready
startSkeletonTracking();
};
} catch (error) {
console.error('Error accessing camera:', error);
let errorMessage = 'Unable to access camera. ';
if (error.name === 'NotAllowedError') {
errorMessage += 'Please grant camera permissions in your browser settings.';
} else if (error.name === 'NotFoundError') {
errorMessage += 'No camera device found. Please make sure your camera is connected.';
} else if (error.name === 'NotReadableError') {
errorMessage += 'Camera is in use by another application. Please close other applications using the camera.';
} else {
errorMessage += 'Please make sure you have granted camera permissions and no other application is using the camera.';
}
alert(errorMessage);
}
} else {
stopCamera();
// Reset layout
visualizationContainer.style.display = 'flex';
visualizationContainer.style.flexDirection = 'row';
}
}
function stopCamera() {
if (stream) {
stream.getTracks().forEach(track => track.stop());
stream = null;
}
if (poseDetector) {
poseDetector.dispose();
poseDetector = null;
}
// Hide camera container
document.querySelector('.camera-container').style.display = 'none';
// Center the reference visualization
const visualizationContainer = document.querySelector('.visualization-container');
visualizationContainer.style.display = 'flex';
visualizationContainer.style.justifyContent = 'center';
visualizationContainer.style.alignItems = 'center';
// Ensure reference visualization is centered
const referenceVis = document.getElementById('reference-vis');
referenceVis.style.display = 'flex';
referenceVis.style.flexDirection = 'column';
referenceVis.style.alignItems = 'center';
document.getElementById('try-it-btn').textContent = 'Try It!';
isRecording = false;
// Clear canvas overlays
const canvas = document.getElementById('pose-canvas');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Skeleton tracking with MediaPipe and OpenCV
async function startSkeletonTracking() {
try {
// Wait for OpenCV to be ready
if (!window.isOpenCvReady) {
await new Promise(resolve => {
window.addEventListener('opencv-ready', resolve, { once: true });
});
}
const videoElement = document.getElementById('camera-feed');
const cameraContainer = document.querySelector('.camera-container');
let canvas = document.getElementById('pose-canvas');
if (!canvas) {
canvas = document.createElement('canvas');
canvas.id = 'pose-canvas';
canvas.style.position = 'absolute';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.width = '100%';
canvas.style.height = '100%';
cameraContainer.appendChild(canvas);
}
// Initialize MediaPipe Pose with improved confidence thresholds
const pose = new Pose({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/pose/${file}`;
}
});
// Configure pose detection with better confidence thresholds
pose.setOptions({
modelComplexity: 1,
smoothLandmarks: true,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5
});
// Setup camera with improved frame processing
const camera = new Camera(videoElement, {
onFrame: async () => {
// Convert video frame to RGB for better detection
const ctx = canvas.getContext('2d');
ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Process frame with MediaPipe
await pose.send({image: imageData});
},
width: 640,
height: 480
});
// Handle pose detection results with improved visualization
pose.onResults((results) => {
if (results.poseLandmarks) {
const processedPose = processFrameWithOpenCV(results, videoElement);
drawPoseOnCanvas(processedPose || results, canvas);
// Convert to our skeleton format and update visualization
userSkeletonData = convertMediaPipePoseToSkeleton(processedPose || results);
updateVisualization(currentFrameIndex);
}
});
// Start camera
await camera.start();
} catch (error) {
console.error('Error initializing pose detector:', error);
alert('Error initializing pose detection. Please make sure you have a working camera and try again.');
}
}
// Process frame with OpenCV for improved tracking
function processFrameWithOpenCV(results, videoElement) {
try {
// Create OpenCV matrices
const frame = cv.imread(videoElement);
if (!results.poseLandmarks) {
if (lastValidPose) {
results.poseLandmarks = lastValidPose;
}
cv.imshow('pose-canvas', frame);
frame.delete();
return results;
}
// Convert landmarks to points for tracking
const currentPoints = results.poseLandmarks.map(lm => ({
x: Math.round(lm.x * frame.cols),
y: Math.round(lm.y * frame.cols),
visibility: lm.visibility
}));
if (lastFrame && lastValidPose) {
// Calculate optical flow for visible points
const prevPoints = lastValidPose.map(lm => ({
x: Math.round(lm.x * frame.cols),
y: Math.round(lm.y * frame.cols)
}));
// Convert points to OpenCV format
const prevPointsMat = cv.matFromArray(prevPoints.length, 1, cv.CV_32FC2,
prevPoints.flatMap(p => [p.x, p.y]));
const currPointsMat = cv.matFromArray(currentPoints.length, 1, cv.CV_32FC2,
currentPoints.flatMap(p => [p.x, p.y]));
// Calculate optical flow
const status = new cv.Mat();
const err = new cv.Mat();
const winSize = new cv.Size(15, 15);
const maxLevel = 2;
const criteria = new cv.TermCriteria(
cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 30, 0.01
);
cv.calcOpticalFlowPyrLK(lastFrame, frame, prevPointsMat, currPointsMat,
status, err, winSize, maxLevel, criteria);
// Update points based on optical flow
for (let i = 0; i < currentPoints.length; i++) {
if (status.data[i] === 1 && currentPoints[i].visibility > 0.5) {
results.poseLandmarks[i].x = currPointsMat.data32F[i * 2] / frame.cols;
results.poseLandmarks[i].y = currPointsMat.data32F[i * 2 + 1] / frame.rows;
}
}
// Clean up OpenCV matrices
prevPointsMat.delete();
currPointsMat.delete();
status.delete();
err.delete();
}
// Update motion history
motionHistory.push(results.poseLandmarks);
if (motionHistory.length > MOTION_HISTORY_SIZE) {
motionHistory.shift();
}
// Apply temporal smoothing
if (motionHistory.length === MOTION_HISTORY_SIZE) {
results.poseLandmarks = results.poseLandmarks.map((landmark, i) => {
const smoothed = {
x: 0,
y: 0,
z: landmark.z,
visibility: landmark.visibility
};
let totalWeight = 0;
motionHistory.forEach((hist, idx) => {
const weight = (idx + 1) / MOTION_HISTORY_SIZE;
smoothed.x += hist[i].x * weight;
smoothed.y += hist[i].y * weight;
totalWeight += weight;
});
smoothed.x /= totalWeight;
smoothed.y /= totalWeight;
return smoothed;
});
}
// Update last frame and pose
if (lastFrame) lastFrame.delete();
lastFrame = frame.clone();
lastValidPose = [...results.poseLandmarks];
frame.delete();
return results;
} catch (error) {
console.error('Error in OpenCV processing:', error);
return results;
}
}
// Update the drawPoseOnCanvas function with improved visualization
function drawPoseOnCanvas(results, canvas) {
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!results || !results.poseLandmarks) return;
const landmarks = results.poseLandmarks;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
// Improved drawing specifications
const connectionColor = '#e74c3c'; // Pink color for connections
const landmarkColor = '#e74c3c'; // White color for landmarks
// Draw pose connections with improved thickness
const connections = [
// Torso
[11, 12], // shoulders
[11, 23], // left shoulder to hip
[12, 24], // right shoulder to hip
[23, 24], // hips
// Arms
[11, 13], [13, 15], // left arm
[12, 14], [14, 16], // right arm
// Legs
[23, 25], [25, 27], [27, 31], // left leg
[24, 26], [26, 28], [28, 32], // right leg
];
// Draw connections with improved style
ctx.lineWidth = 2;
ctx.strokeStyle = connectionColor;
ctx.beginPath();
connections.forEach(([i, j]) => {
const point1 = landmarks[i];
const point2 = landmarks[j];
if (point1.visibility > 0.5 && point2.visibility > 0.5) {
ctx.moveTo(point1.x * canvasWidth, point1.y * canvasHeight);
ctx.lineTo(point2.x * canvasWidth, point2.y * canvasHeight);
}
});
ctx.stroke();
// Draw landmarks with improved visibility
landmarks.forEach((landmark, index) => {
// Skip facial landmarks (indices 0-10)
if (index <= 10) return;
if (landmark.visibility > 0.5) {
const x = Math.round(landmark.x * canvasWidth);
const y = Math.round(landmark.y * canvasHeight);
// Draw larger, more visible points
ctx.beginPath();
ctx.arc(x, y, 4, 0, 2 * Math.PI);
ctx.fillStyle = landmarkColor;
ctx.fill();
// Add white border for better visibility
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 1;
ctx.stroke();
}
});
}
// MediaPipe landmark index to joint name mapping
const mediapipeMap = {
'head': 0,
'left_shoulder': 11,
'right_shoulder': 12,
'left_elbow': 13,
'right_elbow': 14,
'left_wrist': 15,
'right_wrist': 16,
'left_hip': 23,
'right_hip': 24,
'left_knee': 25,
'right_knee': 26,
'left_ankle': 27,
'right_ankle': 28
};
// Convert MediaPipe pose to our skeleton format
function convertMediaPipePoseToSkeleton(results) {
const skeleton = {};
const landmarks = results.poseLandmarks;
// MediaPipe to our format mapping
const mapping = {
0: 'head', // nose
11: 'left_shoulder',
12: 'right_shoulder',
13: 'left_upper_arm', // left_elbow
14: 'right_upper_arm', // right_elbow
15: 'left_forearm', // left_wrist
16: 'right_forearm', // right_wrist
23: 'left_upper_leg', // left_hip
24: 'right_upper_leg', // right_hip
25: 'left_lower_leg', // left_knee
26: 'right_lower_leg', // right_knee
27: 'left_foot', // left_ankle
28: 'right_foot' // right_ankle
};
// Get video dimensions for proper scaling
const videoElement = document.getElementById('camera-feed');
const videoWidth = videoElement.videoWidth || videoElement.width;
const videoHeight = videoElement.videoHeight || videoElement.height;
// Convert landmarks to our format with proper scaling
Object.entries(mapping).forEach(([index, jointName]) => {
const landmark = landmarks[index];
if (landmark.visibility > 0.5) {
// Scale coordinates to match our visualization space
// MediaPipe gives coordinates in [0,1] range, we need to scale them appropriately
skeleton[`${jointName}_x`] = landmark.x;
// Flip Y coordinate since MediaPipe uses top-left origin and we use bottom-left
skeleton[`${jointName}_y`] = 1 - landmark.y;
}
});
// Add derived points with proper scaling
// Neck - midpoint between shoulders
if (skeleton['left_shoulder_x'] && skeleton['right_shoulder_x']) {
skeleton['neck_x'] = (skeleton['left_shoulder_x'] + skeleton['right_shoulder_x']) / 2;
skeleton['neck_y'] = (skeleton['left_shoulder_y'] + skeleton['right_shoulder_y']) / 2 + 0.05; // Adjust neck position up slightly
}
// Pelvis - midpoint between hips with adjusted height
if (skeleton['left_upper_leg_x'] && skeleton['right_upper_leg_x']) {
skeleton['pelvis_x'] = (skeleton['left_upper_leg_x'] + skeleton['right_upper_leg_x']) / 2;
skeleton['pelvis_y'] = (skeleton['left_upper_leg_y'] + skeleton['right_upper_leg_y']) / 2;
}
// Spine points with adjusted spacing
if (skeleton['neck_x'] && skeleton['pelvis_x']) {
const spinePoints = ['t8', 't12', 'l3', 'l5'];
const totalPoints = spinePoints.length + 1;
const spineLength = skeleton['neck_y'] - skeleton['pelvis_y'];
spinePoints.forEach((joint, index) => {
const ratio = (index + 1) / totalPoints;
// Use linear interpolation for X coordinates
skeleton[`${joint}_x`] = skeleton['neck_x'] * (1 - ratio) + skeleton['pelvis_x'] * ratio;
// Use curved interpolation for Y coordinates to create more natural spine curve
const t = ratio;
const curveY = t * t * (3 - 2 * t); // Smoothstep interpolation for more natural curve
skeleton[`${joint}_y`] = skeleton['neck_y'] - (spineLength * curveY);
});
}
return skeleton;
}
// Helper function to map our focus joints to MediaPipe indices
function getFocusJointIndices(focusJoints) {
const mediapipeMap = {
'head': 0,
'left_shoulder': 11,
'right_shoulder': 12,
'left_upper_arm': 13,
'right_upper_arm': 14,
'left_forearm': 15,
'right_forearm': 16,
'left_upper_leg': 23,
'right_upper_leg': 24,
'left_lower_leg': 25,
'right_lower_leg': 26,
'left_foot': 27,
'right_foot': 28
};
return focusJoints
.map(joint => mediapipeMap[joint])
.filter(index => index !== undefined);
}
// Create a playback control for animation
function createPlaybackControls() {
const controls = document.createElement('div');
controls.className = 'playback-controls';
// Play/Pause button
const playBtn = document.createElement('button');
playBtn.textContent = 'Play';
playBtn.id = 'play-pause-btn';
playBtn.addEventListener('click', togglePlayback);
// Frame counter
const counter = document.createElement('span');
counter.id = 'frame-counter';
counter.textContent = 'Frame 1 of 0';
counter.style.marginLeft = '10px';
// Add elements to controls
controls.appendChild(playBtn);
controls.appendChild(counter);
// Insert controls under reference visualization
const referenceVis = document.getElementById('reference-vis');
referenceVis.appendChild(controls);
}
// Toggle playback pause/resume
function togglePlayback() {
const playBtn = document.getElementById('play-pause-btn');
isPlaying = !isPlaying;
if (isPlaying) {
// Start animation
startAnimation();
playBtn.textContent = 'Pause';
} else {
// Pause animation
if (animationFrame) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}
playBtn.textContent = 'Play';
}
}
// Event listeners
document.getElementById('back-btn').addEventListener('click', () => updateStep(-1));
document.getElementById('next-btn').addEventListener('click', () => updateStep(1));
document.getElementById('next-btn').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
updateStep(0);
document.getElementById('next-btn').focus();
}
});
document.getElementById('try-it-btn').addEventListener('click', toggleCamera);
// Add window load event to ensure DOM is fully loaded
window.addEventListener('load', () => {
// Load participant metadata first
loadParticipantMetadata();
// Initialize participant selection form
initializeParticipantSelection();
// Create playback controls (they'll be hidden initially)
createPlaybackControls();
// Initialize first page
const tryItBtn = document.getElementById('try-it-btn');
tryItBtn.style.display = 'none';