-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (118 loc) · 3.97 KB
/
main.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
import "@babel/polyfill";
import * as mobilenetModule from "@tensorflow-models/mobilenet";
import * as tf from "@tensorflow/tfjs";
import * as knnClassifier from "@tensorflow-models/knn-classifier";
import { repeatGameState, setClassesProbability } from "./basic.js";
const NUM_CLASSES = 3;
const CLASSES_NAME = ["LEFT", "STRAIGHT", "RIGHT"];
const CLASSES_PROBABILITY = [0, 0, 0];
const ELEMENT_TAGS = [];
const IMAGE_SIZE = 227;
const TOPK = 10;
class Main {
constructor() {
this.training = -1;
this.videoPlaying = false;
this.count = 0;
//Setting Up Video tag
this.video = document.createElement("video");
this.video.className = "webCam";
this.video.setAttribute("autoplay", "");
this.video.setAttribute("playsinline", "");
document.body.appendChild(this.video);
this.video.addEventListener("playing", () => {
this.videoPlaying = true;
});
this.video.addEventListener("pause", () => {
this.videoPlaying = false;
});
let mainControls = document.createElement("ul");
mainControls.className = "controls";
for (let i = 0; i < NUM_CLASSES; i++) {
let container = document.createElement("li");
container.className = "controls-container";
let elm = document.createElement("button");
elm.className = "controls-container-button";
elm.innerHTML = CLASSES_NAME[i];
elm.addEventListener("mousedown", () => {
this.training = i;
});
elm.addEventListener("mouseup", () => {
this.training = -1;
});
let text = document.createElement("span");
text.innerHTML = "0%";
container.appendChild(elm);
container.appendChild(text);
ELEMENT_TAGS.push(text);
mainControls.appendChild(container);
}
document.body.appendChild(mainControls);
// All critical func's
this.executeInOrder();
}
async executeInOrder() {
await this.setupMediaDevices();
await this.initializeModel();
this.start();
}
async setupMediaDevices() {
let stream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: false,
});
this.video.width = IMAGE_SIZE;
this.video.height = IMAGE_SIZE;
this.video.srcObject = stream;
}
async initializeModel() {
this.knn = knnClassifier.create();
this.mobilenet = await mobilenetModule.load();
}
start() {
this.video.play();
this.timer = requestAnimationFrame(this.getDirection.bind(this));
}
getDirection() {
try {
repeatGameState();
if (this.videoPlaying) {
const image = tf.fromPixels(this.video);
let logits = this.mobilenet.infer(image, "conv_preds");
// training
if (this.training != -1) {
this.knn.addExample(logits, this.training);
}
if (this.knn.getNumClasses() > 0) {
this.knn.predictClass(logits, TOPK).then((res) => {
if (logits) logits.dispose();
// The number of examples for each class
const exampleCount = this.knn.getClassExampleCount();
for (let i = 0; i < NUM_CLASSES; i++) {
if (i != res.classIndex) {
ELEMENT_TAGS[i].style.color = "black";
ELEMENT_TAGS[i].style.fontWeight = "";
} else {
ELEMENT_TAGS[i].style.color = "red";
ELEMENT_TAGS[i].style.fontWeight = "bold";
}
if (i in res.confidences) {
ELEMENT_TAGS[i].innerHTML = ` ${exampleCount[i]} examples - ${
res.confidences[i] * 100
}%`;
CLASSES_PROBABILITY[i] = +res.confidences[i];
}
}
setClassesProbability(CLASSES_PROBABILITY);
});
}
image.dispose();
}
this.timer = requestAnimationFrame(this.getDirection.bind(this));
} catch (err) {
console.log(err);
this.timer = requestAnimationFrame(this.getDirection.bind(this));
}
}
}
window.addEventListener("load", () => new Main());