How to Detect and Find Faces in an Image using Node.js
When processing digital photos of people for any purpose, it’s good to have metadata about the location of faces within the photo. This can help with any subsequent facial recognition operation, and it can also help inform choices of effects and filters that might be applied to the photo (ensuring they do not . Using our Facial Detection API, you can quickly locate the position of each face within an image, and receive specific x/y pixel coordinates as well as a total “FaceCount” value. The below example API response model can serve as a reference for the structured (JSON) data this API returns:
{
"Successful": true,
"Faces": [
{
"LeftX": 0,
"TopY": 0,
"RightX": 0,
"BottomY": 0
}
],
"FaceCount": 0,
"ErrorDetails": "string"
}
Now, we’ll walk through how you can take advantage of this API by installing a Node.js SDK and structuring your API call with ready-to-run Node.js code.
First, run the following command to install the SDK:
npm install cloudmersive-image-api-client --save
Next, include the following code block to call the image processing function, and supply your API key to authenticate (this can be obtained by registering a free account on our website):
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 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);
No more code required after that — you’re all set.