How to compare and match faces in Node.js

For this task, we want to determine if two different faces are the same. This can be useful for security applications, such as face login, photo matching, and so on.

The first step is to add a reference to the library we need:

"dependencies": {
"cloudmersive-image-api-client": "^1.1.4"
}

Now, all we need to do is call the face compare (faceCompare) method:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveImageApiClient.FaceApi();var inputImage = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on; this image can contain one or more faces which will be matched against face provided in the second image. Common file formats such as PNG, JPEG are supported.var matchFace = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image of a single face to compare and match against.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.faceCompare(inputImage, matchFace, callback);

Note that this will take two images in and determine if the faces in them match correctly. Here is an example output:

{
"ErrorDetails": null,
"Successful": true,
"Faces": [
{
"LeftX": -86,
"TopY": 56,
"RightX": 627,
"BottomY": 698,
"HighConfidenceMatch": true,
"MatchScore": 0.8687548935413361
}
],
"FaceCount": 1
}

Note that the input image can have multiple faces, but the match face should only have one reference face. The MatchScore represents a value between 0.0 and 1.0. Higher scores represent a closer match, with values above 0.8 being a good match.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store