xxxxxxxxxx
// First train image model:
// https://teachablemachine.withgoogle.com/train/image
// Classifier Variable
let classifier;
// Your Model URL from Teachable Machine
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/eAh32u4g1/';
// Video
let video;
// To store the classification
let label = "";
let confidence;
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(720, 480);
// Create the video
video = createCapture(VIDEO);
video.size(720, 480);
video.hide();
// Set Google Font
textFont("Roboto Mono");
// Start classifying
classifyVideo();
}
function draw() {
background(0);
// Draw the video
image(video, 0, 0);
//tint(255, 0, 100);
//image(video, 0, 0);
//noTint();
textSize(100);
textAlign(CENTER);
if (label == "Cat" && confidence >= 0.99)
{
textFont("Roboto Mono");
text("Cat!", width/2, height/2);
}
else if (label == "Synthie" && confidence >= 0.99)
{
textFont("Roboto Sans");
text("Synthie!", width/2, height/2);
}
// Draw the debug label
fill(255);
textSize(16);
textAlign(CENTER);
text(label + "\n" + confidence, width / 2, height - 20);
}
// Get a prediction for the current video frame
function classifyVideo() {
let flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
flippedVideo.remove();
}
// 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.
// console.log(results[0]);
label = results[0].label;
confidence = nf(results[0].confidence, 0, 2);
// Classifiy again!
classifyVideo();
}