xxxxxxxxxx
50
let video;
let classifier;
let label = "waiting...";
// Load the model
function preload() {
classifier = ml5.imageClassifier('MobileNet', modelReady);
}
function setup() {
createCanvas(640, 520);
// Create the video
video = createCapture(VIDEO);
video.hide();
// Predict the current frame.
classifyVideo();
}
function modelReady() {
console.log('Model Ready');
}
function classifyVideo() {
classifier.classify(video, gotResults);
}
// A function to run when we get any errors and the results
function gotResults(error, results) {
// If there is an error
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
label = results[0].label; //most confident guess
// Classify again!
classifyVideo();
}
function draw() {
background(0);
// Draw the video
image(video, 0, 0);
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
}