how to use google cloud vision to extract multiple text language in android studio

2.4k Views Asked by At

I am trying to build an android application (in android studio platform) which extracts different text languages from image using google cloud vision, but I have a problem in starting.

I don't know how to use google cloud files. Which files do I need to create or download and how to direct my API to extract multiple languages?

I got the API and this source code :

POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY

{
  "requests": [
    {
      "image": {
        "content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
      },
      "features": [
        {
          "type": "TEXT_DETECTION"
        }
      ]
    }
  ]
}



public static void detectText(String filePath, PrintStream out) throws Exception, IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        out.printf("Error: %s\n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
        out.printf("Text: %s\n", annotation.getDescription());
        out.printf("Position : %s\n", annotation.getBoundingPoly());
      }
    }
  }
}
1

There are 1 best solutions below

0
On

I would suggest you to try your image data on the Cloud Vision Api Explorer [1]. You can try the API directly on the web browser with an Oauth2 authentication. Follow the steps below: Enable the API on Google Cloud Console -> APIs and Services -> Libraries Set the scopes on the API Explorer checkbox:

https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/cloud-vision

{
  "requests": [
    {
      "features": [
        {
          "type": "TEXT_DETECTION"
        }
      ],
      "image": {
        "source": {
          "imageUri": "http://dps.usc.edu/files/2015/07/text-alerts.png"
        },
      },
    "imageContext": {
        "languageHints": [
          "en"
        ]
    }
  ]
}

Set de “imageContext”. There you can set language hints, but the API might detect the language automatically. Check this [2] for available language hints.

In the source you could use an image from your Google Cloud Storage bucket changing “imageUri” by: "gcsImageUri": "gs://your-bucket/text-alerts.png" as your image uri. Note the change in protocol. You are using “content” instead of “source”, and this is for setting a base64 encoded string image. You can try to encode an image with Base64 copy the encode as plain text and try on the API Explorer to check that the encode is correct and works. Be careful when copying as you may get noise like \n, \t and things that might break your b64 encode. I share a python code that does the job:

import base64
f = open("text-alerts.png", "rb")
encoded = base64.b64encode(f.read())
print(encoded)
f.close()
fw = open('content.b64', "wb")
fw.write(encoded)
fw.close()

In your request:

{ "requests": [ { "image": { "content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z" }, "features": [ { "type": "TEXT_DETECTION" } ] } ] }

The content tag is the image string in Base64: "content":“/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z” You can use a web tool to do the same and check that your Base64 works. You can load the file on the Android Studio.

Here [4] you can find a sample for Android, with a README that explains how to configure your App. You need to create your API Key here [3] and in the MainActivity you have a variable that must be set to your API Key that then is used for the request.

private static final String CLOUD_VISION_API_KEY = "YOUR_API_KEY";

The sample loads an image and converts it to Base64 before sending the request [5]. See the method callCloudVision, inside there is an AsyncTask that retrieves an image and converts it to Base64 before sending the request.

[1] https://cloud.google.com/vision/docs/quickstart

[2] https://cloud.google.com/vision/docs/languages

[3] https://console.cloud.google.com/apis/credentials?project=your-project-id

[4] https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/android

[5] https://github.com/GoogleCloudPlatform/cloud-vision/blob/master/android/CloudVision/app/src/main/java/com/google/sample/cloudvision/MainActivity.java#L192