Deep Learning OCR on Document and Receipt Photos in Salesforce Apex
1 min readFeb 15, 2020
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.