Home Salesforce Extract Text From Image using Google Cloud Vision

Extract Text From Image using Google Cloud Vision

by Dhanik Lal Sahni

Recently I got request from one of my blog member for extracting text from image which is uploaded in Salesforce record’s attachment. So i have analyzed some APIs which has similar capability.

There are various tool and APIs available for extracting text from images. Here are some of them which are popular in market

  1. Google Cloud Vision
  2. Microsoft Computer Vision
  3. Taggun
  4. Cloudmersive

For this blog, i have used Google Cloud Vision to extract text from image.  This OCR (Optical Character Recognition) API has special feature to recognize logo as well.

The Google Cloud Vision API enables developers to understand the content of an image by encapsulating powerful machine learning models in an easy to use REST API. You can build metadata on your image catalog, moderate offensive content, or enable new marketing scenarios through image sentiment analysis.

Let us see steps required to extract text from image.

  1. Enable Google Cloud Vision API
  2. Create Apex class to extract text from image
  3. Create lightning component to extract image
  4. Add lightning component as button on record page

1. Enable Google Cloud Vision API

First we have to enable Google Cloud Vision API and create API key for authentication purpose.  Below steps can be performed to get API key.

  1. Create user in Google cloud at https://console.developers.google.com . We can also use any existing gmail user.
  2. Create project in Google Console
  3. Once project is created, go to API library at https://console.developers.google.com/apis/library. Select Google Cloud Vision API and enable it.
  4. Create Credential (API Key) for API to be used.
  5. Add https://vision.googleapis.com in Salesforce’s remote site setting

2. Create Apex class to extract text from image

We have to create apex class which will use Google Cloud Vision API and extract text from image. You can check API request and response detail at https://cloud.google.com/vision/docs/reference/rest.

ImageService apex class will extract text. We should create separate request and response class for passing and getting data as JSON.

3. Create lightning component to extract image

In step 2, We have created image service class which will extract text from given image’s blob data. In this step we will get image file content and pass content to above mentioned class.

ImageServiceController class will be used to get all image files on which processing will be done for extracting text.


Let us create lightning component to consume above ImageServiceController. When image is successful extracted, this component show ‘Text extracted from image. Check file record detail section’.

4. Add lightning component as button on record page

Add lightning component as button or component in your record page.  If you have added that as button, when you click button it will call component and extract text from image. After you got message Text extracted from image. Check file record detail section, you can go to image file and check extracted texts.

Demo

References:

https://cloud.google.com/vision/docs/reference/rest

Related Posts

  1. Named Entity Recognition using Salesforce Einstein API
  2. Extract Driver License Detail from Image using Einstein API
  3. Create OCR App using Salesforce Einstein OCR API

You may also like

15 comments

Dhanik Lal Sahni May 3, 2020 - 8:00 am

Thank You Abhinav. Let me know, if you need our help.

Reply
Nilesh Borse May 18, 2020 - 6:16 pm

I am getting following error:
This API method requires billing to be enabled. Please enable billing on project #659325175485 by visiting https://console.developers.google.com/billing/enable?project=659325175485 then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry

Reply
Dhanik Lal Sahni May 19, 2020 - 2:22 pm

Hello Nilesh,

Looks like billing is not set for your vision project. Please check this video for that support https://www.youtube.com/watch?v=uINleRduCWM.
You can also log support ticket in google console to resolve this issue.

Thank You,
Dhanik

Reply
Vineet Bhagchandani May 29, 2020 - 2:08 pm

Hello Dhanik,
Great Post and Very Useful
One thing I need to know is how we can automatically parse the response to create a Lead Record or Contact Record. The Details are stored as the text file so how to appropriately parse the data to have accurate data?

Your help is much appreciated

Thanks in Advance

Reply
Dhanik Lal Sahni May 31, 2020 - 12:40 am

Hello Vineet,

You can use BoundingPoly (text location coordinates) for each text section. Check link https://cloud.google.com/vision/docs/handwritinghttps://cloud.google.com/vision/docs/handwriting
Thank You,
Dhanik

Reply
Vineet Bhagchandani May 29, 2020 - 2:39 pm

Or I mean to say how to identify the data like this is the name and this is address etc

Reply
Dhanik Lal Sahni May 31, 2020 - 12:42 am

Hello Vineet,
You can use BoundingPoly (text location coordinates) for each text section.

Thank You,
Dhanik

Reply
Create OCR App using Salesforce Einstein OCR API | SalesforceCodex June 26, 2020 - 1:07 pm

[…] Extract License Plate Number from Image In Salesforce […]

Reply
Sandeep October 24, 2020 - 11:37 pm

Hi Dhanik, Thank you for sharing this wonderful post. I have a query regarding component attribute “recordId”. Where does this attribute get its value from?

Thanks, Sandeep

Reply
Dhanik Lal Sahni October 28, 2020 - 6:34 pm

value for recordid is populated by aura framework for current record. see documentation for this.

Thank you,
Dhanik

Reply
Rahul S July 1, 2021 - 10:41 am

Hello Dhanik,

Great post, one doubt will the same code implementation work for getting a text from PDF file if we query the pdf data in “List docs=[SELECT ContentDocumentId FROM ContentDocumentLink
WHERE ContentDocument.FileType=’JPG’ and LinkedEntityId =:record];”

and use “https://vision.googleapis.com/v1/files:asyncBatchAnnotate?key=your-api-key” as end point URL.

Please suggest.

Thanks & Regards,
Rahul S

Reply
Dhanik Lal Sahni July 3, 2021 - 1:28 pm

Hello Rahul,

Concept will be same for PDF also. You have to change some request attribute. Please sample request at https://cloud.google.com/vision/docs/pdf.

Thank You,
Dhanik

Reply
Jonathan Q August 17, 2023 - 12:00 pm

I’m getting the error, “Variable does not exist” for Extracted_Text__c and Is_Extracted__c. Under what object are we supposed to create these variables?

Reply
Dhanik Lal Sahni August 26, 2023 - 6:34 pm

Hello Jonathan,
It will be created in ContentVersion object.
Thank You,
Dhanik

Reply
Ultimate Guide to Integrate Stripe with Salesforce CRM July 15, 2024 - 9:39 pm

[…] Extract Text From Images using Google Cloud Vision […]

Reply

Leave a Comment