Skip to content

Commit 4db442f

Browse files
committed
add code to build objectDetector, change example code callback to return result not error
1 parent 80c35b2 commit 4db442f

File tree

7 files changed

+27
-11
lines changed

7 files changed

+27
-11
lines changed

examples/objectDetection/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<html lang="en">
33
<head>
44
<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> -->
6-
<script src="../../dist/ml5.js"></script>
5+
<!-- <script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script> -->
6+
<script src="../../dist/ml5.js"></script>
77
<link rel="stylesheet" type="text/css" href="style.css">
88
<meta charset="utf-8" />
99

examples/objectDetection/sketch.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ function setup() {
2121
}
2222

2323
function videoReady() {
24-
// Models available are 'cocossd', 'yolo'
2524
detector = ml5.objectDetector('cocossd', modelReady);
2625
}
2726

28-
function gotDetections(error, results) {
29-
if (error) {
30-
console.error(error);
31-
}
27+
function gotDetections(results) {
3228
detections = results;
3329
detector.detect(video, gotDetections);
3430
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@mediapipe/pose": "^0.5.1675469404",
5757
"@mediapipe/selfie_segmentation": "~0.1.0",
5858
"@tensorflow-models/body-segmentation": "^1.0.1",
59+
"@tensorflow-models/coco-ssd": "^2.2.3",
5960
"@tensorflow-models/face-landmarks-detection": "1.0.5",
6061
"@tensorflow-models/hand-pose-detection": "^2.0.0",
6162
"@tensorflow-models/mobilenet": "^2.1.0",

src/ObjectDetector/cocossd.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class CocoSsdBase {
4040
* load model
4141
*/
4242
async loadModel() {
43+
await tf.ready();
44+
4345
this.model = await cocoSsd.load(this.config);
4446

4547
this.modelReady = true;
@@ -115,4 +117,4 @@ export const CocoSsd = (...inputs) => {
115117
return new CocoSsdBase(video, options, callback);
116118
};
117119

118-
// export default CocoSsd;
120+
export default CocoSsd;

src/ObjectDetector/index.js

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

10+
console.log("running ObjectDetection/index.js");
11+
1012
import handleArguments from "../utils/handleArguments";
1113
import {CocoSsd} from "./cocossd.js";
1214

@@ -32,11 +34,11 @@ class ObjectDetector {
3234

3335
switch (modelNameOrUrl) {
3436
case "cocossd":
35-
this.model = new CocoSsd(this.video, this.options, callback);
37+
this.model = CocoSsd(this.video, this.options, callback);
3638
return this;
3739
default:
3840
// use cocossd as default
39-
this.model = new CocoSsd(this.video, this.options, callback);
41+
this.model = CocoSsd(this.video, this.options, callback);
4042
return this;
4143
}
4244
}
@@ -54,6 +56,8 @@ const objectDetector = (...inputs) => {
5456

5557
const instance = new ObjectDetector(model, video, options, callback);
5658

59+
console.log("model", instance.model, instance.model.ready);
60+
5761
return instance.model.callback ? instance.model : instance.model.ready;
5862
};
5963

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import faceMesh from "./FaceMesh";
55
import bodyPose from "./BodyPose";
66
import imageClassifier from "./ImageClassifier";
77
import soundClassifier from "./SoundClassifier";
8+
import objectDetector from "./ObjectDetector";
89
import setBackend from "./utils/setBackend";
910
import bodySegmentation from "./BodySegmentation";
1011
import communityStatement from "./utils/communityStatement";
@@ -22,6 +23,7 @@ const withPreload = {
2223
neuralNetwork,
2324
sentiment,
2425
soundClassifier,
26+
objectDetector
2527
};
2628

2729
const ml5 = Object.assign({ p5Utils }, withPreload, {
@@ -36,4 +38,4 @@ p5Utils.shouldPreload(ml5, Object.keys(withPreload));
3638

3739
communityStatement();
3840

39-
export default ml5;
41+
export default ml5;

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,16 @@ __metadata:
17311731
languageName: node
17321732
linkType: hard
17331733

1734+
"@tensorflow-models/coco-ssd@npm:^2.2.3":
1735+
version: 2.2.3
1736+
resolution: "@tensorflow-models/coco-ssd@npm:2.2.3"
1737+
peerDependencies:
1738+
"@tensorflow/tfjs-converter": ^4.10.0
1739+
"@tensorflow/tfjs-core": ^4.10.0
1740+
checksum: 10c0/3d0e54d433e388439a461f9e7b4995bcfab7825d49eaf587818800549c54adc8192dab1b5a82e0ef48d87065ce155261a3ce934000477ac0da65de6939568e2e
1741+
languageName: node
1742+
linkType: hard
1743+
17341744
"@tensorflow-models/face-landmarks-detection@npm:1.0.5":
17351745
version: 1.0.5
17361746
resolution: "@tensorflow-models/face-landmarks-detection@npm:1.0.5"
@@ -6537,6 +6547,7 @@ __metadata:
65376547
"@mediapipe/pose": "npm:^0.5.1675469404"
65386548
"@mediapipe/selfie_segmentation": "npm:~0.1.0"
65396549
"@tensorflow-models/body-segmentation": "npm:^1.0.1"
6550+
"@tensorflow-models/coco-ssd": "npm:^2.2.3"
65406551
"@tensorflow-models/face-landmarks-detection": "npm:1.0.5"
65416552
"@tensorflow-models/hand-pose-detection": "npm:^2.0.0"
65426553
"@tensorflow-models/mobilenet": "npm:^2.1.0"

0 commit comments

Comments
 (0)