xxxxxxxxxx
126
// 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/n702_a7ev/' ;
let moon = [
"power",
"instincts",
"creatures",
];
let miniMoon = [
"love",
"soul",
"pain",
];
let bakunawa = [
"earth",
"voice",
"heaven",
];
// Variable for displaying the results on the canvas
let predictedWord = "";
// Load the model first
function preload() {
frankenstein = loadImage("frankenstein.png");
astronomen = loadImage("astronomen.png");
winter = loadImage("winter.png");
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
// Options for the SpeechCommands18w model, the default probabilityThreshold is 0
let options = { probabilityThreshold: 0.7 };
// Load SpeechCommands18w sound classifier model
classifier = ml5.soundClassifier("SpeechCommands18w", options);
}
function setup() {
createCanvas(650, 450);
// Classify the sound from microphone in real time
classifier.classifyStart(gotResult);
}
function draw() {
background(250);
// Call function for displaying background words
displayWords();
// Draw the right image
if(label == "A Year Without a Summer"){
image(moon, 0,0, 320, 240);
} else if(label == "Frankenstein"){
image(miniMoon,0,0, 320, 240);
} else {
image(bakunawa, 0, 0, 320, 240);
}
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
}
// Once the model outputs results start displaying the predicted word on the canvas
if (predictedWord !== "") {
fill(211, 107, 255);
textAlign(CENTER, CENTER);
textSize(64);
text(predictedWord, width / 2, 90);
}
// Function to display the 18 words on the canvas
function displayWords() {
textAlign(CENTER, CENTER);
textSize(32);
fill(96);
text("Say one of these words!", width / 2, 40);
let x = 125;
let y = 150;
// Words appear in 3 columns of 6 rows
for (let i = 0; i < words.length; i++) {
fill(158);
text(words[i], x, y);
y += 50;
if ((i + 1) % 6 === 0) {
x += 200;
y = 150;
}
}
}
// A function to run when we get any errors and the results
function gotResult(results) {
// The results are in an array ordered by confidence
console.log(results);
// Load the first label to the text variable displayed on the canvas
predictedWord = results[0].label;
}