How to detect faces in photo in Node.js?

Cloudmersive
2 min readMay 19, 2019

--

The first step is to add the package reference:

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

Next up, we want to import the library:

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');

Now from here, we want to run the faceLocate method:

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 imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.faceLocate(imageFile, callback);

It’s that easy! From here, we will get output that looks like this:

{
"ErrorDetails": null,
"Successful": true,
"Faces": [
{
"LeftX": 227,
"TopY": 181,
"RightX": 330,
"BottomY": 284
},
{
"LeftX": 344,
"TopY": 194,
"RightX": 493,
"BottomY": 344
},
{
"LeftX": 410,
"TopY": 6,
"RightX": 496,
"BottomY": 93
},
{
"LeftX": 77,
"TopY": 77,
"RightX": 181,
"BottomY": 181
},
{
"LeftX": 245,
"TopY": 45,
"RightX": 317,
"BottomY": 117
},
{
"LeftX": 325,
"TopY": 45,
"RightX": 397,
"BottomY": 117
},
{
"LeftX": 526,
"TopY": 135,
"RightX": 630,
"BottomY": 238
},
{
"LeftX": 160,
"TopY": 150,
"RightX": 246,
"BottomY": 237
}
],
"FaceCount": 8
}

We can see that in this example, 8 faces were located. We can take advantage of other similar methods to locate people (note that people and faces are not quite the same thing) or objects. We can even call another method to get a more detailed set of information on each face, including the location of eyes, nose, mouth, cheeks, etc.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet