Skip to content

Commit 2eb9958

Browse files
author
KJ Lawrence
committed
Added the ability to pass in regions for the image
1 parent 52e38fd commit 2eb9958

6 files changed

Lines changed: 61 additions & 12 deletions

File tree

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ node-openalpr
33

44
This package binds [OpenALPR](https://github.com/openalpr/openalpr) with Node.js
55

6+
Version: 1.0.7 - Released August 25th, 2015
7+
8+
```
9+
Changelog:
10+
11+
1.0.7 - Added the capability to specify regions
12+
1.0.6 - Slowed down the event loop to 30 times per second
13+
1.0.1:5 - Documentation changes
14+
1.0.0 - Initial release
15+
```
16+
617
# Installation and Example
718

819
Use npm to get the node-openalpr package. We'll attempt to use node-pre-gyp to compile from source, but if
@@ -55,9 +66,10 @@ This is a breakdown of all of the methods available for node-openalpr. Start nee
5566
* `openalpr.IdentifyLicense (path, options/callback[, callback])` - Begins the process of identifying a license from the given image, returns "working" or "queued" status result
5667
* path - Path to image - if image does not exist an exception will be thrown
5768
* callback/options - Additional options for the image or a callback
58-
* options.state (string) - State ("oh") license plates are in for additional validation
59-
* options.prewarp (string) - Prewarp configuration information
69+
* options.state (string) - State ("oh") license plates are in for additional validation
70+
* options.prewarp (string) - Prewarp configuration information
6071
* options.detectRegion (boolean) - Use detect region functionality of OpenALPR? (slower)
72+
* options.regions (array) - Specify the regions of the image to work on (format: [{ x: 0, y: 0, width: 0, height: 0 }, ...]
6173
* callback - Callback with results: function (errors, output)
6274
* `openalpr.GetVersion ()` - Get the version of OpenALPR currently being run against
6375

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-openalpr",
33
"description": "Node.js OpenALPR Bindings",
4-
"version": "1.0.6",
4+
"version": "1.0.7",
55
"license": "AGPL",
66
"keywords": [
77
"bindings",
@@ -22,7 +22,6 @@
2222
"email": "klawrence@netpark.us"
2323
}
2424
],
25-
2625
"repository": {
2726
"type": "git",
2827
"url": "git://github.com/netPark/node-openalpr.git"
6.05 KB
Binary file not shown.
2.5 KB
Binary file not shown.

src/node_openalpr.cc

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#include <alpr.h>
77
#include <config.h>
88
#include <string.h>
9+
#include <fstream>
910
#include <list>
11+
#include <vector>
1012

1113
#ifdef _WIN32
1214
#pragma comment(lib, "Ws2_32.lib")
@@ -19,11 +21,12 @@ bool running = false;
1921

2022
class LPRQueueItem
2123
{
22-
public:
24+
public:
2325
char *path;
2426
char *state;
2527
std::string prewarp;
2628
bool detectRegion = false;
29+
std::vector <alpr::AlprRegionOfInterest> regions;
2730
Nan::Callback *callback;
2831
};
2932

@@ -48,7 +51,18 @@ class LPR {
4851
this->openalpr->setDetectRegion (queueItem->detectRegion);
4952
this->config->prewarp = queueItem->prewarp;
5053

51-
return this->openalpr->recognize (queueItem->path);
54+
std::ifstream ifs (queueItem->path, std::ios::binary|std::ios::ate);
55+
std::ifstream::pos_type pos = ifs.tellg ();
56+
std::vector<char> buffer(pos);
57+
ifs.seekg(0, std::ios::beg);
58+
ifs.read(&buffer[0], pos);
59+
60+
if (queueItem->regions.size ()) {
61+
return this->openalpr->recognize (buffer, queueItem->regions);
62+
}
63+
else {
64+
return this->openalpr->recognize (buffer);
65+
}
5266
}
5367

5468
bool isWorking () {
@@ -169,16 +183,28 @@ NAN_METHOD (IdentifyLicense)
169183

170184
// Settings
171185
char *path = get (info[0]);
172-
char *state = get (info[2]);
173-
char *prewarp = get (info[3]);
174-
bool detectRegion = info[4]->BooleanValue ();
175-
Nan::Callback *callback = new Nan::Callback (info[1].As<Function>());
186+
char *state = get (info[1]);
187+
char *prewarp = get (info[2]);
188+
bool detectRegion = info[3]->BooleanValue ();
189+
Local<Array> regionsArray = info[4].As<Array> ();
190+
Nan::Callback *callback = new Nan::Callback (info[5].As<Function>());
191+
192+
std::vector<alpr::AlprRegionOfInterest> regions;
193+
for (int i = 0; i < regionsArray->Length (); i++) {
194+
Local<Array> regionValues = Local<Array>::Cast (regionsArray->Get (i));
195+
int x = regionValues->Get (0)->Uint32Value ();
196+
int y = regionValues->Get (1)->Uint32Value ();
197+
int width = regionValues->Get (2)->Uint32Value ();
198+
int height = regionValues->Get (3)->Uint32Value ();
199+
regions.push_back (alpr::AlprRegionOfInterest (x, y, width, height));
200+
}
176201

177202
LPRQueueItem *item = new LPRQueueItem ();
178203
item->path = path;
179204
item->state = state;
180205
item->prewarp = prewarp;
181206
item->detectRegion = detectRegion;
207+
item->regions = regions;
182208
item->callback = callback;
183209

184210
for (auto &i : instances) {

src/openalpr.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,21 @@ function OpenALPR () {
124124
options = {};
125125
}
126126

127-
return nativeLPR.IdentifyLicense (path, function (error, output) {
127+
var regions = [];
128+
if (options.regions && options.regions.length) {
129+
for (var r in options.regions) {
130+
var region = options.regions[r];
131+
if (!region.x || !region.y || !region.width || !region.height) {
132+
continue;
133+
}
134+
135+
regions.push ([region.x, region.y, region.width, region.height]);
136+
}
137+
}
138+
139+
return nativeLPR.IdentifyLicense (path, options.state || "", options.prewarp || "", options.detectRegion || false, regions, function (error, output) {
128140
callback (error, JSON.parse (output));
129-
}, options.state || "", options.prewarp || "", options.detectRegion || false);
141+
});
130142
}
131143

132144
/**

0 commit comments

Comments
 (0)