Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 03b15b4

Browse files
author
Johnson Su
committed
Add tensorflow object detection example
1 parent 4e5c901 commit 03b15b4

File tree

3 files changed

+174
-1
lines changed

3 files changed

+174
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const classNames = require("./dnnTensorflowObjectDetectionClassNames");
4+
const { cv, drawBlueRect, runVideoDetection } = require("./utils");
5+
6+
if (!cv.xmodules.dnn) {
7+
throw new Error("exiting: opencv4nodejs compiled without dnn module");
8+
}
9+
10+
// replace with path where you unzipped detection model
11+
const detectionModelPath = "../data/dnn/tf-detection";
12+
13+
const pbFile = path.resolve(detectionModelPath, "frozen_inference_graph.pb");
14+
const pbtxtFile = path.resolve(
15+
detectionModelPath,
16+
"ssd_mobilenet_v2_coco_2018_03_29.pbtxt"
17+
);
18+
19+
if (!fs.existsSync(pbFile) || !fs.existsSync(pbtxtFile)) {
20+
console.log("could not find detection model");
21+
console.log(
22+
"download the model from: https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API#use-existing-config-file-for-your-model"
23+
);
24+
throw new Error("exiting");
25+
}
26+
27+
// set webcam port
28+
const webcamPort = 0;
29+
30+
// initialize tensorflow darknet model from modelFile
31+
const net = cv.readNetFromTensorflow(pbFile, pbtxtFile);
32+
33+
const classifyImg = img => {
34+
// object detection model works with 300 x 300 images
35+
const size = new cv.Size(300, 300);
36+
const vec3 = new cv.Vec(0, 0, 0);
37+
38+
// network accepts blobs as input
39+
const inputBlob = cv.blobFromImage(img, 1, size, vec3, true, true);
40+
net.setInput(inputBlob);
41+
42+
console.time("net.forward");
43+
// forward pass input through entire network, will return
44+
// classification result as 1x1xNxM Mat
45+
const outputBlob = net.forward();
46+
console.timeEnd("net.forward");
47+
48+
// get height and width from the image
49+
const [imgHeight, imgWidth] = img.sizes;
50+
const numRows = outputBlob.sizes.slice(2,3);
51+
52+
for (let y = 0; y < numRows; y += 1) {
53+
const confidence = outputBlob.at([0, 0, y, 2]);
54+
if (confidence > 0.5) {
55+
const classId = outputBlob.at([0, 0, y, 1]);
56+
const className = classNames[classId];
57+
const boxX = imgWidth * outputBlob.at([0, 0, y, 3]);
58+
const boxY = imgHeight * outputBlob.at([0, 0, y, 4]);
59+
const boxWidht = imgWidth * outputBlob.at([0, 0, y, 5]);
60+
const boxHeight = imgHeight * outputBlob.at([0, 0, y, 6]);
61+
const imgRect = new cv.Rect(boxX, boxY, boxWidht, boxHeight);
62+
63+
// draw the blue rect for the object
64+
drawBlueRect(img, imgRect);
65+
66+
// put text on the object
67+
img.putText(
68+
className,
69+
new cv.Point(boxX, boxY + 0.1 * imgHeight),
70+
cv.FONT_ITALIC,
71+
2,
72+
{
73+
color: new cv.Vec(255, 0, 0),
74+
thickness: 2
75+
}
76+
);
77+
}
78+
}
79+
80+
cv.imshow("Temsorflow Object Detection", img);
81+
};
82+
83+
runVideoDetection(webcamPort, classifyImg);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module.exports = {
2+
0: "background",
3+
1: "person",
4+
2: "bicycle",
5+
3: "car",
6+
4: "motorcycle",
7+
5: "airplane",
8+
6: "bus",
9+
7: "train",
10+
8: "truck",
11+
9: "boat",
12+
10: "traffic light",
13+
11: "fire hydrant",
14+
13: "stop sign",
15+
14: "parking meter",
16+
15: "bench",
17+
16: "bird",
18+
17: "cat",
19+
18: "dog",
20+
19: "horse",
21+
20: "sheep",
22+
21: "cow",
23+
22: "elephant",
24+
23: "bear",
25+
24: "zebra",
26+
25: "giraffe",
27+
27: "backpack",
28+
28: "umbrella",
29+
31: "handbag",
30+
32: "tie",
31+
33: "suitcase",
32+
34: "frisbee",
33+
35: "skis",
34+
36: "snowboard",
35+
37: "sports ball",
36+
38: "kite",
37+
39: "baseball bat",
38+
40: "baseball glove",
39+
41: "skateboard",
40+
42: "surfboard",
41+
43: "tennis racket",
42+
44: "bottle",
43+
46: "wine glass",
44+
47: "cup",
45+
48: "fork",
46+
49: "knife",
47+
50: "spoon",
48+
51: "bowl",
49+
52: "banana",
50+
53: "apple",
51+
54: "sandwich",
52+
55: "orange",
53+
56: "broccoli",
54+
57: "carrot",
55+
58: "hot dog",
56+
59: "pizza",
57+
60: "donut",
58+
61: "cake",
59+
62: "chair",
60+
63: "couch",
61+
64: "potted plant",
62+
65: "bed",
63+
67: "dining table",
64+
70: "toilet",
65+
72: "tv",
66+
73: "laptop",
67+
74: "mouse",
68+
75: "remote",
69+
76: "keyboard",
70+
77: "cell phone",
71+
78: "microwave",
72+
79: "oven",
73+
80: "toaster",
74+
81: "sink",
75+
82: "refrigerator",
76+
84: "book",
77+
85: "clock",
78+
86: "vase",
79+
87: "scissors",
80+
88: "teddy bear",
81+
89: "hair drier",
82+
90: "toothbrush"
83+
};

examples/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const dataPath = path.resolve(__dirname, '../data');
88
exports.dataPath = dataPath;
99
exports.getDataFilePath = fileName => path.resolve(dataPath, fileName);
1010

11-
exports.grabFrames = (videoFile, delay, onFrame) => {
11+
const grabFrames = (videoFile, delay, onFrame) => {
1212
const cap = new cv.VideoCapture(videoFile);
1313
let done = false;
1414
const intvl = setInterval(() => {
@@ -28,6 +28,13 @@ exports.grabFrames = (videoFile, delay, onFrame) => {
2828
}
2929
}, 0);
3030
};
31+
exports.grabFrames = grabFrames;
32+
33+
exports.runVideoDetection = (src, detect) => {
34+
grabFrames(src, 1, frame => {
35+
detect(frame);
36+
});
37+
};
3138

3239
exports.drawRectAroundBlobs = (binaryImg, dstImg, minPxSize, fixedRectWidth) => {
3340
const {

0 commit comments

Comments
 (0)