xxxxxxxxxx
42
let classifier;
let img;
let label = "waiting...";
let conf = 0;
function preload() {
classifier = ml5.imageClassifier("MobileNet");
img = loadImage("dan_and_gloria.jpg");
}
function gotResults(results) {
console.log(results);
label = results[0].label;
conf = results[0].confidence;
}
function setup() {
createCanvas(400, 400);
classifier.classify(img, gotResults);
}
function draw() {
background(0);
let col1 = color(0, 0, 0);
let col2 = color(0, 255, 0);
let col = lerpColor(col1, col2, conf);
tint(col);
image(img, 0, 0, width, height);
rectMode(CENTER);
textAlign(CENTER, CENTER);
fill(0, 150);
rect(200, height - 25, 400, 50);
textSize(24);
fill(255);
noStroke();
text(label, 200, height - 32);
textSize(16);
text("confidence: " + nf(conf, 0, 2), 200, height - 12);
}