Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
");

    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
", 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
", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
281 views
Welcome To Ask or Share your Answers For Others

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...