xxxxxxxxxx
189
/*
* 👋 Hello! This is an ml5.js example made and shared with ❤️.
* Learn more about the ml5.js project: https://ml5js.org/
* ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md
*
* This example demonstrates Sound classification using Google's Teachable Machine and p5.js
* Create your own custom model with Google's Teachable Machine! https://teachablemachine.withgoogle.com/
*/
// Initialize a sound classifier method with SpeechCommands18w model. A callback needs to be passed.
let classifier;
// Variable for holding the results of the classification
let predictedSound = "";
// Link to custom Teachable Machine model
const modelJson = "https://teachablemachine.withgoogle.com/models/n702_a7ev/";
let x = 40,
y = 40;
let n = 3;
let n2 = 3;
let n3 = 3;
let numSentences1 = 2;
let numSentences2 = 3;
let numSentences3 = 3;
let displayText = [""];
let markov;
let markov2;
let markov3;
let source1;
let source2;
let source3;
let moon;
let bakunawa;
let miniMoon;
let words = [
"Love",
"Pain",
"Power",
"Heaven",
"Earth",
"Creatures",
"Instincts",
"Soul",
"Voice",
];
let play = true;
let counter = 0;
let keywordDetected = false;
function preload() {
// Load Teachable Machine model
classifier = ml5.soundClassifier(modelJson);
moon = loadImage("moon.png");
miniMoon = loadImage("miniMoon.png");
bakunawa = loadImage("bakunawa.png");
source1 = loadStrings("source1.txt");
source2 = loadStrings("source2.txt");
source3 = loadStrings("source3.txt");
}
function setup() {
createCanvas(500, 500);
// textAlign(CENTER, CENTER);
// textSize(32);
// Classify the sound from microphone in real time
classifier.classifyStart(gotResult);
// create markov models
markov = RiTa.markov(n);
markov2 = RiTa.markov(n2);
markov3 = RiTa.markov(n3);
// load text into the models
markov.addText(source1.join(" "));
markov2.addText(source2.join(" "));
markov3.addText(source3.join(" "));
}
function draw() {
background(0);
textFont('Courier New');
if (!keywordDetected) { // if keyword not detected
showIntroScreen(); // show intro
} else {
generateMessageFromKeywords(); // show message
}
}
function showIntroScreen() {
fill(255);
textSize(16);
textAlign(CENTER);
text('Offer a prayer for the collective using one of these words:',40 , 50, 400);
textSize(30);
textFont("Bungee Hairline")
text('Love, Pain, Power', width / 2, 180);
text('Heaven, Earth, Creatures', width / 2, 240);
text('Instincts, Soul, Voice', width / 2, 300);
}
function generateMessageFromKeywords() {
if (predictedSound == "Moon") {
background(231, 176, 255);
displayText = markov3.generate(numSentences3);
image(moon, 0, 0, 500, 500);
text(displayText.join(" "), x, y, width / 2 - 210, y, 420, 440);
} else if (predictedSound == "miniMoon") {
background(255, 242, 143);
displayText = markov2.generate(numSentences2);
image(miniMoon, 0, 0, 500, 500);
text(displayText.join(" "), x, y, width / 2 - 210, y, 420, 440);
} else if (predictedSound == "Bakunawa") {
background(255, 242, 143);
displayText = markov.generate(numSentences1);
image(bakunawa, 0, 0, 500, 500);
text(displayText.join(" "), x, y, width / 2 - 210, y, 420, 440);
}
counter = counter + 1;
if (counter > 600) {
play = false;
keywordDetected = false;
counter = 0;
}
}
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
// Print the whole structure of the results to inspect it
console.log("Results Array:", results);
console.log("Results Structure:", JSON.stringify(results, null, 2));
// Check if the array contains valid objects and inspect each object
if (results && Array.isArray(results) && results.length > 0) {
results.forEach((result, index) => {
console.log(`Result ${index}:`, result); // Log the object itself to see its structure
});
}
// Now, process the results
if (results && results[0] && results[0].label) {
predictedSound = results[0].label;
let confidence = results[0].confidence;
console.log(`Predicted Sound: ${predictedSound}, Confidence: ${confidence}`);
// Trigger message if confidence is high enough
if (confidence > 0.75 && (predictedSound === "Moon" || predictedSound === "miniMoon" || predictedSound === "Bakunawa")) {
keywordDetected = true;
console.log("Keyword detected with sufficient confidence, triggering message...");
} else {
console.log("No keyword detected or confidence too low.");
}
} else {
console.log("No valid prediction found in results.");
}
}
// // A function to run when we get any errors and the results
// function gotResult(error, results) {
// if (error) {
// console.log(error);
// return;
// }
// predictedSound = results[0].label;
// // The results are in an array ordered by confidence.
// if (predictedSound === "Moon" || predictedSound === "miniMoon" || predictedSound === "Bakunawa") {
// keywordDetected = true; //trigger message
// }
// }