diff --git a/plugins/face/INSTALL.sh b/plugins/face/INSTALL.sh index 88165bfe..9b6a3bd4 100644 --- a/plugins/face/INSTALL.sh +++ b/plugins/face/INSTALL.sh @@ -1,6 +1,5 @@ #!/bin/bash DIR=`dirname $0` -THE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" if [ -x "$(command -v apt)" ]; then sudo apt update -y fi @@ -17,7 +16,7 @@ if [ -d "/usr/local/cuda" ]; then echo "Do this if you installed NVIDIA Drivers, CUDA Toolkit, and CuDNN" echo "(y)es or (N)o" read usecuda - if [ "$usecuda" = "y" ] || [ "$usecuda" = "Y" ]; then + if [ "$usecuda" = "y" ] || [ "$usecuda" = "Y" ] || [ "$usecuda" = "YES" ] || [ "$usecuda" = "yes" ] || [ "$usecuda" = "Yes" ]; then INSTALL_WITH_GPU="1" fi fi @@ -62,14 +61,23 @@ if [ ! -e "./conf.json" ]; then else echo "conf.json already exists..." 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 "Updating Node Package Manager" sudo npm install npm -g --unsafe-perm -echo "-----------------------------------" -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" +echo "-----------------------------------"echo "Getting node-gyp to build C++ modules" if [ ! -x "$(command -v node-gyp)" ]; then # Check if Ubuntu if [ -x "$(command -v apt)" ]; then @@ -87,6 +95,7 @@ echo "https://github.com/justadudewhohacks/face-api.js" sudo npm install --unsafe-perm --force echo "Getting C++ module : @tensorflow/tfjs-node@0.1.21" 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-converter@0.6.7 --unsafe-perm --force if [ "$INSTALL_WITH_GPU" = "1" ]; then @@ -97,6 +106,7 @@ else sudo npm install @tensorflow/tfjs-node@0.1.21 --unsafe-perm --force fi sudo npm audit fix --force +npm rebuild --unsafe-perm --force echo "-----------------------------------" echo "Start the plugin with pm2 like so :" echo "pm2 start shinobi-face.js" diff --git a/plugins/face/conf.sample.json b/plugins/face/conf.sample.json index dbe923d5..f19c3e5a 100644 --- a/plugins/face/conf.sample.json +++ b/plugins/face/conf.sample.json @@ -1,5 +1,6 @@ { "plug":"Face", + "tfjsBuild":"cpu", "host":"localhost", "port":8080, "key":"Face123123", diff --git a/plugins/face/shinobi-face.js b/plugins/face/shinobi-face.js index 2d5e9147..5d9f5a03 100644 --- a/plugins/face/shinobi-face.js +++ b/plugins/face/shinobi-face.js @@ -25,10 +25,24 @@ try{ // Base Init />> // Face - Face Recognition Init >> var weightLocation = __dirname + '/weights' -const tf = require('@tensorflow/tfjs') -canvas = require('canvas') +const 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') -require('@tensorflow/tfjs-node-gpu') const { createCanvas, Image, ImageData, Canvas } = canvas faceapi.env.monkeyPatch({ Canvas, Image, ImageData }) @@ -37,48 +51,21 @@ s.monitorLock = {} // SsdMobilenetv1Options 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(){ await faceapi.nets.ssdMobilenetv1.loadFromDisk(weightLocation) - // faceapi.nets.tinyFaceDetector.loadFromDisk(weightLocation) await faceapi.nets.faceLandmark68Net.loadFromDisk(weightLocation) await faceapi.nets.faceRecognitionNet.loadFromDisk(weightLocation) const faceDetectionNet = faceapi.nets.ssdMobilenetv1 - // const faceDetectionNet = faceapi.nets.tinyFaceDetector - // const faceDetectionNet = faceapi.nets.mtcnn - var faceDetectionOptions = getFaceDetectorOptions(faceDetectionNet) + var faceDetectionOptions = new faceapi.SsdMobilenetv1Options({ minConfidence }); if(!fs.existsSync('./faces')){ fs.mkdirSync('./faces'); } var faces = fs.readdirSync('./faces') - const labeledDescriptors = [ - // new faceapi.LabeledFaceDescriptors( - // 'obama', - // [descriptorObama1, descriptorObama2] - // ), - // new faceapi.LabeledFaceDescriptors( - // 'trump', - // [descriptorTrump] - // ) - ] + const labeledDescriptors = [] var faceMatcher var facesLoaded = 0 var startDetecting = function(){ + console.log('Ready to Detect Faces') s.detectObject = function(buffer,d,tx,frameLocation){ var detectStuff = function(frameBuffer,callback){ try{