Skip to content

Commit 7deb4c8

Browse files
committed
load model in preload() ; updated ObjectDetector Class to return object (instance.model), added mediaReady utils so that detection waits for video to be ready before detecting, updated tf.backend to webgl to avoid webgpu warning
1 parent 4db442f commit 7deb4c8

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

examples/objectDetection/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
55
<!-- <script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script> -->
66
<script src="../../dist/ml5.js"></script>
7-
<link rel="stylesheet" type="text/css" href="style.css">
87
<meta charset="utf-8" />
98

109
</head>

examples/objectDetection/sketch.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,38 @@ let video;
1313
let detector;
1414
let detections = [];
1515

16+
function preload(){
17+
detector = ml5.objectDetector("cocossd");
18+
}
19+
1620
function setup() {
1721
createCanvas(640, 480);
18-
video = createCapture(VIDEO, videoReady);
22+
23+
video = createCapture(VIDEO);
1924
video.size(640, 480);
2025
video.hide();
21-
}
2226

23-
function videoReady() {
24-
detector = ml5.objectDetector('cocossd', modelReady);
27+
detector.detect(video, gotDetections);
2528
}
2629

2730
function gotDetections(results) {
2831
detections = results;
2932
detector.detect(video, gotDetections);
3033
}
3134

32-
function modelReady() {
33-
detector.detect(video, gotDetections);
34-
}
35-
3635
function draw() {
3736
image(video, 0, 0);
3837

3938
for (let i = 0; i < detections.length; i += 1) {
4039
const object = detections[i];
40+
41+
// draw bounding box
4142
stroke(0, 255, 0);
4243
strokeWeight(4);
4344
noFill();
4445
rect(object.x, object.y, object.width, object.height);
46+
47+
// draw label
4548
noStroke();
4649
fill(255);
4750
textSize(24);

src/ObjectDetector/cocossd.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as tf from "@tensorflow/tfjs";
1111
import * as cocoSsd from "@tensorflow-models/coco-ssd";
1212
import callCallback from "../utils/callcallback";
1313
import handleArguments from "../utils/handleArguments";
14+
import { mediaReady } from "../utils/imageUtilities";
1415

1516
const DEFAULTS = {
1617
base: "lite_mobilenet_v2",
@@ -40,6 +41,7 @@ export class CocoSsdBase {
4041
* load model
4142
*/
4243
async loadModel() {
44+
await tf.setBackend("webgl"); // this line resolves warning : performance is poor on webgpu backend
4345
await tf.ready();
4446

4547
this.model = await cocoSsd.load(this.config);
@@ -73,6 +75,9 @@ export class CocoSsdBase {
7375
*/
7476
async detectInternal(imgToPredict) {
7577
this.isPredicting = true;
78+
mediaReady(imgToPredict, true)
79+
await tf.nextFrame();
80+
7681
const predictions = await this.model.detect(imgToPredict);
7782
const formattedPredictions = predictions.map(prediction => {
7883
return {
@@ -108,6 +113,8 @@ export class CocoSsdBase {
108113
const args = handleArguments(this.video, inputOrCallback, cb);
109114
args.require("image", "Detection subject not supported");
110115

116+
await mediaReady(args.image, true);
117+
111118
return callCallback(this.detectInternal(args.image), args.callback);
112119
}
113120
}

src/ObjectDetector/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
ObjectDetection
88
*/
99

10-
console.log("running ObjectDetection/index.js");
11-
1210
import handleArguments from "../utils/handleArguments";
1311
import {CocoSsd} from "./cocossd.js";
1412

@@ -27,7 +25,7 @@ class ObjectDetector {
2725
* @param {function} callback - Optional. A callback function that is called once the model has loaded.
2826
*/
2927
constructor(modelNameOrUrl, video, options, callback) {
30-
this.video = video;
28+
this.video = video || null;
3129
this.modelNameOrUrl = modelNameOrUrl;
3230
this.options = options || {};
3331
this.callback = callback;
@@ -56,9 +54,8 @@ const objectDetector = (...inputs) => {
5654

5755
const instance = new ObjectDetector(model, video, options, callback);
5856

59-
console.log("model", instance.model, instance.model.ready);
60-
61-
return instance.model.callback ? instance.model : instance.model.ready;
57+
// return instance.model.callback ? instance.model : instance.model.ready;
58+
return instance.model;
6259
};
6360

6461
export default objectDetector;

0 commit comments

Comments
 (0)