Skip to content

Commit ce82f51

Browse files
committed
added detectStart and detectStop to cocossd.js and updated example code to call detectStart in setup()
1 parent 7deb4c8 commit ce82f51

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

examples/objectDetection/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>ml5.js objectDetection Webcam Example</title>
48
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
5-
<!-- <script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script> -->
69
<script src="../../dist/ml5.js"></script>
7-
<meta charset="utf-8" />
8-
910
</head>
1011
<body>
1112
<main>

examples/objectDetection/sketch.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ function setup() {
2424
video.size(640, 480);
2525
video.hide();
2626

27-
detector.detect(video, gotDetections);
27+
// detector.detect(video, gotDetections);
28+
detector.detectStart(video, gotDetections);
2829
}
2930

3031
function gotDetections(results) {
3132
detections = results;
32-
detector.detect(video, gotDetections);
33+
// console.log(results);
34+
// detector.detect(video, gotDetections);
3335
}
3436

3537
function draw() {

src/ObjectDetector/cocossd.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export class CocoSsdBase {
2727
constructor(video, options, constructorCallback) {
2828
this.video = video || null;
2929
this.modelReady = false;
30+
3031
this.isPredicting = false;
32+
this.signalStop = false;
33+
this.prevCall = "";
34+
3135
this.config = {
3236
base: options.base || DEFAULTS.base,
3337
modelUrl: options.modelUrl || DEFAULTS.modelUrl,
@@ -37,9 +41,6 @@ export class CocoSsdBase {
3741
this.ready = callCallback(this.loadModel(), this.callback);
3842
}
3943

40-
/**
41-
* load model
42-
*/
4344
async loadModel() {
4445
await tf.setBackend("webgl"); // this line resolves warning : performance is poor on webgpu backend
4546
await tf.ready();
@@ -74,8 +75,11 @@ export class CocoSsdBase {
7475
* @returns {ObjectDetectorPrediction}
7576
*/
7677
async detectInternal(imgToPredict) {
77-
this.isPredicting = true;
78-
mediaReady(imgToPredict, true)
78+
// this.isPredicting = true;
79+
await this.ready;
80+
81+
mediaReady(imgToPredict, true);
82+
7983
await tf.nextFrame();
8084

8185
const predictions = await this.model.detect(imgToPredict);
@@ -95,7 +99,9 @@ export class CocoSsdBase {
9599
},
96100
};
97101
});
102+
98103
this.isPredicting = false;
104+
99105
return formattedPredictions;
100106
}
101107

@@ -107,16 +113,48 @@ export class CocoSsdBase {
107113
* @returns {ObjectDetectorPrediction}
108114
*/
109115
async detect(inputOrCallback, cb) {
110-
await this.ready;
111-
await tf.nextFrame();
112-
113116
const args = handleArguments(this.video, inputOrCallback, cb);
114117
args.require("image", "Detection subject not supported");
115118

116-
await mediaReady(args.image, true);
117-
118119
return callCallback(this.detectInternal(args.image), args.callback);
119120
}
121+
122+
async detectStart(inputNumOrCallback, numOrCallback, cb){
123+
const { image, number, callback } = handleArguments(
124+
inputNumOrCallback,
125+
numOrCallback,
126+
cb
127+
).require("image", "No input provided.");
128+
129+
const detectFrame = async () => {
130+
await mediaReady(image, true);
131+
132+
await callCallback(this.detectInternal(image), callback);
133+
134+
if(!this.signalStop){
135+
requestAnimationFrame(detectFrame);
136+
} else {
137+
this.isPredicting = false;
138+
}
139+
};
140+
141+
// start the detection
142+
this.signalStop = false;
143+
if (!this.isPredicting) {
144+
this.isPredicting = true;
145+
detectFrame();
146+
}
147+
148+
if (this.prevCall === "start") {
149+
console.warn("warning");
150+
}
151+
this.prevCall = "start";
152+
}
153+
154+
detectStop(){
155+
if (this.isPredicting) { this.signalStop = true; }
156+
this.prevCall = "stop";
157+
}
120158
}
121159

122160
export const CocoSsd = (...inputs) => {

0 commit comments

Comments
 (0)