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

Commit d0a457c

Browse files
committed
improve darknet and tensorflow object detection examples
1 parent 730b900 commit d0a457c

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

examples/dnnDarknetYOLORealTimeObjectDetection.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
const fs = require("fs");
66
const path = require("path");
7-
const { cv, drawBlueRect, runVideoDetection } = require("./utils");
7+
const { cv, runVideoDetection } = require("./utils");
88

99
if (!cv.xmodules.dnn) {
1010
throw new Error("exiting: opencv4nodejs compiled without dnn module");
@@ -41,6 +41,13 @@ const labels = fs
4141

4242
// initialize tensorflow darknet model from modelFile
4343
const net = cv.readNetFromDarknet(cfgFile, weightsFile);
44+
const allLayerNames = net.getLayerNames();
45+
const unconnectedOutLayers = net.getUnconnectedOutLayers();
46+
47+
// determine only the *output* layer names that we need from YOLO
48+
const layerNames = unconnectedOutLayers.map(layerIndex => {
49+
return allLayerNames[layerIndex - 1];
50+
});
4451

4552
const classifyImg = img => {
4653
// object detection model works with 416 x 416 images
@@ -49,12 +56,9 @@ const classifyImg = img => {
4956
const [imgHeight, imgWidth] = img.sizes;
5057

5158
// network accepts blobs as input
52-
const inputBlob = cv.blobFromImage(img, 1 / 255.0, size, vec3, true, true);
59+
const inputBlob = cv.blobFromImage(img, 1 / 255.0, size, vec3, true, false);
5360
net.setInput(inputBlob);
5461

55-
// specify two layers "yolo_16" and "yolo_23"
56-
const layerNames = ["yolo_16", "yolo_23"];
57-
5862
console.time("net.forward");
5963
// forward pass input through entire network
6064
const layerOutputs = net.forward(layerNames);
@@ -95,20 +99,25 @@ const classifyImg = img => {
9599

96100
indices.forEach(i => {
97101
const rect = boxes[i];
98-
const imgRect = new cv.Rect(rect.x, rect.y, rect.width, rect.height);
99-
drawBlueRect(img, imgRect);
102+
103+
const pt1 = new cv.Point(rect.x, rect.y);
104+
const pt2 = new cv.Point(rect.x + rect.width, rect.y + rect.height);
105+
const rectColor = new cv.Vec(255, 0, 0);
106+
const rectThickness = 2;
107+
const rectLineType = cv.LINE_8;
108+
109+
// draw the rect for the object
110+
img.drawRectangle(pt1, pt2, rectColor, rectThickness, rectLineType);
111+
100112
const text = labels[classIDs[i]];
101-
img.putText(
102-
text,
103-
new cv.Point(rect.x, rect.y + 0.1 * imgHeight),
104-
cv.FONT_ITALIC,
105-
2,
106-
{
107-
color: new cv.Vec(255, 0, 0),
108-
thickness: 2
109-
}
110-
);
111-
drawBlueRect(img, imgRect);
113+
const org = new cv.Point(rect.x, rect.y + 15);
114+
const fontFace = cv.FONT_HERSHEY_SIMPLEX;
115+
const fontScale = 0.5;
116+
const textColor = new cv.Vec(123, 123, 255);
117+
const thickness = 2;
118+
119+
// put text on the object
120+
img.putText(text, org, fontFace, fontScale, textColor, thickness);
112121
});
113122
}
114123
});

examples/dnnTensorflowObjectDetection.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const fs = require("fs");
66
const path = require("path");
77
const classNames = require("./dnnTensorflowObjectDetectionClassNames");
8-
const { cv, drawBlueRect, runVideoDetection } = require("./utils");
8+
const { cv, runVideoDetection } = require("./utils");
99

1010
if (!cv.xmodules.dnn) {
1111
throw new Error("exiting: opencv4nodejs compiled without dnn module");
@@ -51,7 +51,7 @@ const classifyImg = img => {
5151

5252
// get height and width from the image
5353
const [imgHeight, imgWidth] = img.sizes;
54-
const numRows = outputBlob.sizes.slice(2,3);
54+
const numRows = outputBlob.sizes.slice(2, 3);
5555

5656
for (let y = 0; y < numRows; y += 1) {
5757
const confidence = outputBlob.at([0, 0, y, 2]);
@@ -62,22 +62,25 @@ const classifyImg = img => {
6262
const boxY = imgHeight * outputBlob.at([0, 0, y, 4]);
6363
const boxWidht = imgWidth * outputBlob.at([0, 0, y, 5]);
6464
const boxHeight = imgHeight * outputBlob.at([0, 0, y, 6]);
65-
const imgRect = new cv.Rect(boxX, boxY, boxWidht, boxHeight);
6665

67-
// draw the blue rect for the object
68-
drawBlueRect(img, imgRect);
66+
const pt1 = new cv.Point(boxX, boxY);
67+
const pt2 = new cv.Point(boxWidht, boxHeight);
68+
const rectColor = new cv.Vec(23, 230, 210);
69+
const rectThickness = 2;
70+
const rectLineType = cv.LINE_8;
71+
72+
// draw the rect for the object
73+
img.drawRectangle(pt1, pt2, rectColor, rectThickness, rectLineType);
74+
75+
const text = `${className} ${confidence.toFixed(5)}`;
76+
const org = new cv.Point(boxX, boxY + 15);
77+
const fontFace = cv.FONT_HERSHEY_SIMPLEX;
78+
const fontScale = 0.5;
79+
const textColor = new cv.Vec(255, 0, 0);
80+
const thickness = 2;
6981

7082
// put text on the object
71-
img.putText(
72-
className,
73-
new cv.Point(boxX, boxY + 0.1 * imgHeight),
74-
cv.FONT_ITALIC,
75-
2,
76-
{
77-
color: new cv.Vec(255, 0, 0),
78-
thickness: 2
79-
}
80-
);
83+
img.putText(text, org, fontFace, fontScale, textColor, thickness);
8184
}
8285
}
8386

0 commit comments

Comments
 (0)