How to crop an image to the face in Salesforce Apex
1 min readFeb 20, 2020
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.