Refactored shinobi-tensorflow.js (by moe and thellamafarm)

fix-non-showing-inputs
Levent K 2020-10-28 15:02:53 +00:00
parent 18b3ae1c45
commit db13c16260
1 changed files with 77 additions and 63 deletions

View File

@ -13,63 +13,77 @@ var fs = require('fs');
var config = require('./conf.json')
var dotenv = require('dotenv').config()
var s
try{
s = require('../pluginBase.js')(__dirname,config)
}catch(err){
try {
s = require('../pluginBase.js')(__dirname, config)
} catch (err) {
console.log(err)
try{
s = require('./pluginBase.js')(__dirname,config)
}catch(err){
try {
s = require('./pluginBase.js')(__dirname, config)
} catch (err) {
console.log(err)
return console.log(config.plug,'Plugin start has failed. pluginBase.js was not found.')
return console.log(config.plug, 'Plugin start has failed. pluginBase.js was not found.')
}
}
// Base Init />>
var spawn = require('child_process').spawn;
var child = spawn('python3', ['-u', 'detect_image.py']);
var ready = false;
var lastStatusLog = new Date();
child.on('close', function() {
child = spawn('python3', ['-u', 'detect_image.py']);
});
child.stdout.on('data', function (data) {
var rawString = data.toString('utf8');
if(new Date() - lastStatusLog > 5000){
lastStatusLog = new Date();
console.log(rawString, new Date());
}
var messages = rawString.split('\n')
messages.forEach((message) => {
if(message === "") return;
var obj = JSON.parse(message)
if (obj.type === "error") {
console.log("Script got error: "+message.data, new Date());
throw message.data;
const spawn = require('child_process').spawn;
var child = null
function respawn() {
console.log("respawned python",(new Date()))
const theChild = spawn('python3', ['-u', 'detect_image.py']);
var lastStatusLog = new Date();
theChild.on('exit', () => {
child = respawn();
});
theChild.stdout.on('data', function (data) {
var rawString = data.toString('utf8');
if (new Date() - lastStatusLog > 5000) {
lastStatusLog = new Date();
console.log(rawString, new Date());
}
if (obj.type === "info" && obj.data === "ready") {
console.log("set ready true")
ready = true;
} else {
if(obj.type !== "data" && obj.type !== "info"){
throw "Unexpected message: "+rawString;
var messages = rawString.split('\n')
messages.forEach((message) => {
if (message === "") return;
var obj = JSON.parse(message)
if (obj.type === "error") {
console.log("Script got error: " + message.data, new Date());
throw message.data;
}
}
if (obj.type === "info" && obj.data === "ready") {
console.log("set ready true")
ready = true;
} else {
if (obj.type !== "data" && obj.type !== "info") {
throw "Unexpected message: " + rawString;
}
}
})
})
})
const emptyDataObject = {data: [], type: undefined, time: 0};
return theChild
}
// Base Init />>
child = respawn();
const emptyDataObject = { data: [], type: undefined, time: 0 };
async function process(buffer, type) {
const startTime = new Date();
if(!ready){
if (!ready) {
return emptyDataObject;
}
ready = false;
child.stdin.write(buffer.toString('base64')+'\n');
child.stdin.write(buffer.toString('base64') + '\n');
var message = null;
await new Promise(resolve => {
child.stdout.once('data', (data) => {
@ -78,7 +92,7 @@ async function process(buffer, type) {
message = JSON.parse(rawString)
}
catch (e) {
message = {data: []};
message = { data: [] };
}
resolve();
});
@ -91,15 +105,15 @@ async function process(buffer, type) {
time: new Date() - startTime
}
}
s.detectObject = function(buffer,d,tx,frameLocation,callback){
process( buffer).then((resp)=>{
s.detectObject = function (buffer, d, tx, frameLocation, callback) {
process(buffer).then((resp) => {
var results = resp.data
//console.log(resp.time)
if(Array.isArray(results) && results[0]){
if (Array.isArray(results) && results[0]) {
var mats = []
results.forEach(function(v){
results.forEach(function (v) {
mats.push({
x: v.bbox[0],
y: v.bbox[1],
@ -110,19 +124,19 @@ s.detectObject = function(buffer,d,tx,frameLocation,callback){
})
})
var isObjectDetectionSeparate = d.mon.detector_pam === '1' && d.mon.detector_use_detect_object === '1'
var width = parseFloat(isObjectDetectionSeparate && d.mon.detector_scale_y_object ? d.mon.detector_scale_y_object : d.mon.detector_scale_y)
var height = parseFloat(isObjectDetectionSeparate && d.mon.detector_scale_x_object ? d.mon.detector_scale_x_object : d.mon.detector_scale_x)
var width = parseFloat(isObjectDetectionSeparate && d.mon.detector_scale_y_object ? d.mon.detector_scale_y_object : d.mon.detector_scale_y)
var height = parseFloat(isObjectDetectionSeparate && d.mon.detector_scale_x_object ? d.mon.detector_scale_x_object : d.mon.detector_scale_x)
tx({
f:'trigger',
id:d.id,
ke:d.ke,
details:{
plug:config.plug,
name:'Tensorflow',
reason:'object',
matrices:mats,
imgHeight:width,
imgWidth:height,
f: 'trigger',
id: d.id,
ke: d.ke,
details: {
plug: config.plug,
name: 'Tensorflow',
reason: 'object',
matrices: mats,
imgHeight: width,
imgWidth: height,
time: resp.time
}
})