Nowadays organizations are going for digital automation for most of their repetitive works like manual entries of records from printed forms. This manual entry requirement can be for application form, insurance forms, doctor prescription forms, examination forms, digitizing business cards and many more.
For extracting text from images we have many API service available which gives almost 95% accuracy. Refer my other post related to this service.
- Extract License Plate Number from Image In Salesforce
- Extract Text From Image using Google Cloud Vision
Salesforce announced OCR (Einstein Optical Character Recognition) service in Apr,2019. This API is now available for use.
Einstein Optical Character Recognition (OCR) leverages computer vision to analyze documents and extract the relevant information, making repetitive tasks like data entry more efficient.
Let us integrated Einstein OCR in Salesforce for extracting form data. Below steps will be required for integrating it.
- Create account in Einstein Platform Services
- Create Private Key and Generate Token
- Call Einstein OCR API from Apex
- Extract image data in Case object
1. Create account in Einstein Platform Services
We have to consume Einstein OCR API so first create API account. Create account at https://api.einstein.ai/signup. This will send email to your provided email. Confirm email to start working on OCR.
In registration process, it will ask you to download key file. Download that file, it will be used to generate token. File will be saved as einstein_platform.pem. Upload this file in Salesforce File object. Below is screenshot for file record of key file.
Yon can also follow steps which is mentioned at https://metamind.readme.io/docs/what-you-need-to-call-api
2. Create Private Key and Generate Token
For integrating external API from Apex we need API token which will require to authenticate request. Einstein OCR API require valid JWT Token. This token will be generated from above mentioned key file.
Token can be generated online from https://api.einstein.ai/token as well but this will not work when we use API in Salesforce. We have to generate token at runtime before calling OCR API. We will use API https://api.einstein.ai/v2/oauth2/token for generating token from apex.
Apex Class for generating Token
EinsteinController.getAccessToken() should be called to generate token from apex code before calling API.
3. Call Einstein OCR API from Apex
We have API Token and API url https://api.einstein.ai/v2/vision/ocr to extract texts from image. Let us call API from apex with required request data.
- sampleLocation : This is image url. We can get downloadable url for our uploaded image. Refer Extract License Plate Number from Image In Salesforce for creating downloadable url for any uploaded image.
- modelId : This parameter define which type of text need to be extracted from image like tabular data or business card. Value for this parameter can be OCRModel (for unstructured data) and tabulatev2 (for tabular data)
Einstein OCR API can be called using multipart/form-data and request parameter will be passed in body as blob.
blob formBlob = EncodingUtil.base64Decode(form64); string contentLength = string.valueOf(formBlob.size()); req.setBodyAsBlob(formBlob);
Apex Code for calling API
Note: Add API url (https://api.einstein.ai) in remote site setting or you can use named credential to avoid this.
4. Extract image data in Case object
Now we are ready with consuming API service to extract image. For this post, I have created one sample image form where some fields information are present. Using above Einstein OCR API we will extract data from image and put that in Case object.
We need to extract field data from above mentioned image. Similar to this we can have different form or business card. To extract information from above mentioned we have to map these information in one mapping object.
Create one custom object OCRTemplateMapping__c with below fields.
|Field Name||Data Type||Size|
Below are sample record for above mentioned image.
If you need to use other form then put these X,Y coordinates accordingly. You can check X and Y coordinates from https://yangcha.github.io/iview/iview.html . For my sample image X, Y cordinates will be like below image.
As we have to extract image data and put that in case object so add below fields in Case object.
|Field Name||Data Type||Size|
Now we ready with object creation. Let us write Apex code that will extract proper image data and put that in case record.
Flow to call above Apex Action:
Create a flow which call above apex method. You can refer post (Extract License Plate Number from Image In Salesforce) for flow and content url creation.
Button to call Flow:
Add one action button in case object which will call above flow.