xxxxxxxxxx
79
// Paste your Teachable Machine Model URL here 🌈
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/IMNYfMvm3/';
// Classifier Variable
let classifier;
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
let emoji = "";
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(640, 520);
// Create the video
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
flippedVideo = ml5.flipImage(video)
// Start classifying
classifyVideo();
}
function draw() { // Do something with label 🔥
background(0);
// Draw the video
image(flippedVideo, 0, 0);
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width/2, height-15);
text(emoji, 100,100);
if (label == "Class 1"){ // what is your first class? 🍓
emoji = ""; // Set your first emoji or letter or word
} else if (label == "Class 2"){ //what's your second class? 🦄
emoji = ""; // Set another emoji or letter or word
} else {
// Do nothing!
}
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
// 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;
// Classifiy again!
classifyVideo();
}
// Adapted by Dr. Em 2021
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT