-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (34 loc) · 1.13 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
let recognizer;
function predictWord() {
// Array of words that the recognizer is trained to recognize.
const words = recognizer.wordLabels();
recognizer.listen(({scores}) => {
// Turn scores into a list of (score,word) pairs.
scores = Array.from(scores).map((s, i) => ({score: s, word: words[i]}));
// Find the most probable word.
scores.sort((s1, s2) => s2.score - s1.score);
// console.log(recognizer);
document.querySelector('#console').textContent += " " + scores[0].word;
// document.querySelector('#console').textContent = scores[0].word;
}, {probabilityThreshold: 0.75});
}
async function registerSW() {
console.log(navigator)
if ('serviceWorker' in navigator) {
console.log("Service worker present");
try {
await navigator.serviceWorker.register('/sw.js');
} catch (e) {
console.log(`SW registration failed`);
}
}else{
console.log("No service worker??");
}
}
async function app() {
recognizer = speechCommands.create('BROWSER_FFT');
await recognizer.ensureModelLoaded();
predictWord();
registerSW();
}
app();