-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
80 lines (73 loc) · 2.32 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
80
// more documentation available at
// https://github.com/tensorflow/tfjs-models/tree/master/speech-commands
import { init as initBubbles, animationFunction, animate } from "./bubbles.js";
const URL = "https://wash-os.netlify.app/model/";
let counterStarted = false;
let interval;
let paused = false;
async function createModel() {
const checkpointURL = URL + "model.json"; // model topology
const metadataURL = URL + "metadata.json"; // model metadata
const recognizer = speechCommands.create(
"BROWSER_FFT", // fourier transform type, not useful to change
undefined, // speech commands vocabulary feature, not useful for your models
checkpointURL,
metadataURL
);
await recognizer.ensureModelLoaded();
return recognizer;
}
async function init() {
const recognizer = await createModel();
const classLabels = recognizer.wordLabels(); // get class labels
console.log(classLabels);
recognizer.listen(
(result) => {
const scores = result.scores;
const max = Math.max(...scores);
for (let i = 0; i < classLabels.length; i++) {
if (max.toFixed(2) === result.scores[i].toFixed(2)) {
if (classLabels[i] === "water" && !counterStarted) {
triggerCountdown();
}
if (classLabels[i] === "_background_noise_" && counterStarted) {
stopCountdown();
}
}
}
},
{
includeSpectrogram: true, // in case listen should return result.spectrogram
probabilityThreshold: 0.75,
invokeCallbackOnNoiseAndUnknown: true,
overlapFactor: 0.5, // probably want between 0.5 and 0.75. More info in README
}
);
}
init();
const triggerCountdown = () => {
let counter = parseInt(document.querySelector(".counter p").textContent);
if (!counterStarted) {
if (!paused) {
initBubbles();
} else {
requestAnimationFrame(animate);
}
counterStarted = true;
interval = setInterval(() => {
if (counter > 1) {
counter--;
document.querySelector(".counter p").textContent = counter;
} else {
document.querySelector(".counter p").textContent = "Well done!";
clearInterval(interval);
}
}, 1000);
}
};
const stopCountdown = () => {
counterStarted = false;
paused = true;
cancelAnimationFrame(animationFunction);
clearInterval(interval);
};