-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.js
More file actions
101 lines (86 loc) · 2.77 KB
/
Copy pathapp.js
File metadata and controls
101 lines (86 loc) · 2.77 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
const classifier = knnClassifier.create();
const webcamElement = document.getElementById("webcam");
const trainLeftButton = document.getElementById("left");
const trainMiddleButton = document.getElementById("middle");
const trainRightButton = document.getElementById("right");
const predictionInfo = document.getElementById("predictionInfo");
const CLASSES = ["L", "M", "R"];
const getClassIndex = (classItem) => {
return CLASSES.indexOf(classItem);
};
let net;
async function loadModel() {
net = await mobilenet.load();
modelInfo.style.display = "none";
}
async function bindTraining() {
// reads an image from the webcam and associates it with a specific class index.
function addExample(elementId) {
// get the intermediate activation of MobileNet 'conv_preds' and pass that to the KNN classifier.
const activation = net.infer(webcamElement, "conv_preds");
// passs the intermediate activation to the classifier.
classifier.addExample(activation, elementId);
}
// add example
trainLeftButton.addEventListener("click", () =>
addExample(getClassIndex("L"))
);
trainMiddleButton.addEventListener("click", () =>
addExample(getClassIndex("M"))
);
trainRightButton.addEventListener("click", () =>
addExample(getClassIndex("R"))
);
while (true) {
if (classifier.getNumClasses() > 0) {
// get the activation from mobilenet from the webcam.
const activation = net.infer(webcamElement, "conv_preds");
// get the most likely class and confidences from the classifier module.
const result = await classifier.predictClass(activation);
predictionInfo.innerText = `
prediction: ${CLASSES[result.classIndex]}\n
probability: ${result.confidences[result.classIndex] * 100}%
`;
// game controller
if (CLASSES[result.classIndex] == "L") {
$.state.keypress.left = true;
$.state.keypress.right = false;
}
if (CLASSES[result.classIndex] == "R") {
$.state.keypress.left = false;
$.state.keypress.right = true;
}
}
await tf.nextFrame();
}
}
async function loadWebcam() {
webcamInfo.style.display = "block";
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
await navigator.mediaDevices
.getUserMedia({
audio: false,
video: {
facingMode: "user",
},
})
.then((stream) => {
window.stream = stream;
webcamElement.srcObject = stream;
return new Promise((resolve, reject) => {
webcamElement.onloadedmetadata = () => {
resolve();
webcamInfo.style.display = "none";
};
});
});
}
}
async function init() {
await loadModel();
loadWebcam();
bindTraining();
drawBg();
draw();
}
window.onload = init();