xxxxxxxxxx
49
/*
* 👋 Hello! This is an ml5.js example made and shared with ❤️.
* Learn more about the ml5.js project: https://ml5js.org/
* ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md
*
* This example demonstrates detecting objects in a live video through ml5.imageClassifier.
*/
// A variable to initialize the Image Classifier
let classifier;
// A variable to hold the video we want to classify
let video;
// Variable for displaying the results on the canvas
let label = "Model loading...";
function preload() {
classifier = ml5.imageClassifier('https://teachablemachine.withgoogle.com/models/AU2blvlo8/');
}
function setup() {
createCanvas(640, 480);
background(255);
// Using webcam feed as video input, hiding html element to avoid duplicate with canvas
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
classifier.classifyStart(video, gotResult);
}
function draw() {
// Each video frame is painted on the canvas
image(video, 0, 0);
// Printing class with the highest probability on the canvas
fill(255);
textSize(32);
text(label, 20, 50);
}
// Callback function for when classification has finished
function gotResult(results) {
// Update label variable which is displayed on the canvas
label = results[0].label;
}