-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
79 lines (63 loc) · 2.71 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const webcamDisplay = document.getElementById('webcam');
webcamDisplay.classList.add('col', 's12');
// webcamDisplay.clientWidth = '100%';
// webcamDisplay.clientHeight = '50%';
const classifier = knnClassifier.create();
let net;
const app = async () => {
document.getElementById("postload").hidden = true;
net = await mobilenet.load();
document.getElementById("preload").hidden = true;
document.getElementById("postload").hidden = false;
console.log('calling setupWebcam')
await setupWebcam();
console.log("Webcam call successful")
// Reads an image from the webcam and associates it with a specific class
// index.
const addExample = classId => {
// Get the intermediate activation of MobileNet 'conv_preds' and pass that
// to the KNN classifier.
const activation = net.infer(webcamDisplay, 'conv_preds');
// Pass the intermediate activation to the classifier.
classifier.addExample(activation, classId);
};
// When clicking a button, add an example for that class.
document.getElementById('class-a').addEventListener('click', () => addExample(0));
document.getElementById('class-b').addEventListener('click', () => addExample(1));
document.getElementById('class-c').addEventListener('click', () => addExample(2));
while (true) {
if (classifier.getNumClasses() > 0) {
// Get the activation from mobilenet from the webcam.
const activation = net.infer(webcamDisplay, 'conv_preds');
// Get the most likely class and confidences from the classifier module.
const result = await classifier.predictClass(activation,3);
const classes = ['A', 'B', 'C'];
document.getElementById('console').innerText = `Prediction: ${classes[result.classIndex]}\nProbability: ${result.confidences[result.classIndex]}
`;
await tf.nextFrame();
}
await tf.nextFrame();
}
};
const setupWebcam = async () => {
const navigatorAny = navigator;
navigator.getUserMedia = navigator.getUserMedia ||
navigatorAny.webkitGetUserMedia||
navigatorAny.mozGetUserMedia ||
navigatorAny.msGetUserMedia;
if(navigator.getUserMedia) {
navigator.getUserMedia({video: true}, (videoStream) => {
webcamDisplay.srcObject = videoStream;
webcamDisplay.addEventListener('loadeddata', (success) => console.log('video loaded'))
}, (error) => {
alert('Error occured please check the logs in console');
console.error(error);
})
} else {
((error) => {
alert('Error occured please check the logs in console');
console.error(error);
})();
}
};
app();