-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (62 loc) · 2.1 KB
/
app.js
File metadata and controls
78 lines (62 loc) · 2.1 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
navigator.mediaDevices.getUserMedia =
navigator.mediaDevices.getUserMedia ||
navigator.mediaDevices.webkitGetUserMedia ||
navigator.mediaDevices.mozGetUserMedia ||
navigator.mediaDevices.msGetUserMedia;
const webcamVideo = document.querySelector('.webcamVideo');
const playbackVideo = document.querySelector('.playbackVideo');
const canvas = document.querySelector('.canvas');
const context = canvas.getContext('2d');
let model;
playbackVideo.style.display = 'none';
const modelParams = {
flipHorizontal: true,
maxNumBoxes: 1,
iouThreshold: 0.5,
scoreThreshold: 0.6,
};
handTrack.startVideo(webcamVideo)
.then(status => {
if (status) {
navigator.mediaDevices.getUserMedia({ video: {} })
.then(stream => {
webcamVideo.srcObject = stream;
startDetection();
})
.catch(err => console.log('Error accessing webcam: ', err));
} else {
console.log('Please enable webcam access');
}
});
function runDetection() {
model.detect(webcamVideo)
.then(predictions => {
console.log(predictions);
context.clearRect(0, 0, canvas.width, canvas.height);
model.renderPredictions(predictions, canvas, context, webcamVideo);
if (predictions.length > 0) {
const handGesture = predictions[0].label;
if(handGesture === 'open'){
playbackVideo.style.display = 'block';
playbackVideo.play();
}
if(handGesture === 'closed'){
playbackVideo.pause();
}
}
});
}
handTrack.load(modelParams)
.then(loaded_model => {
model = loaded_model;
console.log("Handtrack.js model loaded");
});
function startDetection() {
webcamVideo.addEventListener('loadeddata', () => {
canvas.width = webcamVideo.videoWidth;
canvas.height = webcamVideo.videoHeight;
setInterval(() => {
runDetection();
}, 100);
});
}