Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 523
Bill Avatar asked Feb 08 '14 15:02

Bill


People also ask

How do I compare two images in Opencv?

To compare two images, we use the Mean Square Error (MSE) of the pixel values of the two images. Similar images will have less mean square error value. Using this method, we can compare two images having the same height, width and number of channels.

How do you compare two faces in Python?

Face Comparison Using Face++ and Python It is used for multiple purposes like AI, Web Development, Web Scraping, etc. One such use of Python can be Face Comparison. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.

How do I compare images in node JS?

var imgComparator = require('some-awesome-image-comparator-module'); // result would be between 1.0 and 0.0, where 1.0 would mean exact match var result = imgComparator. compare('/path/to/image/1. png', '/path/to/image/2. png');


1 Answers

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.

  1. Initialize FaceRecognizer: var FaceRecognizer = new cv.FaceRecognizer();
  2. 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.

like image 157
Fer To Avatar answered Nov 14 '22 20:11

Fer To