xxxxxxxxxx
99
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// The classifier will use our model to try and classify
// the video image
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/FYiXCGNVc/';
// Video
let video;
// To store the classification
let label = "";
// And the confidence of that classification
let confidence = 0;
// The confidence threshhold determines how strict we'll be with classifying an image, from 0 to 1.
let threshold = 0.8;
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(400, 400);
// Create the video
video = createCapture(VIDEO);
video.size(400, 400);
video.hide();
// Start classifying
classifyVideo();
}
function draw() {
background(255);
// Uncomment below to see your video input
//image(video, 0, 0);
if (confidence > threshold) {
if (label == "apple") {
stroke("brown");
strokeWeight(5);
line(200, 100, 210, 80);
noStroke();
fill("red");
ellipse(200, 200, 200, 200);
} else if (label == "lemon") {
noStroke();
fill("yellow");
ellipse(200, 200, 150, 200);
} else if (label == "lime") {
noStroke();
fill("lime");
ellipse(200, 200, 175, 150);
}
noStroke();
fill(0);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
} else {
noStroke();
fill(0);
textSize(25);
textAlign(CENTER);
text("I'm not sure what that is.\nTry holding up an apple,\nlemon, or lime.", width / 2, height / 2);
}
}
// Get a prediction for the current video frame
function classifyVideo() {
classifier.classify(video, 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;
confidence = results[0].confidence;
// Classifiy again!
classifyVideo();
}