Skip to content

Instantly share code, notes, and snippets.

@jrgcubano
Last active August 13, 2017 12:51
Show Gist options
  • Save jrgcubano/56e3204ecc661b09c16a to your computer and use it in GitHub Desktop.
Save jrgcubano/56e3204ecc661b09c16a to your computer and use it in GitHub Desktop.
Facial recognition and training with node js
// Starkflow comments + github project node js
// + https://github.com/jrgcubano/face-detection-node-opencv
// + http://blog.stevenedouard.com/stroll-node-facial-recognition-3rd-party-apis/
I believe that you're using the node-opencv library ? You will need some more steps. You have to train your opencv system which than allows you to use the method "predictSync" from FaceRecongizer().
The node-opencv library has a FaceRecognizer Object which you first initialize.
Initialize FaceRecognizer: var FaceRecognizer = new cv.FaceRecognizer();
You have to read all the images, create a specific array and train your FaceRecognizer with that. For my purpose, I'm saving each user in a DB and they get a unique ID which I'm using to create a specific subfolder and this is later used. Here is my code:
//Cold start training for opencv
var uploadDir = path.join(global.appRoot, "/uploads");
fs.readdir(uploadDir, function(err, files){
if(err) throw new Error(err);
if(files.length > 0){ //There are some user related image folders
files.forEach(function(subfolder, index, array){
if(subfolder != ".DS_Store" ){ //Issue with Mac, test on Linux-VM
//We are now iterating over each subfolder
var subFolderDir = path.join(uploadDir, "/"+subfolder);
var images = fs.readdirSync(subFolderDir);
//console.log(images);
images.forEach(function(image, index, array){//Get Matrix Objekt for each image to train OpenCV
if(image != ".DS_Store"){
var imageDir = path.join(subFolderDir, "/"+image);
cv.readImage(imageDir, function(err, im){
var channels = im.channels();
if(channels >=3){
var labelNumber = parseInt(subfolder); //Create labelnumber; Account-Id starts by 1, labels for openCV start with 0
cvImages.push(new Array(labelNumber,im)); //Add image to Array
}
});
}
});
}
});
if(cvImages.length > 3){
console.log("Training images (we have at least 3 images)", cvImages);
FaceRecognizer.trainSync(cvImages);
}else{
console.log("Not enough images uploaded yet", cvImages);
}
}else{
console.log("There are no images uploaded yet!");
}
});`
I'm sure, that you can optimize it but for a private project it's good enough.
After training your system, if you want to know the person on the image:
cv.readImage(fileDir, function(err, im){
if(err) res.send(err);
var whoisit = FaceRecognizer.predictSync(im);
console.log("Identified image", whoisit);
});
The "whoisit" object containts, in my case, the ID of the user and the "confidence" value, means how "sure" openCV is about the person on the image. Hope it helps.
@dunklesToast
Copy link

dunklesToast commented Aug 3, 2017

is it possible to save the trained images? So we dont have to train them every time we restart the software?


Update

Found it.
use

let FaceRecognizer = new cv.FaceRecognizer();
FaceRecognizer.load('./recog.xml');
FaceRecognizer.save('./recog.xml');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment