xxxxxxxxxx
85
console.log("ml5 version:", ml5.version);
let classifier;
// Teachable Machine model URL:
let soundModelURL = "https://teachablemachine.withgoogle.com/models/oj4f3Dfry/";
let options = { probabilityThreshold: 0.7 };
let label = "listening...";
let confidence = 0.0;
let outputs = [];
function setup() {
createCanvas(600, 400);
background(0);
classifier = ml5.soundClassifier(soundModelURL + "model.json", options, modelReady);
}
function draw() {
background(0);
fill(255);
// use of the result!
noStroke();
if (label == "Go") { // case-sensitive
fill(0, 255, 0);
circle(width / 2, height / 2, 200);
fill(255);
textSize(30);
text(label, width / 2 - 20, height / 2 + 10);
}
else if (label == "Stop") { // case-sensitive
fill(255, 0, 0);
circle(width / 2, height / 2, 200);
fill(255);
textSize(30);
text(label, width / 2 - 30, height / 2 + 10);
}
else if (label == "Warning"){
background(255);
fill(255, 255, 0);
circle(width / 2, height / 2, 200);
fill(0);
textSize(30);
text(label, width / 2 - 52, height / 2 + 10);
}
// display the most confident result
else {
fill(255);
textSize(30);
text(label, width / 2, height / 2 );
}
//text(confidence, width / 2, height / 2 + 50);
// display the whole results
textSize(10);
for (let i = 0; i < outputs.length; i++) {
let output = outputs[i];
let x = 10;
let y = 20 + i * 30;
text(output.label, x, y);
text(output.confidence, x, y + 10);
}
}
function modelReady() {
console.log("Model Ready!");
// classify sound
classifier.classify(gotResult);
}
function gotResult(error, results) {
if (error) {
console.error(error);
}
//console.log(results);
outputs = results;
if (results.length > 0) {
label = results[0].label;
confidence = results[0].confidence;
}
}