xxxxxxxxxx
78
// Classifier Variable
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/m9EZq5JlJ/';
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
let helloImage;
let helloFade = 0;
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
helloImage = loadImage("hello.png");
}
function setup() {
createCanvas(1280, 720);
// Create the video
video = createCapture(VIDEO);
video.size(160, 120);
// video.hide();
flippedVideo = ml5.flipImage(video);
// Start classifying
classifyVideo();
}
function draw() {
background(0, 255, 0);
// Draw the video
// tint(255);
// imageMode(CORNER)
// image(flippedVideo, 0, 0);
if (label == "Hello") {
helloFade = 255;
}
if (helloFade > 0) {
tint(255, helloFade);
imageMode(CENTER);
image(helloImage, width / 2, 60, 285, 120);
helloFade -= 10;
}
// Draw the label
// fill(255);
// textSize(16);
// textAlign(CENTER);
// text(label, width / 2, height - 4);
}
// Get a prediction for the current video frame
function classifyVideo() {
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;
// Classifiy again!
classifyVideo();
}