xxxxxxxxxx
60
// Copyright (c) 2020 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Object Detection using COCOSSD
This example uses a callback pattern to create the classifier
=== */
let video;
let videoLoaded = false;
let detector;
let detections = [];
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
// Models available are 'cocossd', 'yolo'
detector = ml5.objectDetector('cocossd', modelReady);
video.addEventListener('loadeddata', (event) => {
videoLoaded = true;
modelReady()
});
}
function gotDetections(error, results) {
if (error) {
console.error(error);
}
detections = results;
detector.detect(video, gotDetections);
}
function modelReady() {
if(videoLoaded){
detector.detect(video, gotDetections);
}
}
function draw() {
image(video, 0, 0);
if(detections && video){
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);
noStroke();
fill(255);
textSize(24);
let objectLabel = object.label + " " + (Math.round(object.confidence * 100) /100)
text(objectLabel, object.x + 10, object.y + 24);
}
}
}