xxxxxxxxxx
90
// 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
=== */
// Global variable to store the classifier
let classifier;
// Label (start by showing listening)
let label = "listening";
// Teachable Machine model URL:
let soundModelURL = 'https://teachablemachine.withgoogle.com/models/wcvn1qtWl/model.json';
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModelURL);
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Start classifying
// The sound model will continuously listen to the microphone
classifier.classify(gotResult);
}
function draw() {
background(0);
// Draw the label in the canvas
fill(255);
textSize(32);
textAlign(CENTER, CENTER);
// text(label, width / 2, height / 2);
textSize(50);
text('Learn time telling in Japanese...',width/2, 100);
fill(0, 102, 153);
let emoji = 'what time is it?';
if (label == 'ichiji' && confidence >=0.6) {
emoji = '🕐';}
else if (label == 'niji' && confidence >=0.5) {
emoji = '🕑';}
else if (label == 'sanji' && confidence >=0.7) {
emoji = '🕒';}
else if (label == 'yoji' && confidence >=0.3) {
emoji = '🕓';}
else if (label == 'goji' && confidence >=0.7) {
emoji = '🕔';}
else if (label == 'rokuji' && confidence >=0.6) {
emoji = '🕕';}
else if (label == 'shichiji' && confidence >=0.3) {
emoji = '🕖';}
else if (label == 'hachiji' && confidence >=0.5) {
emoji = '🕗';}
else if (label == 'kuji' && confidence >=0.5) {
emoji = '🕘';}
else if (label == 'juuji' && confidence >=0.4) {
emoji = '🕙';}
else if (label == 'juuichiji' && confidence >=0.5) {
emoji = '🕚';}
else if (label == 'juuniji' && confidence >=0.5) {
emoji = '🕛';}
else if (label == 'Background Noise' && confidence >=0.1) {
emoji = 'what time is it?';}
textSize(140);
text(emoji, width/2, height/2);
}
// The model recognizing a sound will trigger this event
function gotResult(error, results) {
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;
}