0

I am testing few sample code for OCR using Google Cloud Vision API. I observed the APIs can able to detect English language very easily from an Image but in other language like Hindi, the APIs are not able to detect.

MyCode :

public static void detectText(String filePath) throws Exception, IOException {
    System.out.println("Detect Text\n");

    List<AnnotateImageRequest> requests = new ArrayList<>();

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

    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();

    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);


    Credentials myCredentials = ServiceAccountCredentials.fromStream(
            new FileInputStream(jsonPath));
    ImageAnnotatorSettings imageAnnotatorSettings =
            ImageAnnotatorSettings.newBuilder()
                    .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
                    .build();
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create(imageAnnotatorSettings)) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();

        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.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()) {
                System.out.printf("Text: %s\n", annotation.getDescription());
            }
        }
    }
}

Image :

enter image description here

But the same image, i have tried in Google Drive, all the text from the image is easily detected.

Please let me know, same image how can i use in the code to detect the text?

deeptimancode
  • 1,139
  • 4
  • 18
  • 40

2 Answers2

2

Use ImageContext class

ImageContext imageContext = ImageContext.newBuilder()
                .addLanguageHints("hi")
                .build();

and set ImageContext in your AnnotateImageRequest

AnnotateImageRequest request =
                AnnotateImageRequest.newBuilder()
                        .addFeatures(feat)
                        .setImage(img)
                        .setImageContext(imageContext)
                        .build();

Ref : OCR Language Support

Hope this help!

Tachakorn
  • 21
  • 1
  • 2
0

Note: I'm not a Java developer.

Below is the general representation, that matches your need in Python

{
  "requests": [
    {
      "imageContext": {
        "languageHints": [
          "hi"
        ]
      },
      "features": [
        {
          "type": "TEXT_DETECTION"
        }
      ]
    }
  ]
}

Refer to: https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#ImageContext

ExtractTable.com
  • 762
  • 10
  • 20