-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
407 lines (345 loc) · 11.5 KB
/
demo.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const VIDEO = document.getElementById("webcam");
const eye = document.getElementById("eye");
const ENABLE_CAM_BUTTON = document.getElementById("enableCam");
const RESET_BUTTON = document.getElementById("reset");
let editClass = document.querySelectorAll("#edit");
const TRAIN_BUTTON = document.getElementById("train");
let toaddafter = document.querySelector(".classes");
let alladdbtn = document.querySelectorAll(".add");
let wrapper = document.querySelector(".wrapper");
let dataC = document.querySelectorAll("[data-c]");
const modelSaveButton = document.getElementById("saveModel");
let datavce = document.querySelector("[data-vce]");
let expand = document.getElementById("expand");
let bars = document.querySelectorAll(".bar");
let names = document.querySelectorAll(".names");
let trainbar = document.querySelector(".pbar");
let classcount = 2;
let dataCount = 1;
const preview = document.querySelectorAll(".preview");
let datapr = document.querySelector("[data-pr]");
const addClass = document.getElementById("addcls");
const MOBILE_NET_INPUT_WIDTH = 224;
const MOBILE_NET_INPUT_HEIGHT = 224;
const STOP_DATA_GATHER = -1;
const CLASS_NAMES = [];
let voiceState = false;
expand.addEventListener("click", () => {
wrapper.classList.toggle("shrink");
});
let hiddenClasses = document.querySelectorAll("[data-hidden]");
let box1 = document.querySelector("[data-show]");
let box2 = document.querySelector("[data-hide]");
ENABLE_CAM_BUTTON.addEventListener("click", enableCam);
TRAIN_BUTTON.addEventListener("click", trainAndPredict);
RESET_BUTTON.addEventListener("click", reset);
var videoPlaying = false;
let loader = document.querySelector(".loader");
// Just add more buttons in HTML to allow classification of more classes of data!
let dataCollectorButtons = document.querySelectorAll(".add");
dataCollectorButtons.forEach((btn) => {
btn.addEventListener("mousedown", gatherDataForClass);
btn.addEventListener("mouseup", gatherDataForClass);
CLASS_NAMES.push(btn.getAttribute("data-name"));
});
let mobilenet = undefined;
let gatherDataState = STOP_DATA_GATHER;
let trainingDataInputs = [];
let trainingDataOutputs = [];
let examplesCount = [];
let predict = false;
function removeOpacity() {
alladdbtn.forEach((btn) => {
btn.classList.remove("opc");
});
}
function addOpacity() {
alladdbtn.forEach((btn) => {
btn.classList.add("opc");
});
}
async function loadMobileNetFeatureModel() {
const URL =
"https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v3_small_100_224/feature_vector/5/default/1";
let loadingPercentage = 0;
mobilenet = await tf.loadGraphModel(URL, {
fromTFHub: true,
onProgress: function (fraction) {
loadingPercentage = Math.round(fraction * 100);
document.querySelector(
"[data-load]"
).innerHTML = `Loading... ${loadingPercentage}%`;
},
});
loader.style.display = "none";
tf.tidy(function () {
let answer = mobilenet.predict(
tf.zeros([1, MOBILE_NET_INPUT_HEIGHT, MOBILE_NET_INPUT_WIDTH, 3])
);
});
}
loadMobileNetFeatureModel();
let model = tf.sequential();
model.add(
tf.layers.dense({ inputShape: [1024], units: 128, activation: "relu" })
);
model.add(
tf.layers.dense({ units: CLASS_NAMES.length, activation: "softmax" })
);
model.summary();
model.compile({
optimizer: "adam",
loss:
CLASS_NAMES.length === 2 ? "binaryCrossentropy" : "categoricalCrossentropy",
// As this is a classification problem you can record accuracy in the logs too!
metrics: ["accuracy"],
});
function hasGetUserMedia() {
return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}
let isCameraFlipped = false;
let mediaStream;
function enableCam() {
showCamera("none", "flex");
if (hasGetUserMedia()) {
// getUserMedia parameters.
const constraints = {
video: {
facingMode: isCameraFlipped ? "user" : "environment", // Use the front camera if flipped
width: 640,
height: 480,
},
};
// Activate the webcam stream.
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
mediaStream = stream;
VIDEO.srcObject = stream;
removeOpacity();
VIDEO.addEventListener("loadeddata", function () {
VIDEO.style.transform = "scaleX(-1)";
// VIDEO.style.transform = isCameraFlipped ? "scaleX(-1)" : "scaleX(1)";
videoPlaying = true;
});
});
} else {
console.warn("getUserMedia() is not supported by your browser");
}
}
// Button click event listener for flipping the camera
const flipCameraButton = document.getElementById("record");
flipCameraButton.addEventListener("click", function () {
isCameraFlipped = !isCameraFlipped; // Toggle the camera flipped flag
mediaStream.getTracks().forEach(function (track) {
track.stop(); // Stop the current media stream
});
enableCam(); // Re-enable the camera with the updated settings
});
// Initial camera setup
function showCamera(hide, show) {
box1.style.display = hide;
box2.style.display = show;
}
document.getElementById("can").addEventListener("click", close1);
function close1() {
showCamera("flex", "none");
stopVideoStream();
addOpacity();
reset();
resetBarsAndBtns();
}
function stopVideoStream() {
const stream = VIDEO.srcObject;
const tracks = stream.getTracks();
console.log(VIDEO);
tracks.forEach(function (track) {
track.stop();
});
VIDEO.srcObject = null;
videoPlaying = false;
}
let toapend;
function gatherDataForClass() {
let classNumber = parseInt(this.getAttribute("data-1hot"));
toapend = this.parentElement.querySelector(".preview");
gatherDataState =
gatherDataState === STOP_DATA_GATHER ? classNumber : STOP_DATA_GATHER;
dataGatherLoop();
}
function calculateFeaturesOnCurrentFrame() {
return tf.tidy(function () {
// Grab pixels from current VIDEO frame.
let videoFrameAsTensor = tf.browser.fromPixels(VIDEO);
// Resize video frame tensor to be 224 x 224 pixels which is needed by MobileNet for input.
let resizedTensorFrame = tf.image.resizeBilinear(
videoFrameAsTensor,
[MOBILE_NET_INPUT_HEIGHT, MOBILE_NET_INPUT_WIDTH],
true
);
let normalizedTensorFrame = resizedTensorFrame.div(255);
return mobilenet.predict(normalizedTensorFrame.expandDims()).squeeze();
});
}
var editableElements = document.getElementsByClassName("cls");
editableElements.forEach((elem, i) => {
elem.addEventListener("input", (e) => {
handleEdit(e, i);
});
});
function handleEdit(event, i) {
var editedText = event.target.innerText;
CLASS_NAMES.splice(i, 1, editedText);
}
// Create a canvas element to convert the image data to a data URL
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
canvas.width = MOBILE_NET_INPUT_WIDTH;
canvas.height = MOBILE_NET_INPUT_HEIGHT;
function dataGatherLoop() {
// Only gather data if webcam is on and a relevant button is pressed.
if (videoPlaying && gatherDataState !== STOP_DATA_GATHER) {
// Ensure tensors are cleaned up.
let imageFeatures = calculateFeaturesOnCurrentFrame();
trainingDataInputs.push(imageFeatures);
trainingDataOutputs.push(gatherDataState);
// Draw the video frame on the canvas
context.drawImage(
VIDEO,
0,
0,
MOBILE_NET_INPUT_WIDTH,
MOBILE_NET_INPUT_HEIGHT
);
// Create an <img> element and set its src attribute to the captured image data
const capturedImage = document.createElement("img");
capturedImage.src = canvas.toDataURL();
// Append the image element to the imageContainer div
toapend.scrollTop = toapend.scrollHeight;
toapend.append(capturedImage);
// Initialize array index element if currently undefined
if (examplesCount[gatherDataState] === undefined) {
examplesCount[gatherDataState] = 0;
}
// Increment counts of examples for user interface to show
examplesCount[gatherDataState]++;
for (let n = 0; n < CLASS_NAMES.length; n++) {
dataC[n].innerHTML = examplesCount[n] || "0";
}
window.requestAnimationFrame(dataGatherLoop);
}
}
async function trainAndPredict() {
datapr.innerHTML = "Initializing...";
predict = false;
tf.util.shuffleCombo(trainingDataInputs, trainingDataOutputs);
let outputsAsTensor = tf.tensor1d(trainingDataOutputs, "int32");
let oneHotOutputs = tf.oneHot(outputsAsTensor, CLASS_NAMES.length);
let inputsAsTensor = tf.stack(trainingDataInputs);
let results = await model.fit(inputsAsTensor, oneHotOutputs, {
shuffle: true,
batchSize: 5,
epochs: 10,
callbacks: { onEpochEnd: logProgress },
});
outputsAsTensor.dispose();
oneHotOutputs.dispose();
inputsAsTensor.dispose();
predict = true;
predictLoop();
}
function logProgress(epoch, logs) {
let dat = Math.round(epoch * 11.1) + "%";
trainbar.style.width = dat;
datapr.innerHTML = dat;
if (epoch >= 9) {
setTimeout(() => {
expand.click();
}, 1000);
datapr.innerHTML = "Completed";
flipCameraButton.setAttribute("disabled", "true");
}
}
let previousClassName = "";
function predictLoop() {
if (predict) {
tf.tidy(function () {
let imageFeatures = calculateFeaturesOnCurrentFrame();
let prediction = model.predict(imageFeatures.expandDims()).squeeze();
let predictionArray = prediction.arraySync();
for (let i = 0; i < CLASS_NAMES.length; i++) {
let className = CLASS_NAMES[i];
let confidence = Math.round(predictionArray[i] * 100);
names[i].innerHTML = className;
bars[i].style.width = confidence + "%";
bars[i].innerHTML = confidence + "%";
if (confidence >= 99 && className !== previousClassName && voiceState) {
speak(className);
previousClassName = className;
}
}
});
window.requestAnimationFrame(predictLoop);
}
}
// Function to speak the className
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.voice = window.speechSynthesis.getVoices()[12];
speechSynthesis.speak(utterance);
}
// Call the predictLoop function to start the prediction loop
function reset() {
predict = false;
examplesCount.splice(0);
for (let i = 0; i < trainingDataInputs.length; i++) {
trainingDataInputs[i].dispose();
}
trainingDataInputs.splice(0);
trainingDataOutputs.splice(0);
clearImages();
resetBarsAndBtns();
}
function clearImages() {
document.querySelectorAll("img").forEach((i) => i.remove());
}
function resetBarsAndBtns() {
bars.forEach((b) => (b.style.width = 0));
dataC.forEach((d) => (d.innerHTML = 0));
datapr.innerHTML = "Train";
trainbar.style.width = 0;
flipCameraButton.disabled = false;
previousClassName = "";
}
let vce = document.getElementById("voice");
vce.addEventListener("change", (e) => {
if (e.target.checked) {
voiceState = true;
datavce.innerHTML = "volume_up";
} else {
datavce.innerHTML = "volume_off";
voiceState = false;
}
});
editClass.forEach((cls) => {
cls.addEventListener("click", () => {
let select = cls.parentElement.parentElement;
let h33 = select.querySelector(".cls");
selectText(h33);
h33.focus();
});
});
function selectText(element) {
var range, selection;
if (document.body.createTextRange) {
// For Internet Explorer
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
// For modern browsers
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}