-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·116 lines (96 loc) · 3.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const path = require('path');
const os = require('os');
const joi = require('@hapi/joi');
const supos = { linux: 'linux', win32: 'windows' };
const osname = supos[os.platform()];
let scheme = joi.object({
image: joi.string().required(),
c: joi.string().min(2).max(4),
p: joi.string().min(2).max(4),
a: joi.array().items(joi.string())
});
let executor = { };
executor.linux = function (image, { c, p, a }) {
let options = {
cwd: path.join(__dirname, 'bin', osname),
env: {
LD_LIBRARY_PATH: path.join(__dirname, 'bin', osname, 'lib')
}
}
let args = []
let cpath = path.join(__dirname, 'bin', osname, 'share', 'openalpr', 'config', 'openalpr.defaults.conf');
let bpath = path.join(__dirname, 'bin', osname, 'bin', 'alpr');
args.push(`"${bpath}"`);
args.push('-c', c || 'eu');
args.push('--config', `"${cpath}"`);
args.push('-j');
if(p) args.push('-p', p);
if(Array.isArray(a)) args.push(...a);
args.push(`"${image}"`);
return exec(args.join(' '), options);
}
executor.windows = function (image, { c, p, a }) {
let options = {
cwd: path.join(__dirname, 'bin', osname)
}
let args = []
let cpath = path.join(__dirname, 'bin', osname, 'openalpr.conf');
let bpath = path.join(__dirname, 'bin', osname, 'alpr.exe');
args.push(`"${bpath}"`);
args.push('-c', c || 'eu');
args.push('--config', `"${cpath}"`);
args.push('-j');
if (p) args.push('-p', p);
if (Array.isArray(a)) args.push(...a);
args.push(`"${image}"`);
return exec(args.join(' '), options);
}
async function process(image, { c, p, a}){
if (!(os.platform() in supos)) throw new Error('unsupported os');
if (os.arch() !== 'x64') throw new Error('unsupported arch');
if (image.slice(0, 1) === '.') image = path.resolve(module.parent.path, image);
try {
let valid = await scheme.validateAsync({ image, c, p, a });
let { stdout, stderr } = await executor[osname](valid.image, { c: valid.c, p: valid.p, a: valid.a });
if (stderr) throw new Error(stderr);
return JSON.parse(stdout);
} catch(err) {
throw new Error(err.message)
}
}
function OpenALPR(cmax = 2){
if(!new.target) throw new Error('please use NEW');
let queue = [];
let processing = [];
let sync = () => {
if (processing.length < cmax) {
let next = queue.shift();
if (next) {
let _then = x => {
next.resolve(x);
processing.splice(processing.findIndex(p => p === next), 1);
sync();
}
let _catch = x => {
next.reject(x);
processing.splice(processing.findIndex(p => p === next), 1);
sync();
}
processing.push(next);
process(...next.params).then(_then).catch(_catch);
}
}
}
this.getQueueLength = () => queue.length;
this.getProcessingLength = () => processing.length;
this.recognize = (...params) => {
return new Promise((resolve, reject) => {
queue.push({ params, resolve, reject });
sync();
})
}
}
module.exports = OpenALPR;