-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path06-sound-classifier-vanilla-js.html
117 lines (110 loc) · 2.78 KB
/
06-sound-classifier-vanilla-js.html
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
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sound Classifier</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<style>
.hidden {
display: none;
}
.loading .display-loading {
display: block;
}
.running .display-running {
display: block;
}
#results {
background-color: white;
width: 20rem;
min-height: 6rem;
display: none;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 2rem;
}
.running #results {
display: flex;
}
</style>
</head>
<body>
<h1 class="h1">Sound Classifier</h1>
<h2 id="state"></h2>
<div id="results" class="fancy-shadow hidden display-running"></div>
<script src="https://unpkg.com/ml5@1/dist/ml5.js"></script>
<script>
const $results = document.querySelector("#results");
const $state = document.querySelector('#state');
let classifier;
let classification = [];
const words = [
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"up",
"down",
"left",
"right",
"go",
"stop",
"yes",
"no",
];
const STATE_LOADING = "loading";
const STATE_RUNNING = "running";
const ALL_STATES = [STATE_LOADING, STATE_RUNNING];
let state = STATE_LOADING;
const setState = (value) => {
console.log('setState', value);
state = value;
$state.textContent = state;
document.documentElement.classList.remove(...ALL_STATES);
document.documentElement.classList.add(state);
};
const preload = async () => {
setState(STATE_LOADING);
requestAnimationFrame(draw);
const options = { probabilityThreshold: 0.7 };
classifier = ml5.soundClassifier("SpeechCommands18w", options);
await classifier.ready;
console.log('model ready');
setup();
}
const setup = async () => {
console.log('setup');
// no need for video stream
// classifier contains its own
// logic to set up audio
// start classification
classifier.classifyStart((results) => {
// store in global
classification = results;
});
// start the app
setState(STATE_RUNNING);
}
const draw = () => {
if (state === STATE_RUNNING) {
let predictedWord = "no word detected";
if (classification[0]?.confidence > 0.7) {
predictedWord = classification[0].label;
}
$results.textContent = predictedWord;
}
requestAnimationFrame(draw);
}
preload();
</script>
</body>
</html>