xxxxxxxxxx
104
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Webcam Image Classification using MobileNet and p5.js
This example uses a callback pattern to create the classifier
=== */
let classifier;
let video;
let myResults
let resultLabel
let myColor;
let detector
let detections=[]
// Create a new p5.speech object
// You can also control the Language, Rate, Pitch and Volumn of the voice
// Read more at http://ability.nyu.edu/p5.js-speech/
const myVoice = new p5.Speech();
function preload(){
detector = ml5.objectDetector('cocossd');
}
function gotDetections(error, results) {
if (error) {
console.error(error);
}
detections = results;
detector.detect(video, gotDetections);
}
function setup() {
noCanvas();
// 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
detector.detect(video, gotDetections);
}
function draw(){
for (let i = 0; i < detections.length; i++) {
let object = detections[i];
stroke(0, 255, 0);
strokeWeight(4);
noFill();
rect(object.x, object.y, object.width, object.height);
ellipse(object.x + object.width/2, object.y + object.height/2, 10, 10)
noStroke();
fill(255);
textSize(24);
text(object.label, object.x + 10, object.y + 24);
}
}
function keyTyped(){
if (key === 'a') {
if (resultLabel == "hair spray"){
myVoice.speak(`Is that a ${myColor}${resultLabel}?`);
}
}
}
//punching bag, punch bag, punching ball, punchball
// function modelReady() {
// // Change the status of the model once its ready
// select('#status').html('Model Loaded');
// // Call the classifyVideo function to start classifying the video
// classifyVideo();
// }
// Get a prediction for the current video frame
// function classifyVideo() {
// classifier.classify(gotResult);
// }
// When we get a result
// function gotResult(err, results) {
// myResults = results
// resultLabel = results[0].label
// // The results are in an array ordered by confidence.
// select('#result').html(results[0].label);
// select('#probability').html(nf(results[0].confidence, 0, 2));
// console.log(myResults)
// classifyVideo();
// }