mirror of
https://github.com/hexahigh/games.git
synced 2025-12-11 20:15:38 +01:00
50 lines
2.1 KiB
JavaScript
50 lines
2.1 KiB
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 = "./model/";
|
|
|
|
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() {
|
|
const recognizer = await createModel();
|
|
const classLabels = recognizer.wordLabels(); // get class labels
|
|
const labelContainer = document.getElementById("label-container");
|
|
for (let i = 0; i < classLabels.length; i++) {
|
|
labelContainer.appendChild(document.createElement("div"));
|
|
}
|
|
|
|
// 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 => {
|
|
const scores = result.scores; // probability of prediction for each class
|
|
// render the probability scores per class
|
|
for (let i = 0; i < classLabels.length; i++) {
|
|
const classPrediction = classLabels[i] + ": " + result.scores[i].toFixed(2);
|
|
labelContainer.childNodes[i].innerHTML = classPrediction;
|
|
}
|
|
}, {
|
|
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
|
|
});
|
|
|
|
// Stop the recognition in 5 seconds.
|
|
// setTimeout(() => recognizer.stopListening(), 5000);
|
|
} |