-
Notifications
You must be signed in to change notification settings - Fork 160
/
main.js
154 lines (124 loc) · 4.5 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
147
148
149
150
151
152
153
154
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import "@babel/polyfill";
import * as mobilenetModule from '@tensorflow-models/mobilenet';
import * as tf from '@tensorflow/tfjs';
import * as knnClassifier from '@tensorflow-models/knn-classifier';
// Number of classes to classify
const NUM_CLASSES = 3;
// Webcam Image size. Must be 227.
const IMAGE_SIZE = 227;
// K value for KNN
const TOPK = 10;
class Main {
constructor() {
// Initiate variables
this.infoTexts = [];
this.training = -1; // -1 when no class is being trained
this.videoPlaying = false;
// Initiate deeplearn.js math and knn classifier objects
this.bindPage();
// Create video element that will contain the webcam image
this.video = document.createElement('video');
this.video.setAttribute('autoplay', '');
this.video.setAttribute('playsinline', '');
// Add video element to DOM
document.body.appendChild(this.video);
// Create training buttons and info texts
for (let i = 0; i < NUM_CLASSES; i++) {
const div = document.createElement('div');
document.body.appendChild(div);
div.style.marginBottom = '10px';
// Create training button
const button = document.createElement('button')
button.innerText = "Train " + i;
div.appendChild(button);
// Listen for mouse events when clicking the button
button.addEventListener('mousedown', () => this.training = i);
button.addEventListener('mouseup', () => this.training = -1);
// Create info text
const infoText = document.createElement('span')
infoText.innerText = " No examples added";
div.appendChild(infoText);
this.infoTexts.push(infoText);
}
// Setup webcam
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then((stream) => {
this.video.srcObject = stream;
this.video.width = IMAGE_SIZE;
this.video.height = IMAGE_SIZE;
this.video.addEventListener('playing', () => this.videoPlaying = true);
this.video.addEventListener('paused', () => this.videoPlaying = false);
})
}
async bindPage() {
this.knn = knnClassifier.create();
this.mobilenet = await mobilenetModule.load();
this.start();
}
start() {
if (this.timer) {
this.stop();
}
this.video.play();
this.timer = requestAnimationFrame(this.animate.bind(this));
}
stop() {
this.video.pause();
cancelAnimationFrame(this.timer);
}
async animate() {
if (this.videoPlaying) {
// Get image data from video element
const image = tf.fromPixels(this.video);
let logits;
// 'conv_preds' is the logits activation of MobileNet.
const infer = () => this.mobilenet.infer(image, 'conv_preds');
// Train class if one of the buttons is held down
if (this.training != -1) {
logits = infer();
// Add current image to classifier
this.knn.addExample(logits, this.training)
}
const numClasses = this.knn.getNumClasses();
if (numClasses > 0) {
// If classes have been added run predict
logits = infer();
const res = await this.knn.predictClass(logits, TOPK);
for (let i = 0; i < NUM_CLASSES; i++) {
// The number of examples for each class
const exampleCount = this.knn.getClassExampleCount();
// Make the predicted class bold
if (res.classIndex == i) {
this.infoTexts[i].style.fontWeight = 'bold';
} else {
this.infoTexts[i].style.fontWeight = 'normal';
}
// Update info text
if (exampleCount[i] > 0) {
this.infoTexts[i].innerText = ` ${exampleCount[i]} examples - ${res.confidences[i] * 100}%`
}
}
}
// Dispose image when done
image.dispose();
if (logits != null) {
logits.dispose();
}
}
this.timer = requestAnimationFrame(this.animate.bind(this));
}
}
window.addEventListener('load', () => new Main());