xxxxxxxxxx
113
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Webcam Image Classification using a pre-trained customized model and p5.js
This example uses p5 preload function to create the classifier
=== */
// Classifier Variable
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/NZuEU76pl/' ;
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
//image variables
let adder;
let crow;
let deer;
let hare;
let toad;
let cat;
let book;
// Load the model first
function preload() {
adder = loadImage("adder.png");
crow = loadImage("crow.png");
deer = loadImage("deer.png");
hare = loadImage("hare.png");
toad = loadImage("toad.png");
cat = loadImage("cat.png");
book = loadImage("book.png")
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(400, 400);
let readButton = createButton("consult grimoire");
readButton.position(150, 420);
readButton.mousePressed(classifyVideo);
textSize(18);
// Create the video
video = createCapture(VIDEO);
video.size(400, 400);
video.hide();
flippedVideo = ml5.flipImage(video)
// Start classifying
//classifyVideo();
}
function draw() {
background(0);
// Draw the video
//image(flippedVideo, 0, 0);
if(label == "HARE"){
image(hare, 0, 0, 400, 400);
}else if(label == "ADDER"){
image(adder, 0, 0, 400, 400);
}else if(label =="DEER"){
image(deer, 0, 0, 400, 400);
}else if(label == "CROW"){
image(crow, 0, 0, 400, 400);
}else if(label == "CAT"){
image(cat, 0, 0, 400, 400);
}else if (label == "TOAD"){
image(toad, 0, 0, 400, 400);
}else {
image(book, 0, 0, 400, 400);
}
// Draw the label
// Draw the label
fill(153, 102, 0);
textFont('Garamond');
textSize(40);
textAlign(CENTER);
textStyle(ITALIC);
text(label, width / 2, height - 70);}
// 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();
}