Update OpenALPR Plugin to use C++ base

- This will make it so the classifier is not loaded for every frame. It is loaded once and then frames are run against it while the plugin daemon is active.
pushover
Moe 2019-08-06 18:53:35 -07:00
parent 791a281f88
commit 48444b4ddf
3 changed files with 64 additions and 95 deletions

View File

@ -33,7 +33,10 @@ else
fi fi
echo "-----------------------------------" echo "-----------------------------------"
echo "Installing Modules.." echo "Installing Modules.."
npm install --unsafe-perm apt install node-pre-gyp -y
npm install nopt npmlog rimraf semver -g
npm install
npm uninstall nopt npmlog rimraf semver -g
echo "Finding and Fixing Module Vulnerabilities.." echo "Finding and Fixing Module Vulnerabilities.."
npm audit fix --force npm audit fix --force
echo "Shinobi - Do you want to start the plugin?" echo "Shinobi - Do you want to start the plugin?"

View File

@ -6,7 +6,8 @@
"dependencies": { "dependencies": {
"express": "^4.16.2", "express": "^4.16.2",
"moment": "^2.19.2", "moment": "^2.19.2",
"socket.io": "^2.0.4" "socket.io": "^2.0.4",
"node-openalpr-shinobi": "1.1.4"
}, },
"devDependencies": {}, "devDependencies": {},
"scripts": { "scripts": {

View File

@ -11,6 +11,10 @@
var fs = require('fs'); var fs = require('fs');
var config = require('./conf.json') var config = require('./conf.json')
var exec = require('child_process').exec; var exec = require('child_process').exec;
var openalpr = {
us: require ("node-openalpr-shinobi"),
eu: require ("node-openalpr-shinobi"),
};
var s var s
try{ try{
s = require('../pluginBase.js')(__dirname,config) s = require('../pluginBase.js')(__dirname,config)
@ -29,100 +33,61 @@ try{
if(config.alprConfig === undefined){ if(config.alprConfig === undefined){
config.alprConfig = __dirname + '/openalpr.conf' config.alprConfig = __dirname + '/openalpr.conf'
} }
// OpenALPR Init />> Object.keys(openalpr).forEach(function(region){
s.detectObject = function(buffer,d,tx,frameLocation){ openalpr[region].Start(config.alprConfig, null, null, true, region)
var detectStuff = function(frame){ })
try{ var convertResultsToMatrices = function(results){
exec('alpr -j --config '+config.alprConfig+' -c '+d.mon.detector_lisence_plate_country+' '+frame,{encoding:'utf8'},(err, scan, stderr) => { var mats = []
if(err){ var plates = []
s.systemLog(err); results.forEach(function(v){
}else{ v.candidates.forEach(function(g,n){
try{ if(v.candidates[n].matches_template){
try{ delete(v.candidates[n].matches_template)
if(scan.indexOf('Initialize OpenCL runtime...') > -1){
scan = scan.split('Initialize OpenCL runtime...')[1].trim()
}
scan = JSON.parse(scan.replace('--(!)Loaded CUDA classifier','').trim())
}catch(err){
if(!scan||!scan.results){
return s.systemLog(scan,err);
}
}
// console.log('scan',scan)
if(scan.results.length > 0){
scan.plates=[]
scan.mats=[]
scan.results.forEach(function(v){
v.candidates.forEach(function(g,n){
if(v.candidates[n].matches_template){
delete(v.candidates[n].matches_template)
}
})
scan.plates.push({
coordinates: v.coordinates,
candidates: v.candidates,
confidence: v.confidence,
plate: v.plate
})
var width = Math.sqrt( Math.pow(v.coordinates[1].x - v.coordinates[0].x, 2) + Math.pow(v.coordinates[1].y - v.coordinates[0].y, 2));
var height = Math.sqrt( Math.pow(v.coordinates[2].x - v.coordinates[1].x, 2) + Math.pow(v.coordinates[2].y - v.coordinates[1].y, 2))
scan.mats.push({
x: v.coordinates[0].x,
y: v.coordinates[0].y,
width: width,
height: height,
tag: v.plate
})
})
tx({
f: 'trigger',
id: d.id,
ke: d.ke,
details: {
plug: config.plug,
name: 'licensePlate',
reason: 'object',
matrices: scan.mats,
imgHeight: d.mon.detector_scale_y,
imgWidth: d.mon.detector_scale_x,
frame: d.base64
}
})
}
}catch(err){
s.systemLog(scan,err);
}
}
fs.unlink(frame,function(){
})
})
}catch(err){
console.log(err)
}
}
if(frameLocation){
detectStuff(frameLocation)
}else{
d.tmpFile=s.gid(5)+'.jpg'
if(!fs.existsSync(s.dir.streams)){
fs.mkdirSync(s.dir.streams);
}
d.dir=s.dir.streams+d.ke+'/'
if(!fs.existsSync(d.dir)){
fs.mkdirSync(d.dir);
}
d.dir=s.dir.streams+d.ke+'/'+d.id+'/'
if(!fs.existsSync(d.dir)){
fs.mkdirSync(d.dir);
}
fs.writeFile(d.dir+d.tmpFile,buffer,function(err){
if(err) return s.systemLog(err);
try{
detectStuff(d.dir+d.tmpFile)
}catch(error){
console.error('Catch: ' + error);
} }
}) })
plates.push({
coordinates: v.coordinates,
candidates: v.candidates,
confidence: v.confidence,
plate: v.plate
})
var width = Math.sqrt( Math.pow(v.coordinates[1].x - v.coordinates[0].x, 2) + Math.pow(v.coordinates[1].y - v.coordinates[0].y, 2));
var height = Math.sqrt( Math.pow(v.coordinates[2].x - v.coordinates[1].x, 2) + Math.pow(v.coordinates[2].y - v.coordinates[1].y, 2))
mats.push({
x: v.coordinates[0].x,
y: v.coordinates[0].y,
width: width,
height: height,
tag: v.plate
})
})
return mats
}
// OpenALPR Init />>
s.detectObject = function(buffer,d,tx,frameLocation){
try{
var region = d.mon.detector_lisence_plate_country || 'us'
openalpr[region].IdentifyLicense(buffer, {}, function (error, output){
var results = output.results
if(results.length > 0){
var matrices = convertResultsToMatrices(results)
tx({
f: 'trigger',
id: d.id,
ke: d.ke,
details: {
plug: config.plug,
name: 'licensePlate',
reason: 'object',
matrices: matrices,
imgHeight: d.mon.detector_scale_y,
imgWidth: d.mon.detector_scale_x,
frame: d.base64
}
})
}
})
}catch(err){
console.log(err)
} }
} }