-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (49 loc) · 1.72 KB
/
index.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
const webcamElement = document.getElementById('webcam');
const classifier = knnClassifier.create();
async function setupWebcam() {
return new Promise((resolve, reject) => {
const navigatorAny = navigator;
navigator.getUserMedia =
navigator.getUserMedia ||
navigatorAny.webkitGetUserMedia ||
navigatorAny.mozGetUserMedia ||
navigatorAny.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia(
{video: true},
stream => {
webcamElement.srcObject = stream;
webcamElement.addEventListener('loadeddata', () => resolve(), false);
},
() => reject()
);
} else {
reject();
}
});
}
async function app() {
const net = await mobilenet.load();
await setupWebcam();
const addExample = classId => {
const activation = net.infer(webcamElement, 'conv_preds');
classifier.addExample(activation, classId);
};
document.getElementById('class-a').addEventListener('click', () => addExample(0));
document.getElementById('class-b').addEventListener('click', () => addExample(1));
document.getElementById('class-c').addEventListener('click', () => addExample(2));
document.getElementById('class-no-action').addEventListener('click', () => addExample(3));
while (true) {
if (classifier.getNumClasses() > 0) {
const activation = net.infer(webcamElement, 'conv_preds');
const result = await classifier.predictClass(activation);
const classes = ['A', 'B', 'C', 'No action'];
document.getElementById('console').innerText = `
prediction: ${classes[result.classIndex]}
probability: ${result.confidences[result.classIndex]}
`;
}
await tf.nextFrame();
}
}
app();