Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

How to setLanguageHint in Google OCR SDK?

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

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

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...