// To store sound classifier object
let classify;
// This varible is to keep track of
// the word that model predicts
let predictedWord = "";
// This array consists of words that
// the SpeechCommand18w model can predict.
let words = [
"zero",
"three",
"six",
"nine",
"left",
"stop",
"one",
"four",
"seven",
"up",
"right",
"yes",
"two",
"five",
"eight",
"down",
"go",
"no",
];
/*
The 'preload' function loads SpeechCommand18w
with a confidence threshold of 0.6 .
This means that the model only accepts
prediction above this confidence level.
*/
function preload() {
let options = { probabilityThreshold: 0.6 };
classify = ml5.soundClassifier("SpeechCommands18w", options);
}
/*
The 'setup' function runs once when program starts.
Here, a canvas is created and classification process starts.
The 'gotresult' function handle predictions and errors.
*/
function setup() {
createCanvas(650, 450);
classify.classifyStart(gotResult);
}
/*
The 'gotResult' function is called
when classifier makes a prediction
Results to the prediction are in an
array ordered by confidence.
The first label is one the model feels
most confident about, at indicated by result[0].
*/
function gotResult(results) {
console.log(results);
predictedWord = results[0].label;
}
/*
This function is to draw a list of
18 words recognized by model in tha canvas.
*/
function displayWords() {
textAlign(CENTER, CENTER);
textSize(28);
fill(0);
text("Speak one of these words!", width / 2, 40);
let x = 125;
let y = 150;
for (let i = 0; i < words.length; i++) {
fill(120);
text(words[i], x, y);
y += 50;
if ((i + 1) % 6 === 0) {
x += 200;
y = 150;
}
}
}
/*
This function runs continuously in a
loop, it updates and renders
the canvas repeatedly.
It shows the predicted word.
*/
function draw() {
background(249);
displayWords();
if (predictedWord !== "") {
fill("#2f8d46");
textAlign(CENTER, CENTER);
textSize(70);
text(predictedWord, width / 2, 100);
}
}