How to crop an image to the face in Salesforce Apex

Facial cropping is a great way to auto-generate profile pictures and thumbnails, among other things. So how can we unlock this practical potential without delving into Deep Learning? The answer is simple, let’s use an API.

To get things going, we need to download and extract our API client into the directory of our project.

We can now choose between round and square cropping by calling either faceCropFirstRound or faceCropFirst, and selecting an image.

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

And that’s it! The API will handle the Deep Learning, and our photo will be returned to us cropped. Not bad.

Image for post
Image for post

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