Dear Readers,

Welcome again! πŸ™‚

In previous post, we learned about how to authenticate with Google Cloud API. If you have not read, you can read the full blog Here.

In this post, we are going to learn how to use Google Cloud API to Extract the Text ( both Handwriting & Computer Writing ) and how to Detect Multiple Objects in Same picture.

Google Cloud Image Extraction API – Here is the link for the official link of Google Cloud API.

Request Format – Google API Accepts the JSON body as input.

Below is the typical request that api accept to read the computer text.

{
  "requests": [
    {
      "image": {
        "content": "base64-encoded-image"
,
        "source": {
          "imageUri" : "cloud-storage-image-uri"
,
          "gcsImageUri" : "public hosted image url"
        }
      },
      "features": [
        {
          "type"  : "TEXT_DETECTION"
,
          "model" : "builtin/stable",
          "maxResults": 10
        }
      ]
    }
  ]
}

Below is the typical body to detect handwriting text. If you wanted to read the text which is written by hand then use type as “DOCUMENT_TEXT_DETECTION” the only change is the type of task which should be “DOCUMENT_TEXT_DETECTION”

Create an Apex Class Name it “GoogleCloudVisionAPI”

Methods in the class

Method NameDescription
detectTextFromImageThis method is used to detect the text from image and text might be computer written or hand written. This method also inserts text into a custom object.
detectObjectFromImageThis method is used to identify the objects from the image and creates the data into a custom object.
detectLogoFromImageThe method used to detect the logo from the image.
detectSafeFromImageThe method used to check if the image is safe for children, adults, the image is racist or not.
doCallGcCloudAPIThis is the main and reusable method that is used to call the google cloud API with the help of another method.
sendRequestThis method is used to send the request to Google Cloud API and returns the response.
prepareBodyThis method is used to prepare the body for each request.

Click Here for complete code.

Below is the code for the class.

Note: – Please use the above GitHub Repo code to get the complete code and related classes which are used in parent class “GoogleCloudVisionAPI

Attached is the demo video

Thanks for reading πŸ™‚

#Trailblazer #Sharing #GivingBack #Integration

Leave a Reply