xxxxxxxxxx
52
// Teachable Machine ml5 image example - modified from The Coding Train https://thecodingtrain.com/TeachableMachine/1-teachable-machine.html
let video;
let label = "waiting...";
let confidence = 0.0;
let classifier;
let modelURL = 'https://teachablemachine.withgoogle.com/models/Rs9GzaiJa/';
// STEP 1: Load the model
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
}
function setup() {
createCanvas(640, 520);
video = createCapture(VIDEO);
video.hide();
classifyVideo();
}
function draw() {
background(0);
image(video, 0, 0, 640, 480);
// STEP 4: Show current label
textSize(32);
textAlign(CENTER, CENTER);
fill(255);
text(label + " " + confidence, width / 2, height - 16);
if (label === "hand") {
ellipse(100, 100, 50, 50)
}
}
// STEP 2: Do the classifying
function classifyVideo() {
classifier.classify(video, gotResults);
}
// STEP 3: Get the classification
function gotResults(error, results) {
if (error) {
console.error(error);
return;
}
// Store the label and classify again
label = results[0].label;
confidence = nf(results[0].confidence, 0, 2);
classifyVideo();
}