xxxxxxxxxx
81
// Teachable Machine
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/TeachableMachine/1-teachable-machine.html
// https://editor.p5js.org/codingtrain/sketches/PoZXqbu4v
// The video
let video;
// For displaying the label
let label = "waiting...";
// The classifier
let classifier;
let modelURL = 'https://teachablemachine.withgoogle.com/models/It0gZ1aI2/';
// STEP 1: Load the model!
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
}
function setup() {
createCanvas(640, 520);
// Create the video
video = createCapture(VIDEO);
video.hide();
// STEP 2: Start classifying
classifyVideo();
}
// STEP 2 classify the videeo!
function classifyVideo() {
classifier.classify(video, gotResults);
}
function draw() {
background(0);
noStroke();
// Draw the video
image(video, 0, 0);
// STEP 4: Draw the label
textSize(32);
textAlign(CENTER, CENTER);
fill(255);
text(label, width / 2, height - 16);
noStroke()
// Pick an emoji, the "default" is train
let emoji = "🧐";
if (label == "Without Mask") {
emoji = "🤭";
stroke('red');
strokeWeight(10);
noFill();
rect(5, 5, 630, 470);
} else {
(label == "Mask")
emoji = "😷";
stroke('green');
strokeWeight(10);
noFill();
rect(5, 5, 630, 470);
}
// Draw the emoji
textSize(256);
text(emoji, width / 2, height / 2);
}
// STEP 3: Get the classification!
function gotResults(error, results) {
// Something went wrong!
if (error) {
console.error(error);
return;
}
// Store the label and classify again!
label = results[0].label;
classifyVideo();
}