xxxxxxxxxx
67
let classifier;
let video;
var speech;
var recog;
let started = false;
let stop = false;
function setup() {
noCanvas();
speech = new p5.Speech(); // speech synthesis object
speech.speak('hi there, what do you want to do?'); // say something
recog = new p5.SpeechRec('en-US'); // speech recognition object (will prompt for mic access)
recog.onResult = showResult;
recog.continuous = true; // do continuous recognition
recog.interimResults = true; // allow partial recognition (faster, less accurate)
recog.start(); // start listening
}
function showResult()
{
console.log(recog.resultString); // log the result
if (!started && recog.resultString.indexOf("start") > -1) {
started = true;
stop = false;
// Create a camera input
video = createCapture(VIDEO);
// Initialize the Image Classifier method with MobileNet and the video as the second argument
classifier = ml5.imageClassifier('MobileNet', video, modelReady);
} else if (started && recog.resultString.indexOf("stop")) {
stop = true;
// started = true;
}
}
function modelReady() {
// Change the status of the model once its ready
//select('#status').html('Model Loaded');
console.log("Model Loaded");
// Call the classifyVideo function to start classifying the video
classifyVideo();
}
// Get a prediction for the current video frame
function classifyVideo() {
classifier.predict(gotResult);
}
// When we get a result
function gotResult(err, results) {
// The results are in an array ordered by probability.
speech.speak(results[0].className);
console.log(results[0].className);
//select('#result').html(results[0].className);
//select('#probability').html(nf(results[0].probability, 0, 2));
if (!stop) {
classifyVideo();
}
}