Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 58d72c6

Browse files
author
Bennett Hardwick
committed
Updated version number and examples
1 parent 652238e commit 58d72c6

File tree

6 files changed

+106
-31
lines changed

6 files changed

+106
-31
lines changed

README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
A Node wrapper of pjreddie's open source neural network framework Darknet, using the Foreign Function Interface Library. Read: YOLOv3 in JavaScript.
33

44
## Prerequisites
5-
- Linux, Windows (Linux sub-system), Mac (probably)
5+
- Linux, Mac, Windows (Linux sub-system),
66
- Node (most versions will work, darknet.js <=1.1.5 only works on node <=8.11.2)
77
- Build tools (make, gcc, etc.)
88

@@ -37,6 +37,34 @@ let darknet = new Darknet({
3737
console.log(darknet.detect('/image/of/a/dog.jpg'));
3838
```
3939

40+
In conjuction with [opencv4nodejs](https://github.com/justadudewhohacks/opencv4nodejs), Darknet.js can also be used to detect objects inside videos.
41+
```javascript
42+
const fs = require('fs');
43+
const cv = require('opencv4nodejs');
44+
const { Darknet } = require('darknet');
45+
46+
const darknet = new Darknet({
47+
weights: 'yolov3.weights',
48+
config: 'cfg/yolov3.cfg',
49+
nameFile: 'data/coco.names'
50+
});
51+
52+
const cap = new cv.VideoCapture('video.mp4');
53+
54+
let frame;
55+
let index = 0;
56+
do {
57+
frame = cap.read().cvtColor(cv.COLOR_BGR2RGB);
58+
console.log('frame', index++);
59+
console.log(darknet.detect({
60+
b: frame.getData(),
61+
w: frame.cols,
62+
h: frame.rows,
63+
c: frame.channels
64+
}));
65+
} while(!frame.empty);
66+
```
67+
4068
### Example Configuration
4169
You can download pre-trained weights and configuration from pjreddie's website. The latest version (yolov3-tiny) is linked below:
4270
- [weights](https://pjreddie.com/media/files/yolov3-tiny.weights)

darknet.d.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,51 @@ export declare class Darknet {
99
* @param config
1010
*/
1111
constructor(config: IDarknetConfig);
12-
private getArrayFromBuffer;
13-
private bufferToDetections;
14-
private _detectSync;
15-
private _detectAsync;
12+
private getArrayFromBuffer(buffer, length, type);
13+
private bufferToDetections(buffer, length);
14+
private _detectSync(net, meta, image, thresh?, hier_thresh?, nms?);
15+
private _detectAsync(net, meta, image, thresh?, hier_thresh?, nms?);
1616
/**
1717
* Synchronously detect objects in an image.
1818
* @param image the destination of the image to be detected
1919
* @param config optional configuration (threshold, etc.)
2020
*/
2121
detect(image: string | IBufferImage, config?: IConfig): Detection[];
22+
/**
23+
* Get a Darknet Image from path
24+
* @param path
25+
* @returns IMAGE
26+
*/
2227
getImageFromPath(path: string): any;
28+
/**
29+
* Get a Darknet Image async from path
30+
* @param path
31+
* @returns Promise<IMAGE>
32+
*/
2333
getImageFromPathAsync(path: String): Promise<{}>;
34+
/**
35+
* convert darknet image to rgb buffer
36+
* @param {IMAGE} image
37+
* @returns {Buffer}
38+
*/
2439
imageToRGBBuffer(image: any): Buffer;
25-
private rgbToDarknet;
40+
private rgbToDarknet(buffer, w, h, c);
41+
/**
42+
* Transform an RGB buffer to a darknet encoded image
43+
* @param buffer - rgb buffer
44+
* @param w - width
45+
* @param h - height
46+
* @param c - channels
47+
* @returns {IMAGE}
48+
*/
2649
RGBBufferToImage(buffer: Buffer, w: number, h: number, c: number): any;
2750
/**
2851
* Transform an RGB buffer to a darknet encoded image
2952
* @param buffer - rgb buffer
3053
* @param w - width
3154
* @param h - height
3255
* @param c - channels
33-
* @returns Promise<IMAGE>
56+
* @returns {Promise<IMAGE>}
3457
*/
3558
RGBBufferToImageAsync(buffer: Buffer, w: number, h: number, c: number): Promise<any>;
3659
/**

darknet.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ var Darknet = /** @class */ (function () {
151151
};
152152
Darknet.prototype._detectAsync = function (net, meta, image, thresh, hier_thresh, nms) {
153153
return __awaiter(this, void 0, void 0, function () {
154-
var pnum, dets, num, detections;
155154
var _this = this;
155+
var pnum, dets, num, detections;
156156
return __generator(this, function (_a) {
157157
switch (_a.label) {
158158
case 0: return [4 /*yield*/, new Promise(function (res, rej) {
@@ -201,9 +201,19 @@ var Darknet = /** @class */ (function () {
201201
}
202202
return detection;
203203
};
204+
/**
205+
* Get a Darknet Image from path
206+
* @param path
207+
* @returns IMAGE
208+
*/
204209
Darknet.prototype.getImageFromPath = function (path) {
205210
return this.darknet.load_image_color(path, 0, 0);
206211
};
212+
/**
213+
* Get a Darknet Image async from path
214+
* @param path
215+
* @returns Promise<IMAGE>
216+
*/
207217
Darknet.prototype.getImageFromPathAsync = function (path) {
208218
return __awaiter(this, void 0, void 0, function () {
209219
var _this = this;
@@ -214,6 +224,11 @@ var Darknet = /** @class */ (function () {
214224
});
215225
});
216226
};
227+
/**
228+
* convert darknet image to rgb buffer
229+
* @param {IMAGE} image
230+
* @returns {Buffer}
231+
*/
217232
Darknet.prototype.imageToRGBBuffer = function (image) {
218233
var w = image.w;
219234
var h = image.h;
@@ -246,6 +261,14 @@ var Darknet = /** @class */ (function () {
246261
}
247262
return floatBuff;
248263
};
264+
/**
265+
* Transform an RGB buffer to a darknet encoded image
266+
* @param buffer - rgb buffer
267+
* @param w - width
268+
* @param h - height
269+
* @param c - channels
270+
* @returns {IMAGE}
271+
*/
249272
Darknet.prototype.RGBBufferToImage = function (buffer, w, h, c) {
250273
var floatBuff = this.rgbToDarknet(buffer, w, h, c);
251274
return this.darknet.float_to_image(w, h, c, new Uint8Array(floatBuff.buffer, 0, floatBuff.length * Float32Array.BYTES_PER_ELEMENT));
@@ -256,12 +279,12 @@ var Darknet = /** @class */ (function () {
256279
* @param w - width
257280
* @param h - height
258281
* @param c - channels
259-
* @returns Promise<IMAGE>
282+
* @returns {Promise<IMAGE>}
260283
*/
261284
Darknet.prototype.RGBBufferToImageAsync = function (buffer, w, h, c) {
262285
return __awaiter(this, void 0, void 0, function () {
263-
var floatBuff;
264286
var _this = this;
287+
var floatBuff;
265288
return __generator(this, function (_a) {
266289
floatBuff = this.rgbToDarknet(buffer, w, h, c);
267290
return [2 /*return*/, new Promise(function (res, rej) { return _this.darknet.float_to_image.async(w, h, c, new Uint8Array(floatBuff.buffer, 0, floatBuff.length * Float32Array.BYTES_PER_ELEMENT), function (e, image) { return e ? rej(e) : res(image); }); })];
@@ -276,8 +299,8 @@ var Darknet = /** @class */ (function () {
276299
*/
277300
Darknet.prototype.detectAsync = function (image, config) {
278301
return __awaiter(this, void 0, void 0, function () {
279-
var thresh, hier_thresh, nms, darkNetLoadedImage, imageData, _a, detection;
280302
var _this = this;
303+
var thresh, hier_thresh, nms, darkNetLoadedImage, imageData, _a, detection;
281304
return __generator(this, function (_b) {
282305
switch (_b.label) {
283306
case 0:

examples/yolov3-tiny.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Darknet = require('../darknet').Darknet;
1+
const { Darknet } = require('../darknet');
22

33
const darknet = new Darknet({
44
weights: 'yolov3-tiny.weights',
@@ -8,4 +8,4 @@ const darknet = new Darknet({
88

99
console.log("Dog:", darknet.detect('./dog.jpg'));
1010
console.log("Eagle:", darknet.detect('./eagle.jpg'));
11-
console.log("Giraffe:", darknet.detect('./giraffe.jpg'));
11+
console.log("Giraffe:", darknet.detect('./giraffe.jpg'));

package-lock.json

+18-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "darknet",
3-
"version": "1.1.6",
3+
"version": "1.2.6",
44
"description": "",
55
"main": "darknet.js",
66
"typings": "darknet.d.ts",

0 commit comments

Comments
 (0)