Skip to content

Commit

Permalink
Added the ability to pass in regions for the image
Browse files Browse the repository at this point in the history
  • Loading branch information
KJ Lawrence committed Aug 25, 2015
1 parent 52e38fd commit 2eb9958
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ node-openalpr

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

Version: 1.0.7 - Released August 25th, 2015

```
Changelog:
1.0.7 - Added the capability to specify regions
1.0.6 - Slowed down the event loop to 30 times per second
1.0.1:5 - Documentation changes
1.0.0 - Initial release
```

# Installation and Example

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

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-openalpr",
"description": "Node.js OpenALPR Bindings",
"version": "1.0.6",
"version": "1.0.7",
"license": "AGPL",
"keywords": [
"bindings",
Expand All @@ -22,7 +22,6 @@
"email": "[email protected]"
}
],

"repository": {
"type": "git",
"url": "git://github.com/netPark/node-openalpr.git"
Expand Down
Binary file modified release/linux_x64/node_openalpr.node
Binary file not shown.
Binary file modified release/win32_x64/node_openalpr.node
Binary file not shown.
38 changes: 32 additions & 6 deletions src/node_openalpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include <alpr.h>
#include <config.h>
#include <string.h>
#include <fstream>
#include <list>
#include <vector>

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

class LPRQueueItem
{
public:
public:
char *path;
char *state;
std::string prewarp;
bool detectRegion = false;
std::vector <alpr::AlprRegionOfInterest> regions;
Nan::Callback *callback;
};

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

return this->openalpr->recognize (queueItem->path);
std::ifstream ifs (queueItem->path, std::ios::binary|std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg ();
std::vector<char> buffer(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(&buffer[0], pos);

if (queueItem->regions.size ()) {
return this->openalpr->recognize (buffer, queueItem->regions);
}
else {
return this->openalpr->recognize (buffer);
}
}

bool isWorking () {
Expand Down Expand Up @@ -169,16 +183,28 @@ NAN_METHOD (IdentifyLicense)

// Settings
char *path = get (info[0]);
char *state = get (info[2]);
char *prewarp = get (info[3]);
bool detectRegion = info[4]->BooleanValue ();
Nan::Callback *callback = new Nan::Callback (info[1].As<Function>());
char *state = get (info[1]);
char *prewarp = get (info[2]);
bool detectRegion = info[3]->BooleanValue ();
Local<Array> regionsArray = info[4].As<Array> ();
Nan::Callback *callback = new Nan::Callback (info[5].As<Function>());

std::vector<alpr::AlprRegionOfInterest> regions;
for (int i = 0; i < regionsArray->Length (); i++) {
Local<Array> regionValues = Local<Array>::Cast (regionsArray->Get (i));
int x = regionValues->Get (0)->Uint32Value ();
int y = regionValues->Get (1)->Uint32Value ();
int width = regionValues->Get (2)->Uint32Value ();
int height = regionValues->Get (3)->Uint32Value ();
regions.push_back (alpr::AlprRegionOfInterest (x, y, width, height));
}

LPRQueueItem *item = new LPRQueueItem ();
item->path = path;
item->state = state;
item->prewarp = prewarp;
item->detectRegion = detectRegion;
item->regions = regions;
item->callback = callback;

for (auto &i : instances) {
Expand Down
16 changes: 14 additions & 2 deletions src/openalpr.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,21 @@ function OpenALPR () {
options = {};
}

return nativeLPR.IdentifyLicense (path, function (error, output) {
var regions = [];
if (options.regions && options.regions.length) {
for (var r in options.regions) {
var region = options.regions[r];
if (!region.x || !region.y || !region.width || !region.height) {
continue;
}

regions.push ([region.x, region.y, region.width, region.height]);
}
}

return nativeLPR.IdentifyLicense (path, options.state || "", options.prewarp || "", options.detectRegion || false, regions, function (error, output) {
callback (error, JSON.parse (output));
}, options.state || "", options.prewarp || "", options.detectRegion || false);
});
}

/**
Expand Down

0 comments on commit 2eb9958

Please sign in to comment.