Skip to content

Commit 49b2e8e

Browse files
committed
update objectDetection-webcam example
1 parent 9375570 commit 49b2e8e

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed
File renamed without changes.

examples/objectDetection/sketch.js renamed to examples/objectDetection-webcam/sketch.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
// Copyright (c) 2020 ml5
2-
//
3-
// This software is released under the MIT License.
4-
// https://opensource.org/licenses/MIT
5-
6-
/* ===
7-
ml5 Example
8-
Object Detection using COCOSSD
9-
This example uses a callback pattern to create the classifier
10-
=== */
1+
/*
2+
* 👋 Hello! This is an ml5.js example made and shared with ❤️.
3+
* Learn more about the ml5.js project: https://ml5js.org/
4+
* ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md
5+
*
6+
* This example demonstrates detecting objects in a live video through ml5.imageClassifier.
7+
*/
118

129
let video;
1310
let detector;
@@ -20,30 +17,34 @@ function preload(){
2017
function setup() {
2118
createCanvas(640, 480);
2219

20+
// Using webcam feed as video input, hiding html element to avoid duplicate with canvas
2321
video = createCapture(VIDEO);
2422
video.size(width, height);
2523
video.hide();
2624

2725
detector.detectStart(video, gotDetections);
2826
}
2927

28+
// Callback function is called each time the object detector finishes processing a frame.
3029
function gotDetections(results) {
30+
// Update detections array with the new results
3131
detections = results;
3232
}
3333

3434
function draw() {
35+
// Draw the current video frame onto the canvas.
3536
image(video, 0, 0);
3637

3738
for (let i = 0; i < detections.length; i += 1) {
38-
const detection = detections[i];
39+
let detection = detections[i];
3940

40-
// draw bounding box
41+
// Draw bounding box
4142
stroke(0, 255, 0);
4243
strokeWeight(4);
4344
noFill();
4445
rect(detection.x, detection.y, detection.width, detection.height);
4546

46-
// draw label
47+
// Draw label
4748
noStroke();
4849
fill(255);
4950
textSize(24);

src/ObjectDetector/cocossd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class CocoSsd {
4040
*/
4141
async detect(imgToPredict) {
4242
mediaReady(imgToPredict, true);
43+
4344
await tf.nextFrame();
4445

4546
const detections = await this.model.detect(imgToPredict);

src/ObjectDetector/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import handleArguments from "../utils/handleArguments";
1313
import callCallback from "../utils/callcallback";
1414
import { mediaReady } from "../utils/imageUtilities";
1515

16-
const MODEL_OPTIONS = ["cocossd"]; // Expandable for other models like YOLO
16+
const MODEL_OPTIONS = ["cocossd", "yolo"]; // Expandable for other models like YOLO
1717

1818
class ObjectDetector {
1919
/**
@@ -24,8 +24,7 @@ class ObjectDetector {
2424
*/
2525
/**
2626
* Create ObjectDetector model. Works on video and images.
27-
* @param {string} modelNameOrUrl - The name or the URL of the model to use. Current model name options
28-
* are: 'YOLO' and 'CocoSsd'.
27+
* @param {string} modelNameOrUrl - The name or the URL of the model to use. Current model name options are: 'YOLO' and 'CocoSsd'.
2928
* @param {Object} options - Optional. A set of options.
3029
* @param {function} callback - Optional. A callback function that is called once the model has loaded.
3130
*/
@@ -46,7 +45,6 @@ class ObjectDetector {
4645
"objectDetector"
4746
);
4847

49-
5048
switch (this.modelName) {
5149
case "cocossd":
5250
this.modelToUse = cocoSsd;

0 commit comments

Comments
 (0)