xxxxxxxxxx
114
// 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/JqgQzdkxT/';
let img;
// STEP 1: Load the model!
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
}
function setup() {
createCanvas(1024, 1366);
// Create the video
video = createCapture(VIDEO);
video.hide();
// STEP 2: Start classifying
classifyVideo();
Place_Leaf_Here = loadImage('https://cdn-images-1.medium.com/max/1200/1*KBfg9Bh9-vdq5To6mbsWzw.png');
Anthurium_hookeri = loadImage('https://cdn-images-1.medium.com/max/1200/1*2cBhZYXhb2XmIM4xs89SDA.png');
Chamaedorea_metallica = loadImage('https://cdn-images-1.medium.com/max/1200/1*Y16G9H_v9SqtOc_ZTWybfQ.png');
Epipremnum_aureum = loadImage('https://cdn-images-1.medium.com/max/1200/1*zlU2u7tKg_6UwTKSreNnYQ.png');
Ficus_elastica = loadImage('https://cdn-images-1.medium.com/max/1200/1*SeGtYFDqDn8AuaHGMVaf5Q.png');
Ficus_lyrata = loadImage('https://cdn-images-1.medium.com/max/1200/1*10rxqiOx1EpCkVrcivbM0g.png');
Ficus_pumila = loadImage('https://cdn-images-1.medium.com/max/1200/1*TzpK3-SXzq8httoLfA8LvA.png');
Ficus_sagittata = loadImage('https://cdn-images-1.medium.com/max/1200/1*FGAoWyj9H7oJvV6XnIcxTg.png');
Mangifera_indica = loadImage('https://cdn-images-1.medium.com/max/1200/1*C55w40lnggh4Ct37JIXqlw.png');
Philodendron_erubescens = loadImage('https://cdn-images-1.medium.com/max/1200/1*iED-5YC50KATYzB39pY1Jw.png');
Philodendron_hederaceum_var_micans = loadImage('https://cdn-images-1.medium.com/max/1200/1*ifitr5roHeuOcJa-vZtLPA.png');
Pilea_peperomioides = loadImage('https://cdn-images-1.medium.com/max/1200/1*l7ec0lUvOAeDjNoZhwBcLQ.png');
Rhapis_excelsa = loadImage('https://cdn-images-1.medium.com/max/1200/1*KPPSjsFUkmcjPbZ2U1YzzQ.png');
Thaumatophyllum_xanadu = loadImage('https://cdn-images-1.medium.com/max/1200/1*yo5VDx3mACrFvpmp4N2RDw.png');
Theobroma_cacao = loadImage('https://cdn-images-1.medium.com/max/1200/1*ZkH2xLf5ABfP_DBbESDnKw.png')
}
// STEP 2 classify the video!
function classifyVideo() {
classifier.classify(video, gotResults);
}
function draw() {
background(0);
// Draw the video
image(video, 173, 340, 384, 288);
// STEP 4: Draw the label
textSize(32);
textAlign(CENTER, CENTER);
fill(255);
text(label, width / 2, height - 16);
switch(label) {
case "Anthurium hookeri":
img = Anthurium_hookeri
break;
case "Chamaedorea metallica":
img = Chamaedorea_metallica;
break;
case "Epipremnum aureum":
img = Epipremnum_aureum;
break;
case "Ficus elastica":
img = Ficus_elastica;
break;
case "Ficus lyrata":
img = Ficus_lyrata;
break;
case "Ficus pumila":
img = Ficus_pumila;
break;
case "Ficus sagittata":
img = Ficus_sagittata;
break;
case "Mangifera indica":
img = Mangifera_indica;
break;
case "Philodendron erubescens":
img = Philodendron_erubescens;
break;
case "Philodendron hederaceum var micans":
img = Philodendron_hederaceum_var_micans;
break;
case "Pilea peperomioides":
img = Pilea_peperomioides;
break;
case "Rhapis_excelsa":
img = Rhapis_excelsa;
break;
case "Thaumatophyllum xanadu":
img = Thaumatophyllum_xanadu;
break;
case "Theobroma cacao":
img = Theobroma_cacao
break;
case "Place Leaf Here!":
img = Place_Leaf_Here
}
if (img) {
image(img, 0, 0, 1024, 1366)
}
}
// 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();
}