2020-11-03 19:41:17 +01:00
process . stderr . write = null ; // silly hack to stock tfjs logging too much to stderr
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
const tf = require ( '@tensorflow/tfjs-node' ) ;
const faceapi = require ( '../dist/face-api.node.js' ) ;
2020-12-02 20:34:43 +01:00
// if you have module installed, this would be
2020-11-03 19:41:17 +01:00
// const faceapi = require('@vladmandic/face-api');
// configuration options
const modelPathRoot = '../model/' ; // path to model folder that will be loaded using http
const imgSize = 512 ; // maximum image size in pixels
const minScore = 0.1 ; // minimum score
const maxResults = 5 ; // maximum number of results to return
const samples = [ 'sample (1).jpg' , 'sample (2).jpg' , 'sample (3).jpg' , 'sample (4).jpg' , 'sample (5).jpg' , 'sample (6).jpg' ] ; // sample images to be loaded using http
// helper function to pretty-print json object to string
function str ( json ) {
const text = json ? JSON . stringify ( json ) . replace ( /{|}|"|\[|\]/g , '' ) . replace ( /,/g , ', ' ) : '' ;
return text ;
}
// helper function to print strings to html document as a log
function log ( ... txt ) {
// eslint-disable-next-line no-console
console . log ( ... txt ) ;
}
async function image ( img ) {
const buffer = fs . readFileSync ( img ) ;
const decoded = tf . node . decodeImage ( buffer ) ;
const casted = decoded . toFloat ( ) ;
2020-11-03 19:50:14 +01:00
const result = casted . expandDims ( 0 ) ;
2020-11-03 19:41:17 +01:00
decoded . dispose ( ) ;
casted . dispose ( ) ;
2020-11-03 19:50:14 +01:00
return result ;
2020-11-03 19:41:17 +01:00
}
async function main ( ) {
// initialize tfjs
log ( 'FaceAPI Test' ) ;
2020-12-02 20:34:43 +01:00
await faceapi . tf . setBackend ( 'tensorflow' ) ; //Sets the backend (cpu, webgl, wasm, tensorflow, etc) responsible for creating tensors and executing operations on those tensors.
2020-11-03 19:41:17 +01:00
await faceapi . tf . enableProdMode ( ) ;
await faceapi . tf . ENV . set ( 'DEBUG' , false ) ;
2020-12-02 20:34:43 +01:00
await faceapi . tf . ready ( ) ; //Returns a promise that resolves when the currently selected backend (or the highest priority one) has initialized.
2020-11-03 19:41:17 +01:00
// check version
log ( ` Version: TensorFlow/JS ${ str ( faceapi . tf ? . version _core || '(not loaded)' ) } FaceAPI ${ str ( faceapi ? . version || '(not loaded)' ) } Backend: ${ str ( faceapi . tf ? . getBackend ( ) || '(not loaded)' ) } ` ) ;
log ( ` Flags: ${ JSON . stringify ( faceapi . tf . ENV . flags ) } ` ) ;
// load face-api models
log ( 'Loading FaceAPI models' ) ;
const modelPath = path . join ( _ _dirname , modelPathRoot ) ;
await faceapi . nets . ssdMobilenetv1 . loadFromDisk ( modelPath ) ;
await faceapi . nets . ageGenderNet . loadFromDisk ( modelPath ) ;
await faceapi . nets . faceLandmark68Net . loadFromDisk ( modelPath ) ;
await faceapi . nets . faceRecognitionNet . loadFromDisk ( modelPath ) ;
await faceapi . nets . faceExpressionNet . loadFromDisk ( modelPath ) ;
const optionsSSDMobileNet = new faceapi . SsdMobilenetv1Options ( { minConfidence : minScore , maxResults } ) ;
// check tf engine state
const engine = await faceapi . tf . engine ( ) ;
log ( ` TF Engine State: ${ str ( engine . state ) } ` ) ;
const dir = fs . readdirSync ( _ _dirname ) ;
for ( const img of dir ) {
if ( ! img . toLocaleLowerCase ( ) . endsWith ( '.jpg' ) ) continue ;
// load image
const tensor = await image ( path . join ( _ _dirname , img ) ) ;
// actual model execution
const result = await faceapi
. detectAllFaces ( tensor , optionsSSDMobileNet )
. withFaceLandmarks ( )
. withFaceExpressions ( )
. withFaceDescriptors ( )
. withAgeAndGender ( ) ;
2020-11-03 19:50:14 +01:00
log ( 'Image:' , img , 'Detected faces:' , result . length ) ;
2020-11-03 19:41:17 +01:00
// you can access entire result object
// console.log(result);
tensor . dispose ( ) ;
}
}
main ( ) ;