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

Commit f91d873

Browse files
committed
Merge remote-tracking branch 'justadudewhohacks/master'
2 parents 3e56e05 + 9d694b2 commit f91d873

File tree

11 files changed

+136
-49
lines changed

11 files changed

+136
-49
lines changed

cc/modules/dnn/Net.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ NAN_MODULE_INIT(Net::Init) {
2020
Nan::SetPrototypeMethod(ctor, "setInputAsync", SetInputAsync);
2121
Nan::SetPrototypeMethod(ctor, "forward", Forward);
2222
Nan::SetPrototypeMethod(ctor, "forwardAsync", ForwardAsync);
23+
Nan::SetPrototypeMethod(ctor, "getLayerNames", GetLayerNames);
24+
Nan::SetPrototypeMethod(ctor, "getLayerNamesAsync", GetLayerNamesAsync);
25+
Nan::SetPrototypeMethod(ctor, "getUnconnectedOutLayers", GetUnconnectedOutLayers);
26+
Nan::SetPrototypeMethod(ctor, "getUnconnectedOutLayersAsync", GetUnconnectedOutLayersAsync);
2327

2428
target->Set(Nan::New("Net").ToLocalChecked(), ctor->GetFunction());
2529
};
@@ -63,4 +67,32 @@ NAN_METHOD(Net::ForwardAsync) {
6367
);
6468
}
6569

70+
NAN_METHOD(Net::GetLayerNames) {
71+
FF::SyncBinding(
72+
std::make_shared<NetBindings::GetLayerNamesWorker>(Net::Converter::unwrap(info.This())),
73+
"Net::GetLayerNames",
74+
info);
75+
}
76+
77+
NAN_METHOD(Net::GetLayerNamesAsync) {
78+
FF::AsyncBinding(
79+
std::make_shared<NetBindings::GetLayerNamesWorker>(Net::Converter::unwrap(info.This())),
80+
"Net::GetLayerNamesAsync",
81+
info);
82+
}
83+
84+
NAN_METHOD(Net::GetUnconnectedOutLayers) {
85+
FF::SyncBinding(
86+
std::make_shared<NetBindings::GetUnconnectedOutLayersWorker>(Net::Converter::unwrap(info.This())),
87+
"Net::GetUnconnectedOutLayers",
88+
info);
89+
}
90+
91+
NAN_METHOD(Net::GetUnconnectedOutLayersAsync) {
92+
FF::AsyncBinding(
93+
std::make_shared<NetBindings::GetUnconnectedOutLayersWorker>(Net::Converter::unwrap(info.This())),
94+
"Net::GetUnconnectedOutLayersAsync",
95+
info);
96+
}
97+
6698
#endif

cc/modules/dnn/Net.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Net : public Nan::ObjectWrap {
2828
static NAN_METHOD(SetInputAsync);
2929
static NAN_METHOD(Forward);
3030
static NAN_METHOD(ForwardAsync);
31+
static NAN_METHOD(GetLayerNames);
32+
static NAN_METHOD(GetLayerNamesAsync);
33+
static NAN_METHOD(GetUnconnectedOutLayers);
34+
static NAN_METHOD(GetUnconnectedOutLayersAsync);
3135
};
3236

3337
#endif

cc/modules/dnn/NetBindings.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,47 @@ namespace NetBindings {
7979
}
8080
};
8181

82+
struct GetLayerNamesWorker : public CatchCvExceptionWorker {
83+
public:
84+
cv::dnn::Net self;
85+
GetLayerNamesWorker(cv::dnn::Net self) {
86+
this->self = self;
87+
}
88+
89+
std::vector<std::string> returnValue;
90+
91+
std::string executeCatchCvExceptionWorker() {
92+
std::vector<cv::String> layerNames = self.getLayerNames();
93+
std::vector<std::string> strings(
94+
layerNames.begin(),
95+
layerNames.end());
96+
returnValue = strings;
97+
return "";
98+
}
99+
100+
v8::Local<v8::Value> getReturnValue() {
101+
return StringArrayConverter::wrap(returnValue);
102+
}
103+
};
104+
105+
struct GetUnconnectedOutLayersWorker : public CatchCvExceptionWorker {
106+
public:
107+
cv::dnn::Net self;
108+
GetUnconnectedOutLayersWorker(cv::dnn::Net self) {
109+
this->self = self;
110+
}
111+
112+
std::vector<int> layerIndexes;
113+
114+
std::string executeCatchCvExceptionWorker() {
115+
layerIndexes = self.getUnconnectedOutLayers();
116+
return "";
117+
}
118+
119+
v8::Local<v8::Value> getReturnValue() {
120+
return IntArrayConverter::wrap(layerIndexes);
121+
}
122+
};
82123

83124
}
84125

cc/modules/dnn/dnn.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ NAN_MODULE_INIT(Dnn::Init) {
2020
Nan::SetMethod(target, "blobFromImages", BlobFromImages);
2121
Nan::SetMethod(target, "blobFromImagesAsync", BlobFromImagesAsync);
2222
#if CV_VERSION_MINOR > 3
23-
Nan::SetMethod(target, "readNetFromDarknet", readNetFromDarknet);
24-
Nan::SetMethod(target, "readNetFromDarknetAsync", readNetFromDarknetAsync);
23+
Nan::SetMethod(target, "readNetFromDarknet", ReadNetFromDarknet);
24+
Nan::SetMethod(target, "readNetFromDarknetAsync", ReadNetFromDarknetAsync);
2525
Nan::SetMethod(target, "NMSBoxes", NMSBoxes);
2626
#endif
2727
};
@@ -92,19 +92,17 @@ NAN_METHOD(Dnn::BlobFromImagesAsync) {
9292
}
9393

9494
#if CV_VERSION_MINOR > 3
95-
NAN_METHOD(Dnn::readNetFromDarknet)
96-
{
95+
NAN_METHOD(Dnn::ReadNetFromDarknet) {
9796
FF::SyncBinding(
98-
std::make_shared<DnnBindings::readNetFromDarknetWorker>(),
99-
"readNetFromDarknet",
97+
std::make_shared<DnnBindings::ReadNetFromDarknetWorker>(),
98+
"ReadNetFromDarknet",
10099
info);
101100
}
102101

103-
NAN_METHOD(Dnn::readNetFromDarknetAsync)
104-
{
102+
NAN_METHOD(Dnn::ReadNetFromDarknetAsync) {
105103
FF::AsyncBinding(
106-
std::make_shared<DnnBindings::readNetFromDarknetWorker>(),
107-
"readNetFromDarknetAsync",
104+
std::make_shared<DnnBindings::ReadNetFromDarknetWorker>(),
105+
"ReadNetFromDarknetAsync",
108106
info);
109107
}
110108

cc/modules/dnn/dnn.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Dnn {
2020
static NAN_METHOD(BlobFromImages);
2121
static NAN_METHOD(BlobFromImagesAsync);
2222
#if CV_VERSION_MINOR > 3
23-
static NAN_METHOD(readNetFromDarknet);
24-
static NAN_METHOD(readNetFromDarknetAsync);
23+
static NAN_METHOD(ReadNetFromDarknet);
24+
static NAN_METHOD(ReadNetFromDarknetAsync);
2525
static NAN_METHOD(NMSBoxes);
2626
#endif
2727
};

cc/modules/dnn/dnnBindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace DnnBindings {
77

88
#if CV_VERSION_MINOR > 3
9-
struct readNetFromDarknetWorker : public CatchCvExceptionWorker{
9+
struct ReadNetFromDarknetWorker : public CatchCvExceptionWorker{
1010
public:
1111
std::string cfgFile;
1212
std::string darknetModelFile = "";

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

lib/typings/VideoWriter.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Size } from './Size.d';
33

44
export class VideoWriter {
55
constructor(filePath: string, fourccCode: number, fps: number, frameSize: Size, isColor?: boolean);
6-
fourcc(fourcc: string): number;
6+
static fourcc(fourcc: string): number;
77
get(property: number): void;
88
release(): void;
99
set(property: number, value: number): void;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opencv4nodejs",
3-
"version": "4.11.0",
3+
"version": "4.12.0",
44
"description": "Asynchronous OpenCV 3.x nodejs bindings with JavaScript and TypeScript API.",
55
"keywords": [
66
"opencv",

0 commit comments

Comments
 (0)