How to detect faces in photo in Javascript
2 min readMar 11, 2020
Face detection is the bedrock upon which so many useful features can be built, everything from photo filters to sorting. So are you ready to start your own Deep Learning farm and train up an AI from scratch to do this? Neither am I. Instead, I’m going to show you how to use an API to get the same results in a tiny fraction of the time.
To start off, we must import our client. To do this, simply past this snippet between the head tags of your page.
<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-image-client.js"></script>
Now call our function, like so.
var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveImageApiClient.FaceApi();var imageFile = "/path/to/file"; // 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);
Easy! All faces will be accounted for using this format for the results:
{
"Successful": true,
"Faces": [
{
"LeftX": 0,
"TopY": 0,
"RightX": 0,
"BottomY": 0
}
],
"FaceCount": 0,
"ErrorDetails": "string"
}