xxxxxxxxxx
// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Image classification using MobileNet and p5.js
=== */
// Initialize the Image Classifier method with MobileNet
const classifier = ml5.imageClassifier('MobileNet');
// A variable to hold the image we want to classify
let img;
function setup() {
imageMode(CENTER);
createCanvas(windowWidth, windowHeight);
// Load the image
//img = createImg('bird.jpg', imageReady);
//img.size(400, 400);
img = loadImage('bird.jpg', imageReady); // run callback imageReady when finishing loading
}
// When the image has been loaded,
// get a prediction for that image
function imageReady() {
image(img, width/2, height/2, 400, 400); // display the image
classifier.predict(img, gotResult);
// You can also specify the amount of classes you want
// classifier.predict(img, 10, gotResult);
}
// A function to run when we get the results
function gotResult(results) {
// The results are in an array ordered by probability.
console.log(results);
//print("The MobileNet model labeled this as " + results[0].className + " with a confidence of " + results[0].probability);
textAlign(CENTER);
text("The MobileNet model labeled this as " + results[0].className + "with a confidence of " + results[0].probability, width/2, height-height/3);
//select('#result').html(results[0].className);
//select('#probability').html(nf(results[0].probability, 0, 2));
}