xxxxxxxxxx
// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Multiple Image classification using MobileNet and p5.js
=== */
// Initialize the Image Classifier method using MobileNet
const classifier = ml5.imageClassifier('MobileNet');
let img;
let currentIndex = 0;
let allImages = [];
let display = true;
let displayTime = 750;
let predictions = [];
function appendImages() {
for (i = 0; i < data.images.length; i++) {
imgPath = data.images[i];
allImages.push(imgPath);
}
}
function preload() {
data = loadJSON('https://github.com/ml5js/ml5-examples/blob/master/p5js/ImageClassification_MultipleImages/assets/data.json');
}
function drawNextImage() {
img.attribute('src', allImages[currentIndex], imageReady);
}
function setup() {
noCanvas();
appendImages();
img = createImg(allImages[0], imageReady);
}
// When the image has been loaded,
// get a prediction for that image
function imageReady() {
classifier.predict(img, gotResult);
}
function savePredictions() {
predictionsJSON = {
"predictions": predictions
}
saveJSON(predictionsJSON, 'predictions.json');
}
function removeImage() {
currentIndex++;
if (currentIndex <= allImages.length - 1) {
drawNextImage();
}
else {
savePredictions();
}
}
// When we get the results
function gotResult(results) {
information = {
"name": allImages[currentIndex],
"result": results,
}
predictions.push(information);
console.log(results);
if (display) {
// The results are in an array ordered by probability.
print(results[0].className);
print(results[0].probability);
//select('#result').html(results[0].className);
//select('#probability').html(nf(results[0].probability, 0, 2));
// Can be changed with the displayTime variable.
setTimeout(removeImage, displayTime);
}
else {
removeImage();
}
}