-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
515 lines (438 loc) · 15.1 KB
/
sketch.js
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
let video;
let poseNet;
let poses = [];
let poseHistory = [];
let currentStream;
let usingFrontCamera = false;
let recognizing = false;
let speechRecognizer;
let captionsDiv;
let captionsLines = []; // Array to hold lines of captions
let confidenceLevel = 0.9;
let gainValue = 4.0; // Default gain value
let audioContext;
let gainNode;
let mediaStreamSource;
let analyser;
let dataArray;
let peakValue = 0;
let peakTimestamp = 0;
let maxLines = 14; // Maximum number of lines to display
function setup() {
createCanvas(windowWidth, windowHeight + 50); // Added extra height
setupCamera();
const confidenceDisplay = select('#confidenceLevel');
confidenceDisplay.mousePressed(toggleControlPopup);
const switchButton = select('#switchCamera');
switchButton.mousePressed(switchCamera);
const toggleCaptionsButton = select('#toggleCaptions');
toggleCaptionsButton.mousePressed(toggleCaptions);
const confidenceSlider = select('#confidenceSlider');
confidenceSlider.input(updateSliderValue);
const gainSlider = select('#gainSlider');
gainSlider.attribute('min', 0.1);
gainSlider.attribute('max', 10);
gainSlider.value(gainValue);
gainSlider.input(updateGainValue);
const confirmPopupButton = select('#confirmPopup');
confirmPopupButton.mousePressed(confirmPopup);
const closePopupButton = select('#closePopup');
closePopupButton.mousePressed(hideControlPopup);
captionsDiv = select('#captions');
captionsDiv.mousePressed(hideCaptions);
// Load settings from local storage
loadSettings();
// Enable text recognition by default
startSpeechRecognition();
// Hide the control popup initially
hideControlPopup();
// Listen for visual viewport changes
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', viewportResized);
}
// Reload the page on orientation change to fix pose alignment
window.addEventListener('orientationchange', function() {
window.location.reload();
});
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight + 50); // Added extra height
}
function viewportResized() {
resizeCanvas(windowWidth, windowHeight + 50); // Added extra height
}
function setupCamera() {
if (currentStream) {
currentStream.getTracks().forEach(track => track.stop());
}
let constraints = {
video: {
facingMode: usingFrontCamera ? 'user' : 'environment',
width: { ideal: 1280 },
height: { ideal: 720 }
},
audio: false
};
video = createCapture(constraints);
video.hide();
video.elt.setAttribute('playsinline', 'true');
video.elt.onloadedmetadata = function() {
video.loadedmetadata = true;
currentStream = video.elt.srcObject;
// Initialize PoseNet after the video is ready
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', poseEventHandler);
// Log video dimensions for debugging
console.log('Video width:', video.width);
console.log('Video height:', video.height);
};
}
function poseEventHandler(results) {
poses = results;
poseHistory.push({ poses: results, timestamp: millis() });
poseHistory = poseHistory.filter(entry => millis() - entry.timestamp <= 2000);
}
function switchCamera() {
usingFrontCamera = !usingFrontCamera;
// Stop current video stream
if (currentStream) {
currentStream.getTracks().forEach(track => track.stop());
}
// Remove old PoseNet instance
if (poseNet) {
poseNet.removeListener('pose', poseEventHandler);
poseNet = null;
}
// Remove old video element
if (video && video.remove) {
video.remove();
}
setupCamera();
}
function modelReady() {
console.log("Model Ready");
}
function draw() {
background(0);
// Ensure the video metadata is loaded
if (video.loadedmetadata) {
// Update video.width and video.height
video.width = video.elt.videoWidth;
video.height = video.elt.videoHeight;
let videoAspect = video.width / video.height;
let canvasAspect = width / height;
let videoWidth, videoHeight;
if (canvasAspect > videoAspect) {
// Canvas is wider than video
videoHeight = height;
videoWidth = videoHeight * videoAspect;
} else {
// Canvas is taller than video
videoWidth = width;
videoHeight = videoWidth / videoAspect;
}
// Adjust video size in landscape mode
if (windowWidth > windowHeight) {
let extraHeight = 50; // Amount of extra height added
videoHeight += extraHeight;
videoWidth = videoHeight * videoAspect;
}
let x = (width - videoWidth) / 2;
let y = (height - videoHeight) / 2;
// Draw the video on the canvas
image(video, x, y, videoWidth, videoHeight);
// Draw keypoints and skeletons
drawKeypoints(x, y, videoWidth, videoHeight);
drawSkeletons(x, y, videoWidth, videoHeight);
}
if (isVuMeterVisible()) {
updateVuMeter();
}
}
function drawKeypoints(xOffset, yOffset, videoWidth, videoHeight) {
for (let historyEntry of poseHistory) {
let ageFactor = (millis() - historyEntry.timestamp) / 2000;
for (let i = 0; i < historyEntry.poses.length; i++) {
const pose = historyEntry.poses[i].pose;
for (let j = 0; j < pose.keypoints.length; j++) {
const keypoint = pose.keypoints[j];
if (keypoint.score > confidenceLevel) {
let x = map(keypoint.position.x, 0, video.width, xOffset, xOffset + videoWidth);
let y = map(keypoint.position.y, 0, video.height, yOffset, yOffset + videoHeight);
if (keypoint.part === 'leftEye' || keypoint.part === 'rightEye') {
drawEye(x, y, ageFactor);
} else if (keypoint.part === 'nose') {
drawNose(x, y, ageFactor);
} else if (keypoint.part === 'leftEar' || keypoint.part === 'rightEar') {
drawEar(x, y, ageFactor);
} else {
fill(255, 0, 0);
noStroke();
ellipse(x, y, 5, 5);
}
}
}
}
}
}
function drawSkeletons(xOffset, yOffset, videoWidth, videoHeight) {
for (let historyEntry of poseHistory) {
let ageFactor = (millis() - historyEntry.timestamp) / 2000;
for (let i = 0; i < historyEntry.poses.length; i++) {
const skeleton = historyEntry.poses[i].skeleton;
let baseColor = getBaseColor(i);
for (let j = 0; j < skeleton.length; j++) {
const partA = skeleton[j][0];
const partB = skeleton[j][1];
let x1 = map(partA.position.x, 0, video.width, xOffset, xOffset + videoWidth);
let y1 = map(partA.position.y, 0, video.height, yOffset, yOffset + videoHeight);
let x2 = map(partB.position.x, 0, video.width, xOffset, xOffset + videoWidth);
let y2 = map(partB.position.y, 0, video.height, yOffset, yOffset + videoHeight);
stroke(lerpColor(baseColor, color(0, 0, 0), ageFactor));
line(x1, y1, x2, y2);
}
}
}
}
function isVuMeterVisible() {
const controlPopup = document.getElementById('controlPopup');
return controlPopup.style.display !== 'none';
}
function getBaseColor(index) {
if (index === 0) return color(255, 0, 0);
let hueValue = (index * 60) % 360;
return color('hsb(' + hueValue + ', 100%, 50%)');
}
function drawEye(x, y, ageFactor) {
let eyeColor = lerpColor(color(255), color(0), ageFactor);
fill(eyeColor);
stroke(0);
strokeWeight(1);
ellipse(x, y, 10, 10);
fill(0);
noStroke();
ellipse(x, y, 3, 3);
}
function drawNose(x, y, ageFactor) {
let noseColor = lerpColor(color(255, 204, 0), color(0), ageFactor);
fill(noseColor);
noStroke();
ellipse(x, y, 8, 8);
}
function drawEar(x, y, ageFactor) {
let earColor = lerpColor(color(255, 204, 0), color(0), ageFactor);
fill(earColor);
noStroke();
ellipse(x, y, 8, 16);
}
function toggleCaptions() {
if (recognizing) {
speechRecognizer.stop();
if (audioContext) {
audioContext.close().then(() => {
audioContext = null;
if (mediaStreamSource) {
mediaStreamSource.mediaStream.getTracks().forEach(track => track.stop());
mediaStreamSource = null;
}
});
}
recognizing = false;
select('#toggleCaptions').html('🤐');
} else {
startSpeechRecognition();
recognizing = true;
select('#toggleCaptions').html('💬');
}
}
function startSpeechRecognition() {
if ('webkitSpeechRecognition' in window) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
gainNode = audioContext.createGain();
gainNode.gain.value = gainValue; // Use the current gain value
navigator.mediaDevices.getUserMedia({ audio: true }).then(function(stream) {
mediaStreamSource = audioContext.createMediaStreamSource(stream);
gainNode.gain.value = gainValue; // Ensure gain is set correctly
mediaStreamSource.connect(gainNode);
// Set up the analyser node
analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
gainNode.connect(analyser);
dataArray = new Uint8Array(analyser.frequencyBinCount);
// Create a new MediaStream with the adjusted gain node
const destinationStream = audioContext.createMediaStreamDestination();
gainNode.connect(destinationStream);
speechRecognizer = new webkitSpeechRecognition();
speechRecognizer.continuous = true;
speechRecognizer.interimResults = true;
speechRecognizer.lang = 'en-US';
speechRecognizer.maxAlternatives = 3; // Increased from 1 to 3
speechRecognizer.onstart = function() {
recognizing = true;
select('#toggleCaptions').html('💬');
};
speechRecognizer.onend = function() {
recognizing = false;
select('#toggleCaptions').html('🤐');
// Restart recognition to keep listening
if (recognizing) {
startSpeechRecognition();
}
};
speechRecognizer.onresult = function(event) {
let interimTranscript = '';
let finalTranscript = '';
for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
finalTranscript += event.results[i][0].transcript + ' ';
} else {
interimTranscript += event.results[i][0].transcript + ' ';
}
}
appendCaptions(finalTranscript); // Append the final transcript to captions
updateCaptions();
};
// Start the speech recognition
speechRecognizer.start();
}).catch(function(err) {
console.log('Error accessing the microphone: ' + err);
});
} else {
console.log('Speech recognition not supported.');
}
}
function appendCaptions(newText) {
// Split the new text into lines based on the available width
let words = newText.split(' ');
let line = '';
words.forEach(word => {
let testLine = line + word + ' ';
let testWidth = textWidth(testLine);
if (testWidth > width - 20 && line.length > 0) {
captionsLines.push(line.trim());
line = word + ' ';
} else {
line = testLine;
}
});
if (line.length > 0) {
captionsLines.push(line.trim());
}
// Ensure we only keep the last `maxLines` lines
if (captionsLines.length > maxLines) {
captionsLines = captionsLines.slice(captionsLines.length - maxLines);
}
}
function updateCaptions() {
captionsDiv.html(''); // Clear current captions
let opacityStep = 1 / maxLines;
captionsLines.forEach((line, index) => {
let lineElement = createP(line);
lineElement.style('margin', '0');
lineElement.style('padding', '0');
lineElement.style('opacity', (1 - (opacityStep * (maxLines - index))).toFixed(2));
captionsDiv.child(lineElement);
});
captionsDiv.style('white-space', 'normal'); // Allow captions to wrap to multiple lines if needed
}
function hideCaptions() {
captionsDiv.html(''); // Clear current captions
}
function updateSliderValue() {
let slider = select('#confidenceSlider');
let newConfidence = slider.value();
select('#popupConfidenceLevel').html(newConfidence + '%');
}
function updateGainValue() {
let slider = select('#gainSlider');
let newGain = slider.value();
gainValue = parseFloat(newGain);
select('#popupGainLevel').html(newGain.toFixed(1));
if (gainNode) {
gainNode.gain.value = gainValue;
}
}
function confirmPopup() {
let slider = select('#confidenceSlider');
confidenceLevel = slider.value() / 100;
updateConfidenceLevel();
saveSettings();
hideControlPopup(); // Hide the popup after saving
}
function updateConfidenceLevel() {
select('#confidenceLevel').html((confidenceLevel * 100).toFixed(0) + '%');
select('#popupConfidenceLevel').html((confidenceLevel * 100).toFixed(0) + '%');
}
function toggleControlPopup() {
let controlPopup = select('#controlPopup');
if (controlPopup.style('display') === 'none') {
showControlPopup();
} else {
hideControlPopup();
}
}
function showControlPopup() {
select('#confidenceSlider').value(confidenceLevel * 100);
select('#popupConfidenceLevel').html((confidenceLevel * 100).toFixed(0) + '%');
select('#gainSlider').value(gainValue);
select('#popupGainLevel').html(gainValue.toFixed(1));
select('#controlPopup').style('display', 'flex');
// Disable pointer events on the canvas
select('canvas').style('pointer-events', 'none');
}
function hideControlPopup() {
select('#controlPopup').style('display', 'none');
// Enable pointer events on the canvas
select('canvas').style('pointer-events', 'auto');
}
function saveSettings() {
localStorage.setItem('confidenceLevel', confidenceLevel);
localStorage.setItem('gainValue', gainValue);
}
function loadSettings() {
const savedConfidenceLevel = localStorage.getItem('confidenceLevel');
const savedGainValue = localStorage.getItem('gainValue');
if (savedConfidenceLevel !== null) {
confidenceLevel = parseFloat(savedConfidenceLevel);
updateConfidenceLevel();
select('#confidenceSlider').value(confidenceLevel * 100); // Update the slider value
}
if (savedGainValue !== null) {
gainValue = parseFloat(savedGainValue);
select('#popupGainLevel').html(gainValue.toFixed(1));
select('#gainSlider').value(gainValue); // Update the slider value
} else {
select('#gainSlider').value(gainValue); // Ensure default gain value is set if no saved value
}
}
function updateVuMeter() {
if (analyser && recognizing) {
analyser.getByteFrequencyData(dataArray);
let volume = Math.max(...dataArray) / 256;
let vuMeterFill = select('#audioLevelIndicator');
if (volume > peakValue) {
peakValue = volume;
peakTimestamp = millis();
}
let clipping = volume >= 1.0;
vuMeterFill.style('width', (volume * 100) + '%');
vuMeterFill.style('background-color', clipping ? 'red' : 'green');
// Display the peak value
let peakLine = select('#peakLine');
if (millis() - peakTimestamp <= 1000) {
peakLine.style('left', (peakValue * 100) + '%');
peakLine.style('background-color', 'yellow');
} else {
peakValue = 0;
peakLine.style('left', '0%');
}
} else {
// Set VU meter to zero when mic is turned off
let vuMeterFill = select('#audioLevelIndicator');
vuMeterFill.style('width', '0%');
vuMeterFill.style('background-color', 'green');
let peakLine = select('#peakLine');
peakLine.style('left', '0%');
peakValue = 0;
}
}