-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
61 lines (53 loc) · 2.5 KB
/
index.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
<link href="https://fonts.googleapis.com/css2?family=Carter+One&display=swap" rel="stylesheet">
<div style="padding-bottom: 1em; padding-top: 0.25em; text-align: center; background-color: #cccccc9f">
<h3 style="font-family: 'Carter One', cursive;">If you're happy and you know it,<br/>press the button!</h3>
<button id="btn" type="button" onclick="init()">Start 👏</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/[email protected]/dist/speech-commands.min.js"></script>
<script type="text/javascript">
// more documentation available at
// https://github.com/tensorflow/tfjs-models/tree/master/speech-commands
// the link to your model provided by Teachable Machine export panel
const URL = "https://teachablemachine.withgoogle.com/models/CW7ZZU1uE/";
const { ipcRenderer } = require('electron');
const tolerance = 0.90;
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);
// check that model and metadata are loaded via HTTPS requests.
await recognizer.ensureModelLoaded();
return recognizer;
}
async function init() {
let button = document.getElementById("btn");
button.innerHTML = "Loading 🔄"
let recognizer = await createModel();
let classLabels = recognizer.wordLabels(); // get class labels
// listen() takes two arguments:
// 1. A callback function that is invoked anytime a word is recognized.
// 2. A configuration object with adjustable fields
recognizer.listen(result => {
let scores = result.scores; // probability of prediction for each class
if(result.scores[0].toFixed(2) > tolerance) {
ipcRenderer.send('clap', 'clap')
}
}, {
includeSpectrogram: true, // in case listen should return result.spectrogram
probabilityThreshold: 0.75,
invokeCallbackOnNoiseAndUnknown: true,
overlapFactor: 0.50 // probably want between 0.5 and 0.75. More info in README
});
button.innerHTML = "Stop ❌"
button.onclick = function() {
recognizer.stopListening()
button.onclick = init;
button.innerHTML = "Start 👏"
}
}
</script>