xxxxxxxxxx
// Global variable to store the classifier
let classifier;
// Label
let label = 'listening...';
let confidence;
let Tastatur = 0;
let Stift = 0;
let img, img_tastatur, img_stift;
// Teachable Machine model URL:
let soundModel = 'https://teachablemachine.withgoogle.com/models/UJRwGJgh7/';
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModel + 'model.json');
img = loadImage("1F3A4_black.png"); // microphone
img_tastatur = loadImage("Element 1.svg"); // clap
img_stift = loadImage("Piktogramm_Kulli.png"); // snap
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Start classifying
// The sound model will continuously listen to the microphone
classifier.classify(gotResult);
}
function draw() {
background(200);
// Draw the label in the canvas
fill(255);
imageMode(CENTER);
image(img, width/2, height/2);
//textSize(50);
//textAlign(CENTER, CENTER);
//text("Clap: " + clap, width / 2, height / 3);
//text("Snap: " + snap, width / 2, height / 2);
}
// The model recognizing a sound will trigger this event
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
label = results[0].label;
confidence = nf(results[0].confidence, 0, 2);
//print(label + " / " + confidence);
if (label.indexOf("Tastatur") > -1 && confidence > 0.9) {
img = img_tastatur;
print("Digital");
}
//clap += 1;
else if (label.indexOf("Stift") > -1 && confidence > 0.9) {
img = img_stift;
print("Analog");
//snap += 1;
}
}