Deep Learning OCR on Document and Receipt Photos in Salesforce Apex

Extracting text from document photos with optical character recognition has never been easier in Apex. Today’s lesson will demonstrate just how easy the process can be with the help of an API.

To begin, we need to download our Apex Client. Then extract the /client folder into your Apex project. With that out of the way, all we need to do is call imageOcrPhotoWordsWithLocation.

SwagImageOcrApi api = new SwagImageOcrApi();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'),'recognitionMode' => 'recognitionMode_example','language' => 'language_example','preprocessing' => 'preprocessing_example','diagnostics' => 'diagnostics_example'};try {// cross your fingersSwagPhotoToWordsWithLocationResult result = api.imageOcrPhotoWordsWithLocation(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

The API will proceed to apply some deep learning to the input photograph and provide its results in text form.

Image for post
Image for post

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