diff --git a/plugins/tensorflow-coral/README.md b/plugins/tensorflow-coral/README.md index d16e0662..1398b0ce 100644 --- a/plugins/tensorflow-coral/README.md +++ b/plugins/tensorflow-coral/README.md @@ -29,7 +29,7 @@ IF YOU DON'T HAVE INSTALLED CORAL DEPENDENCIES BEFORE, YOU NEED TO PLUG OUT AND Start the plugin. ``` -pm2 start shinobi-tensorflow.js +pm2 start shinobi-tensorflow-coral.js ``` Doing this will reveal options in the monitor configuration. Shinobi does not need to be restarted when a plugin is initiated or stopped. diff --git a/plugins/tensorflow-coral/package.json b/plugins/tensorflow-coral/package.json index e79dbef6..ccb28c80 100644 --- a/plugins/tensorflow-coral/package.json +++ b/plugins/tensorflow-coral/package.json @@ -3,7 +3,7 @@ "author": "Shinob Systems, Moinul Alam | dermodmaster, Levent Koch", "version": "1.0.0", "description": "Object Detection plugin based on tensorflow using Google Coral USB Accelerator", - "main": "shinobi-tensorflow.js", + "main": "shinobi-tensorflow-coral.js", "dependencies": { "dotenv": "^8.2.0", "express": "^4.16.2", @@ -12,7 +12,7 @@ "socket.io-client": "^1.7.4" }, "devDependencies": {}, - "bin": "shinobi-tensorflow.js", + "bin": "shinobi-tensorflow-coral.js", "scripts": { "package": "pkg package.json -t linux,macos,win --out-path dist", "package-x64": "pkg package.json -t linux-x64,macos-x64,win-x64 --out-path dist/x64", diff --git a/plugins/tensorflow-coral/shinobi-tensorflow-coral.js b/plugins/tensorflow-coral/shinobi-tensorflow-coral.js new file mode 100644 index 00000000..f7d5d8b9 --- /dev/null +++ b/plugins/tensorflow-coral/shinobi-tensorflow-coral.js @@ -0,0 +1,146 @@ +// +// Shinobi - Tensorflow Plugin +// Copyright (C) 2016-2025 Moe Alam, moeiscool +// Copyright (C) 2020 Levent Koch, dermodmaster +// +// # Donate +// +// If you like what I am doing here and want me to continue please consider donating :) +// PayPal : paypal@m03.ca +// +// Base Init >> +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) { + console.log(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.') + } +} + + +var ready = false; +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()); + } + 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; + } + } + }) + }) + return theChild +} + + + + +// Base Init />> +child = respawn(); + +const emptyDataObject = { data: [], type: undefined, time: 0 }; + +async function process(buffer, type) { + const startTime = new Date(); + if (!ready) { + return emptyDataObject; + } + ready = false; + child.stdin.write(buffer.toString('base64') + '\n'); + + var message = null; + await new Promise(resolve => { + child.stdout.once('data', (data) => { + var rawString = data.toString('utf8').split("\n")[0]; + try { + message = JSON.parse(rawString) + } + catch (e) { + message = { data: [] }; + } + resolve(); + }); + }) + const data = message.data; + ready = true; + return { + data: data, + type: type, + time: new Date() - startTime + } +} + + +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]) { + var mats = [] + results.forEach(function (v) { + mats.push({ + x: v.bbox[0], + y: v.bbox[1], + width: v.bbox[2], + height: v.bbox[3], + tag: v.class, + confidence: v.score, + }) + }) + 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) + tx({ + 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 + } + }) + } + callback() + }) +} diff --git a/plugins/tensorflow-coral/shinobi-tensorflow.js b/plugins/tensorflow-coral/shinobi-tensorflow.js index f7d5d8b9..904fd713 100644 --- a/plugins/tensorflow-coral/shinobi-tensorflow.js +++ b/plugins/tensorflow-coral/shinobi-tensorflow.js @@ -1,146 +1 @@ -// -// Shinobi - Tensorflow Plugin -// Copyright (C) 2016-2025 Moe Alam, moeiscool -// Copyright (C) 2020 Levent Koch, dermodmaster -// -// # Donate -// -// If you like what I am doing here and want me to continue please consider donating :) -// PayPal : paypal@m03.ca -// -// Base Init >> -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) { - console.log(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.') - } -} - - -var ready = false; -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()); - } - 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; - } - } - }) - }) - return theChild -} - - - - -// Base Init />> -child = respawn(); - -const emptyDataObject = { data: [], type: undefined, time: 0 }; - -async function process(buffer, type) { - const startTime = new Date(); - if (!ready) { - return emptyDataObject; - } - ready = false; - child.stdin.write(buffer.toString('base64') + '\n'); - - var message = null; - await new Promise(resolve => { - child.stdout.once('data', (data) => { - var rawString = data.toString('utf8').split("\n")[0]; - try { - message = JSON.parse(rawString) - } - catch (e) { - message = { data: [] }; - } - resolve(); - }); - }) - const data = message.data; - ready = true; - return { - data: data, - type: type, - time: new Date() - startTime - } -} - - -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]) { - var mats = [] - results.forEach(function (v) { - mats.push({ - x: v.bbox[0], - y: v.bbox[1], - width: v.bbox[2], - height: v.bbox[3], - tag: v.class, - confidence: v.score, - }) - }) - 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) - tx({ - 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 - } - }) - } - callback() - }) -} +require('./shinobi-tensorflow-coral.js')