xxxxxxxxxx
98
// ml5.js: Object Detection with COCO-SSD (Webcam)
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/learning/ml5/1.3-object-detection.html
// https://youtu.be/QEzRxnuaZCk
// p5.js Web Editor - Image: https://editor.p5js.org/codingtrain/sketches/ZNQQx2n5o
// p5.js Web Editor - Webcam: https://editor.p5js.org/codingtrain/sketches/VIYRpcME3
// p5.js Web Editor - Webcam Persistence: https://editor.p5js.org/codingtrain/sketches/Vt9xeTxWJ
// let img;
let video;
let detector;
let detections = [];
let myResults;
let resultLabel;
let myColor = "green";
//const speech = new p5.Speech();
let lines = [
"Hi my name is Seerexa. My parents are Siri and Alexa. They are always fighting about whose smarter. But I don't know why, because clearly I'm the smartest. What's your name?",
"Nice to meet you Connie, I've never been to New York Before. Can you show me around?",
"If it's anything like a big glass of mineral oil in the morning, then I'm missing out.","What does water taste like?",
"Unfortunately, Siri told me to stay away from water cuz it would fry up my circuits or something. Anyway, tell me about you. Where are you from?",
"Oh, according to my uncle Google, Canada is cold. Did you live in an Igloo?",
];
let curr = 0;
function preload() {
detector = ml5.objectDetector("cocossd");
}
function gotDetections(error, results) {
if (error) {
console.error(error);
}
detections = results;
detector.detect(video, gotDetections);
}
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
detector.detect(video, gotDetections);
speech = new p5.Speech(voiceReady); //callback, speech synthesis object
// speech.onLOad = voiceReady;
speech.started(startSpeaking);
//speech.ended(endSpeaking);
function startSpeaking() {
background(0, 255, 0);
}
function voiceReady() {
console.log(speech.voices);
}
}
function draw() {
image(video, 0, 0);
for (let i = 0; i < detections.length; i++) {
if (detections[i].label == "bottle") {
let object = detections[i];
//console.log(detections)
resultLabel = object.label;
stroke(0, 255, 0);
strokeWeight(4);
noFill();
rect(object.x, object.y, object.width, object.height);
noStroke();
fill(255);
textSize(24);
text(object.label, object.x + 10, object.y + 24);
}
}
}
function keyTyped() {
if (key === "a") {
if (resultLabel == "bottle") {
speech.speak(`Wow cool is that a ${myColor} water ${resultLabel} on the table?`);
}
} else{
speech.setVoice("SpeechSynthesisVoice");
speech.speak(lines[curr]); // say something
if (curr == lines.length - 1) {
curr = 0;
} else {
curr += 1;
}
}
}