Age detection of photos in Salesforce Apex

Photographic age detection is awesome, and it opens up many doors such as user demographic collection and advertisement tailoring. Today we will dive into a super simple method for implementing this great feature in a matter of minutes instead of hours or days.

To kick this party off, we have to download the Cloudmersive API client. Then extract the archive and drop the contents into your project directory.

Proceed to calling our function, faceDetectAge, as you can see here:

SwagFaceApi api = new SwagFaceApi();SwagClient client = api.getClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{'imageFile' => Blob.valueOf('Sample text file\nContents')};try {// cross your fingersSwagAgeDetectionResult result = api.faceDetectAge(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

So far, so good. Let’s try it out on this photo.

Image for post
Image for post

Now here is our response information.

{
"Successful": true,
"PeopleWithAge": [
{
"FaceLocation": {
"LeftX": 367,
"TopY": 432,
"RightX": 653,
"BottomY": 819
},
"AgeClassificationConfidence": 0.9,
"AgeClass": "25-32",
"Age": 28.38979721069336
}
],
"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