Age detection of photos in Javascript

A solid algorithm for age detection can give you some great options from a developer’s standpoint, including ad tailoring and user demographics. Don’t worry, this article is not going to go into the long and laborious task of setting up and training an AI with Deep Learning. We are still going to have functional photographic age detection by the end, however, but through much easier means. In a nutshell, our API is going to do all the work for us.

To begin with, import our client with this script tag for your HTML file.

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-image-client.js"></script>

Chugging right along, we are now going to use this code snippet to activate to call upon our API:

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.faceDetectAge(imageFile, callback);

Drop in your file and that’s all that’s needed. Let’s try it out on this image here:

Image for post
Image for post

Our results for this:

{
"Successful": true,
"PeopleWithAge": [
{
"FaceLocation": {
"LeftX": 304,
"TopY": 88,
"RightX": 875,
"BottomY": 760
},
"AgeClassificationConfidence": 0.9,
"AgeClass": "25-32",
"Age": 29.34435272216797
}
],
"PeopleIdentified": 1
}

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