xxxxxxxxxx
// Global variable to store the classifier
let classifier;
// Label
let label = 'listening...';
let confidence;
let Cutter = 0;
let Schere = 0;
let img, img_cutter, img_schere;
// Teachable Machine model URL:
let soundModel = 'https://teachablemachine.withgoogle.com/models/lvJfm8xq0/';
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModel + 'model.json');
img = loadImage("blatt.png"); // microphone
img_cutter = loadImage("cutter.png"); // clap cutter
img_schere = loadImage("schere.png"); // snap
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Start classifying
// The sound model will continuously listen to the microphone
classifier.classify(gotResult);
}
function draw() {
background(255);
// Draw the label in the canvas
fill(255);
imageMode(CENTER);
image(img, width/2, height/2);
//scale(0.5);
//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("Cutter") > -1 && confidence > 0.85) {
img = img_cutter;
print("Cutter");
}
//clap += 1;
else if (label.indexOf("Schere") > -1 && confidence > 0.85) {
img = img_schere;
print("Schere");
//snap += 1;
}
}
webgl backend was already registered. Reusing existing backend factory.
cpu backend was already registered. Reusing existing backend factory.
Platform browser has already been set. Overwriting the platform with [object Object].
🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈
🌟 Thank you for using ml5.js v0.12.2 🌟
Please read our community statement to ensure
that the use of this software reflects the values
of the ml5.js community:
↳ https://ml5js.org/about
Reporting:
↳ https://github.com/ml5js/ml5-library/issues
↳ Email: info@ml5js.org
🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈