diff --git a/src/openalpr.js b/src/openalpr.js index 106e0f2..72f876f 100644 --- a/src/openalpr.js +++ b/src/openalpr.js @@ -1,28 +1,22 @@ -var nativeBio = {} - fs = require ("fs") - child_process = require ("child_process") - os = require ("os") - path = require ("path"), - native_lib_path = path.join (__dirname, '../build/Release/node_openalpr.node'); - -if(fs.existsSync (native_lib_path)) { - nativeLPR = require (native_lib_path); -} -else { - nativeLPR = require (path.join (__dirname, "../release/" + process.platform + "_" + process.arch + "/node_openalpr.node")); -} +var nativeBio = {}, + fs = require("fs"), + child_process = require("child_process"), + os = require("os"), + path = require("path"), + native_lib_path = path.join(__dirname, '../build/Release/node_openalpr.node'), + nativeLPR = fs.existsSync(native_lib_path) ? require(native_lib_path) : require(path.join(__dirname, "../release/" + process.platform + "_" + process.arch + "/node_openalpr.node")); /** * OpenALPR NodeJS wrapper class */ -function OpenALPR () { - var object = this; - var initialized = false; - var started = false; - var callback; - var loop; - var in_loop = false; - +function OpenALPR() { + var object = this, + initialized = false, + started = false, + callback, + loop, + in_loop = false; + /** * Start the OpenALPR process * @param int count Amount of OpenALPR threads to startup [Default: System CPU count] @@ -30,73 +24,73 @@ function OpenALPR () { */ this.Start = function (config, runtime, count, start_queue) { if (initialized) { - return true; - } - - if (!config && os.platform () === "win32") { - config = path.join (__dirname, "../openalpr.conf"); - } - - if (!runtime && os.platform () === "win32") { - runtime = path.join (__dirname, "../openalpr_runtime/"); - } - - var cpu_count = os.cpus ().length; - - initialized = nativeLPR.Start (config || "", runtime || "", count || cpu_count, function (reason) { - console.log ("OpenALPR Stopped " + reason); + return true; + } + + if (!config && os.platform() === "win32") { + config = path.join(__dirname, "../openalpr.conf"); + } + + if (!runtime && os.platform() === "win32") { + runtime = path.join(__dirname, "../openalpr_runtime/"); + } + + var cpu_count = os.cpus().length; + + initialized = nativeLPR.Start(config || "", runtime || "", count || cpu_count, function (reason) { + console.log("OpenALPR Stopped " + reason); }); - + if (start_queue !== false) { - this.StartQueue (); - } + this.StartQueue(); + } return initialized; - } - + }; + /** * Stops OpenALPR and kills off all queued up pictures */ this.Stop = function () { - this.StopQueue (); - return nativeLPR.Stop (); - } - + this.StopQueue(); + return nativeLPR.Stop(); + }; + /** * Starts the queue to update 30 times a second */ this.StartQueue = function () { if (!initialized || typeof loop === "object") { - return; - } - - loop = setInterval (this.queueLoop, 32); - } - + return; + } + + loop = setInterval(this.queueLoop, 32); + }; + /** * Stops the queue from auto-updating */ this.StopQueue = function () { if (typeof loop !== "object") { - return; - } - - clearInterval (loop); + return; + } + + clearInterval(loop); loop = null; - } - + }; + /** * Get the version of OpenALPR detected * @return string Version (x.x.x) */ this.GetVersion = function () { if (!initialized) { - throw "OpenALPR Not Initialized"; - } - - return nativeLPR.GetVersion (); - } - + throw "OpenALPR Not Initialized"; + } + + return nativeLPR.GetVersion(); + }; + /** * Begin the process of identifying a license plate * @param string path Full path to image @@ -106,53 +100,52 @@ function OpenALPR () { */ this.IdentifyLicense = function (path, options, callback) { if (!initialized) { - throw "OpenALPR Not Initialized"; - } - + throw "OpenALPR Not Initialized"; + } + if (typeof callback !== "function" && typeof options !== "function") { throw "Callback method required"; - } - else if (typeof options === "function") { + } else if (typeof options === "function") { callback = options; - } - - if (!fs.existsSync (path)) { - throw "File does exist"; - } - + } + + if (!fs.existsSync(path)) { + throw "File does exist"; + } + if (typeof options === "undefined" || typeof options === "function" || !options) { - options = {}; - } - + options = {}; + } + 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]); + 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)); + + return nativeLPR.IdentifyLicense(path, options.state || "", options.prewarp || "", options.detectRegion || false, regions, function (error, output) { + callback(error, JSON.parse(output)); }); - } - + }; + /** * Checks the queue to see if any instances are free to begin work */ this.queueLoop = function () { if (in_loop) { - return; - } - + return; + } + in_loop = true; - nativeLPR.CheckQueue (); + nativeLPR.CheckQueue(); in_loop = false; - } + }; } -module.exports = new OpenALPR (); +module.exports = new OpenALPR();