xxxxxxxxxx
54
let classifier;
let img;
let c;
function setup() {
c = createCanvas(400, 400);
background(0);
// c.drop(gotFile);
classifier = ml5.imageClassifier('MobileNet', modelReady);
img = new Image();
img.crossOrigin = "Anonymous";
img.src = 'rat.jpg';
document.body.appendChild(img);
classifier.predict(img, processResults);
}
function draw() {
}
function modelReady() {
print('Model is ready!');
}
function processResults(err, results) {
if (err) {
console.error(err)
} else {
print(results);
createP(results[0].className);
}
}
function gotFile(file) {
// If it's an image file
if (file.type === 'image') {
removeElements();
// Create an image DOM element but don't show it
// let img = createImg(file.data).hide();
let img = createImg(file.data, '', function() {
img.hide();
background(255);
if (img.width > img.height) {
image(img, 0, 0, width, img.height * width / img.width);
} else {
image(img, 0, 0, img.width * height / img.height, height);
}
classifier.predict(img, processResults);
// img.style('display', 'none');
});
// Draw the image onto the canvas
} else {
println('Not an image file!');
}
}