xxxxxxxxxx
42
let canvasImage; //holds canvas as image to pass to TM
let classifier; // teachable machine classifier variable
let imageModelURL = "https://teachablemachine.withgoogle.com/models/Rx9iVhlk9/"; // Model URL
// To store the classification
let label = "";
function preload() {
classifier = ml5.imageClassifier(imageModelURL + "model.json"); //load TM model
}
function setup() {
createCanvas(400, 400);
}
function draw() {}
// Get a prediction for the current video frame
function classify() {
/*******************************************************/
//really weird way of passing the canvas as an image to Teachable machine... I am looking for a better way to do this but for now here it is:
canvasImage = loadImage(
document.querySelector("canvas").toDataURL("image/jpeg", 0.1)
);
setTimeout(function () {
classifier.classify(canvasImage, gotResult);
}, 100);
/*******************************************************/
}
// When we get a result
function gotResult(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;
state = "result";
}