DeepStack Object Detection Plugin by Elad Bar

At Elad's request, here it is. The official distribution for the deepstack-object plugin.

https://hub.shinobi.video/articles/view/PcBtEgGuWuEL529
merge-requests/74/merge
Moe 2021-05-19 11:33:51 -07:00
parent d219b0979f
commit 8bf8a67910
7 changed files with 3998 additions and 0 deletions

4
plugins/deepstack-object/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
conf.json
dist
models
node_modules

View File

@ -0,0 +1,19 @@
#!/bin/bash
DIR=$(dirname $0)
echo "Removing existing Node.js modules..."
rm -rf $DIR/node_modules
nonInteractiveFlag=false
if [ ! -e "$DIR/conf.json" ]; then
dontCreateKeyFlag=false
echo "Creating conf.json"
sudo cp $DIR/conf.sample.json $DIR/conf.json
else
echo "conf.json already exists..."
fi
if [ "$dontCreateKeyFlag" = false ]; then
echo "Adding Random Plugin Key to Main Configuration"
node $DIR/../../tools/modifyConfigurationForPlugin.js deepstack-object key=$(head -c 64 < /dev/urandom | sha256sum | awk '{print substr($1,1,60)}')
fi

View File

@ -0,0 +1,25 @@
# Shinobi Video plugin for DeepStack Object Detection
### How to Install DeepStack Object Detection on GPU
> [This document has been rewritten over on ShinobiHub Articles.](https://hub.shinobi.video/articles/view/PcBtEgGuWuEL529)
# Additional Information
Docker - [Get docker](https://docs.docker.com/get-docker/)
DeepStack - [Getting started](https://docs.deepstack.cc/getting-started/index.html#setting-up-deepstack)
Run DeepStack CPU docker image:
```
sudo docker run -e VISION-FACE=True -e VISION-DETECTION=True -v localstorage:/datastore -p 80:5000 deepquestai/deepstack
```
GPU [installation guide](https://docs.deepstack.cc/using-deepstack-with-nvidia-gpus/#step-1-install-docker)
#### More installation options
[Windows (CPU / GPU support)](https://docs.deepstack.cc/windows/index.html)
[nVidia Jetson](https://docs.deepstack.cc/nvidia-jetson/index.html#using-deepstack-with-nvidia-jetson)
[Raspberry PI](https://docs.deepstack.cc/raspberry-pi/index.html#using-deepstack-on-raspberry-pi-alpha)

View File

@ -0,0 +1,16 @@
{
"plug": "DeepStack-Object",
"host": "localhost",
"tfjsBuild": "cpu",
"port": 8080,
"hostPort": 58084,
"key": "1234567890",
"mode": "client",
"type": "detector",
"deepStack": {
"host": "127.0.0.1",
"port": 5000,
"isSSL": false,
"apiKey": "api key as defined in DeepStack"
}
}

3737
plugins/deepstack-object/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,26 @@
{
"name": "shinobi-deepstack-object",
"author": "Elad Bar",
"version": "1.0.0",
"description": "Object Detection plugin for DeepStack",
"main": "shinobi-deepstack-object.js",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.16.2",
"moment": "^2.19.2",
"socket.io": "^2.0.4",
"socket.io-client": "^1.7.4"
},
"devDependencies": {},
"bin": "shinobi-deepstack-object.js",
"pkg": {
"targets": [
"node12"
],
"scripts": [
"../pluginBase.js"
],
"assets": []
},
"disabled": false
}

View File

@ -0,0 +1,171 @@
//
// Shinobi - Tensorflow Plugin
// Copyright (C) 2016-2025 Elad Bar, Moe Alam
//
// Base Init >>
var fs = require('fs');
var config = require('./conf.json')
const request = require("request")
var s
const {
workerData
} = require('worker_threads');
if(workerData && workerData.ok === true){
try{
s = require('../pluginWorkerBase.js')(__dirname,config)
}catch(err){
console.log(err)
try{
s = require('./pluginWorkerBase.js')(__dirname,config)
}catch(err){
console.log(err)
return console.log(config.plug,'WORKER : Plugin start has failed. pluginBase.js was not found.')
}
}
}else{
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.')
}
}
const {
haltMessage,
checkStartTime,
setStartTime,
} = require('../pluginCheck.js')
if(!checkStartTime()){
console.log(haltMessage,new Date())
s.disconnectWebSocket()
return
}
setStartTime()
}
// Base Init />>
var deepStackHost = config.deepStack["host"]
var deepStackPort = config.deepStack["port"]
var deepStackIsSSL = config.deepStack["isSSL"]
var deepStackApiKey = config.deepStack["apiKey"]
var deepStackProtocol = deepStackIsSSL ? "https" : "http"
var baseUrl = `${deepStackProtocol}://${deepStackHost}:${deepStackPort}/v1`
var objectDetectionUrl = `${baseUrl}/vision/detection`
s.detectObject = function(buffer,d,tx,frameLocation,callback){
var timeStart = new Date()
var detectStuff = async function(frame){
try{
image_stream = fs.createReadStream(frame)
const form = {
"image":image_stream
}
if(deepStackApiKey) {
form["api_key"] = deepStackApiKey
}
request.post({url:objectDetectionUrl, formData:form}, function(err,res,body){
const responseDate = new Date()
const responseTime = (responseDate.getTime() - timeStart.getTime());
const response = JSON.parse(body)
const success = response["success"]
const predictions = response["predictions"]
const mats = []
const detected = []
if(success) {
predictions.forEach(function(v){
const label = v["label"]
const confidence = v["confidence"]
const y_min = v["y_min"]
const x_min = v["x_min"]
const y_max = v["y_max"]
const x_max = v["x_max"]
const width = x_max - x_min
const height = y_max - y_min
detected.push(`${label}: ${confidence}`)
mats.push({
x: x_min,
y: y_min,
width: width,
height: height,
tag: label,
confidence: confidence,
})
})
}
if(detected.length > 0) {
detectedStr = detected.join(",")
}
const isObjectDetectionSeparate = d.mon.detector_pam === '1' && d.mon.detector_use_detect_object === '1'
const width = parseFloat(isObjectDetectionSeparate && d.mon.detector_scale_y_object ? d.mon.detector_scale_y_object : d.mon.detector_scale_y)
const 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: `DeepStack-Object`,
reason:'object',
matrices:mats,
imgHeight:width,
imgWidth:height,
time: responseTime
}
})
})
}catch(err){
console.log(err)
}
callback()
}
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);
}
})
}
}