Q:

use opencv and node to compare 2 faces for similarity

I have openCV and nodejs running, and my goal is to make a program that takes a picture of a face when it sees it (did that already), and then compare that face with various faces to a database to see if it's a match. I would know who's face is who in the db, so then basically I would know who's face the site took a picture of. I got this code below to take the picture and save it... but I"m not sure how to get the measurements or compare with another face. I've found examples for image comparison, but I imagine I need facial comparison. 
 var snap = function(){

vid.read(function(err, im){
    im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
    console.log("FACES", faces)
  if (faces.length == 0){
    console.log("No Faces")
    return false;
  }
  var face = faces[0]
    , ims = im.size()

  var im2 = im.roi(face.x, face.y, face.width, face.height)
  /*
  im.adjustROI(
       -face.y
     , (face.y + face.height) - ims[0]
     , -face.x
     , (face.x + face.width) - ims[1])
     */
  im2.save('out.jpg')
})

});
}
snap()
Thanks

A:

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.

