xxxxxxxxxx
72
/*
In this code we are using the ml5 library to listen
to the user commands and try to understand them.
The model is trained to recognize pre-trained commands, such as,
the numbers from zero to nine, up, down, left, right, go, stop and
yes.
Lets go over each line 🤓
'options' is a variable that will set how sure (confident) we want the
machine learning model to be on what is hearing, before responding with
what it thinks it heard */
let options = {
probabilityThreshold: 0.9
};
/* 'classifier' is a variable that stores the result of calling the
ml5 sound Classifier with the options we provided. In this case, the
'options' we provided is the probabilityThreshols on line 15 and when
it all that is processed, it runs a function called 'modelReady'
this function is on line 24 */
let classifier = ml5.soundClassifier('SpeechCommands18w', options, modelReady);
/* this function takes the variable 'classifier' and runs an ml5 function
called 'classify' passing the 'gotResult' function to it */
function modelReady() {
// classify sound
classifier.classify(gotResult);
}
/* this function has two parameters, 'error' and 'result'. The error
will return an error if one happens, and the result will have the
result of classifying (identifying a sound) the input from the user */
function gotResult(error, result) {
/* this if statement is checking if there is an error or not. If
there is an error it will show it in the 'console' and stop. */
if (error) {
console.log(error);
return;
}
/* if there is not an error the following code will run
set the colour of the background, colour of the text and
the size of the text */
background(42);
textSize(50);
text('🎙', 175, 100);
fill('white');
textSize(18);
/* show on the canvas the text between quotes '', add to it
the content of the array 'result' at position 0, and from that
information extract the content of a 'variable' called 'label' */
text('🦻 The computer heard: ' + result[0].label, 20, 190);
/* show on the canvas the text between quotes '', add to it
the content of the array 'result' at position 0, and from that
information extract the content of a 'variable' called 'confidence' */
text('🤔 The confidence is: ' + result[0].confidence, 20, 240);
}
function setup() {
createCanvas(400, 400);
}
/* Play with the probabilityThreshold on line 16 and see how it
affects the results on the canvas */