How to detect faces in photo in Salesforce Apex

Facial detection is the foundation of any good photo app these days, and has wide reaching uses in so much else. I’m guessing you’re not keen to dive into the Deep Learning rabbit hole to set it up from scratch, though. That’s OK, we’ve got a lightning-fast alternative for you.

First, download our API Client. Unzip it and extract the contents to your project folder.

Proceed to call faceLocate.

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 fingersSwagFaceLocateResponse result = api.faceLocate(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

And just like that, you’re all done. Let’s try it out on this example photograph.

Image for post
Image for post

Here is our response:

{
"Successful": true,
"Faces": [
{
"LeftX": 1494,
"TopY": 250,
"RightX": 1598,
"BottomY": 353
}
],
"FaceCount": 1,
"ErrorDetails": null
}

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

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