Sign in

How to detect people in a photo in Salesforce Apex

What’s a photo app without the ability to detect people autonomously? Today we are going to tackle a method for applying this feature. Don’t worry, this is going to be super easy.

Download our API client and copy the /client folder into the directory of your Apex project.

Now we can implement the recognizeDetectPeople function, like this:

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

Pretty easy, huh? Let’s give it a quick shot with this photo.

Image for post
Image for post

Our result from this photo:

{
"Successful": true,
"Objects": [
{
"ObjectClassName": "person",
"Height": 1279,
"Width": 1361,
"Score": 0.8737415075302124,
"X": 772,
"Y": 162
},
{
"ObjectClassName": "person",
"Height": 1481,
"Width": 2167,
"Score": 0.8343223333358765,
"X": 916,
"Y": 18
}
],
"ObjectCount": 2
}

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