Update Face Recognition plugin, add CPU/GPU selection in Installer

build-default-monitor-config-from-definitions
Moe 2020-04-10 09:31:12 -07:00
parent 39b8337b1c
commit 273a38d9d2
3 changed files with 38 additions and 40 deletions

View File

@ -1,6 +1,5 @@
#!/bin/bash #!/bin/bash
DIR=`dirname $0` DIR=`dirname $0`
THE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
if [ -x "$(command -v apt)" ]; then if [ -x "$(command -v apt)" ]; then
sudo apt update -y sudo apt update -y
fi fi
@ -17,7 +16,7 @@ if [ -d "/usr/local/cuda" ]; then
echo "Do this if you installed NVIDIA Drivers, CUDA Toolkit, and CuDNN" echo "Do this if you installed NVIDIA Drivers, CUDA Toolkit, and CuDNN"
echo "(y)es or (N)o" echo "(y)es or (N)o"
read usecuda read usecuda
if [ "$usecuda" = "y" ] || [ "$usecuda" = "Y" ]; then if [ "$usecuda" = "y" ] || [ "$usecuda" = "Y" ] || [ "$usecuda" = "YES" ] || [ "$usecuda" = "yes" ] || [ "$usecuda" = "Yes" ]; then
INSTALL_WITH_GPU="1" INSTALL_WITH_GPU="1"
fi fi
fi fi
@ -62,14 +61,23 @@ if [ ! -e "./conf.json" ]; then
else else
echo "conf.json already exists..." echo "conf.json already exists..."
fi fi
if [ "$INSTALL_WITH_GPU" = "1" ]; then
echo "TensorFlow.js plugin will use GPU"
sed -i 's/"tfjsBuild":"cpu"/"tfjsBuild":"gpu"/g' conf.json
sed -i 's/"tfjsBuild":"gpuORcpu"/"tfjsBuild":"gpu"/g' conf.json
else
echo "TensorFlow.js plugin will use CPU"
sed -i 's/"tfjsBuild":"gpu"/"tfjsBuild":"cpu"/g' conf.json
sed -i 's/"tfjsBuild":"gpuORcpu"/"tfjsBuild":"cpu"/g' conf.json
fi
echo "-----------------------------------"
echo "Adding Random Plugin Key to Main Configuration"
node $DIR/../../tools/modifyConfigurationForPlugin.js face key=$(head -c 64 < /dev/urandom | sha256sum | awk '{print substr($1,1,60)}')
echo "-----------------------------------" echo "-----------------------------------"
echo "Updating Node Package Manager" echo "Updating Node Package Manager"
sudo npm install npm -g --unsafe-perm sudo npm install npm -g --unsafe-perm
echo "-----------------------------------" echo "-----------------------------------"echo "Getting node-gyp to build C++ modules"
echo "Adding Random Plugin Key to Main Configuration"
node $THE_DIR/../../tools/modifyConfigurationForPlugin.js face key=$(head -c 64 < /dev/urandom | sha256sum | awk '{print substr($1,1,60)}')
echo "-----------------------------------"
echo "Getting node-gyp to build C++ modules"
if [ ! -x "$(command -v node-gyp)" ]; then if [ ! -x "$(command -v node-gyp)" ]; then
# Check if Ubuntu # Check if Ubuntu
if [ -x "$(command -v apt)" ]; then if [ -x "$(command -v apt)" ]; then
@ -87,6 +95,7 @@ echo "https://github.com/justadudewhohacks/face-api.js"
sudo npm install --unsafe-perm --force sudo npm install --unsafe-perm --force
echo "Getting C++ module : @tensorflow/tfjs-node@0.1.21" echo "Getting C++ module : @tensorflow/tfjs-node@0.1.21"
echo "https://github.com/tensorflow/tfjs-node" echo "https://github.com/tensorflow/tfjs-node"
sudo npm install @tensorflow/tfjs-core@0.13.11 --unsafe-perm --force
sudo npm install @tensorflow/tfjs-layers@0.8.5 --unsafe-perm --force sudo npm install @tensorflow/tfjs-layers@0.8.5 --unsafe-perm --force
sudo npm install @tensorflow/tfjs-converter@0.6.7 --unsafe-perm --force sudo npm install @tensorflow/tfjs-converter@0.6.7 --unsafe-perm --force
if [ "$INSTALL_WITH_GPU" = "1" ]; then if [ "$INSTALL_WITH_GPU" = "1" ]; then
@ -97,6 +106,7 @@ else
sudo npm install @tensorflow/tfjs-node@0.1.21 --unsafe-perm --force sudo npm install @tensorflow/tfjs-node@0.1.21 --unsafe-perm --force
fi fi
sudo npm audit fix --force sudo npm audit fix --force
npm rebuild --unsafe-perm --force
echo "-----------------------------------" echo "-----------------------------------"
echo "Start the plugin with pm2 like so :" echo "Start the plugin with pm2 like so :"
echo "pm2 start shinobi-face.js" echo "pm2 start shinobi-face.js"

View File

@ -1,5 +1,6 @@
{ {
"plug":"Face", "plug":"Face",
"tfjsBuild":"cpu",
"host":"localhost", "host":"localhost",
"port":8080, "port":8080,
"key":"Face123123", "key":"Face123123",

View File

@ -25,10 +25,24 @@ try{
// Base Init />> // Base Init />>
// Face - Face Recognition Init >> // Face - Face Recognition Init >>
var weightLocation = __dirname + '/weights' var weightLocation = __dirname + '/weights'
const tf = require('@tensorflow/tfjs') const canvas = require('canvas')
canvas = require('canvas') var tfjsSuffix = ''
switch(config.tfjsBuild){
case'gpu':
tfjsSuffix = '-gpu'
break;
case'cpu':
break;
default:
try{
require(`@tensorflow/tfjs-node`)
}catch(err){
console.log(err)
}
break;
}
var tf = require(`@tensorflow/tfjs-node${tfjsSuffix}`)
faceapi = require('face-api.js') faceapi = require('face-api.js')
require('@tensorflow/tfjs-node-gpu')
const { createCanvas, Image, ImageData, Canvas } = canvas const { createCanvas, Image, ImageData, Canvas } = canvas
faceapi.env.monkeyPatch({ Canvas, Image, ImageData }) faceapi.env.monkeyPatch({ Canvas, Image, ImageData })
@ -37,48 +51,21 @@ s.monitorLock = {}
// SsdMobilenetv1Options // SsdMobilenetv1Options
const minConfidence = 0.5 const minConfidence = 0.5
// TinyFaceDetectorOptions
const inputSize = 384
const scoreThreshold = 0.5
// MtcnnOptions
const minFaceSize = 50
const scaleFactor = 0.8
function getFaceDetectorOptions(net) {
return net === faceapi.nets.ssdMobilenetv1
? new faceapi.SsdMobilenetv1Options({ minConfidence })
: (net === faceapi.nets.tinyFaceDetector
? new faceapi.TinyFaceDetectorOptions({ inputSize, scoreThreshold })
: new faceapi.MtcnnOptions({ minFaceSize, scaleFactor })
)
}
var addAwaitStatements = async function(){ var addAwaitStatements = async function(){
await faceapi.nets.ssdMobilenetv1.loadFromDisk(weightLocation) await faceapi.nets.ssdMobilenetv1.loadFromDisk(weightLocation)
// faceapi.nets.tinyFaceDetector.loadFromDisk(weightLocation)
await faceapi.nets.faceLandmark68Net.loadFromDisk(weightLocation) await faceapi.nets.faceLandmark68Net.loadFromDisk(weightLocation)
await faceapi.nets.faceRecognitionNet.loadFromDisk(weightLocation) await faceapi.nets.faceRecognitionNet.loadFromDisk(weightLocation)
const faceDetectionNet = faceapi.nets.ssdMobilenetv1 const faceDetectionNet = faceapi.nets.ssdMobilenetv1
// const faceDetectionNet = faceapi.nets.tinyFaceDetector var faceDetectionOptions = new faceapi.SsdMobilenetv1Options({ minConfidence });
// const faceDetectionNet = faceapi.nets.mtcnn
var faceDetectionOptions = getFaceDetectorOptions(faceDetectionNet)
if(!fs.existsSync('./faces')){ if(!fs.existsSync('./faces')){
fs.mkdirSync('./faces'); fs.mkdirSync('./faces');
} }
var faces = fs.readdirSync('./faces') var faces = fs.readdirSync('./faces')
const labeledDescriptors = [ const labeledDescriptors = []
// new faceapi.LabeledFaceDescriptors(
// 'obama',
// [descriptorObama1, descriptorObama2]
// ),
// new faceapi.LabeledFaceDescriptors(
// 'trump',
// [descriptorTrump]
// )
]
var faceMatcher var faceMatcher
var facesLoaded = 0 var facesLoaded = 0
var startDetecting = function(){ var startDetecting = function(){
console.log('Ready to Detect Faces')
s.detectObject = function(buffer,d,tx,frameLocation){ s.detectObject = function(buffer,d,tx,frameLocation){
var detectStuff = function(frameBuffer,callback){ var detectStuff = function(frameBuffer,callback){
try{ try{