【发布时间】:2018-04-09 12:25:45
【问题描述】:
我正在使用 Google Cloud Vision API 测试 OCR 的一些示例代码。我观察到 API 可以很容易地从图像中检测到 English 语言,但在 Hindi 等其他语言中,API 无法检测到。
我的代码:
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());
}
}
}
}
图片:
但是同一张图片,我在 Google Drive 中试过,图片中的所有文字都很容易检测到。
请告诉我,我如何在代码中使用相同的图像来检测文本?
【问题讨论】:
标签: ocr google-vision hindi