【发布时间】:2020-02-09 16:35:26
【问题描述】:
我正在使用 Cloud Vision API - DOCUMENT_TEXT_DETECTION 功能从图像中检测手写文本。尽管它为我提取了手写数据,但是当涉及到同时具有打印文本和手写文本的图像时,它没有响应标识符,该标识符表示该位是手写的并且该位是打印的。直截了当地问,我想确认一张图片是否有手写文字。注意 - 图像可能仅包含手写文本或打印和手写文本的组合。
如果有人可以详细说明我需要传递给云视觉 api 以实现结果的所有其他属性,将不胜感激?或者有没有办法让 Cloud Vision API 标记出我的图像是否包含手写数据。
示例代码
public class Detect {
public static void main(String args[]) {
String filePath = "C:\\Development_Avecto\\images.jpg";
try {
detectDocumentText(filePath, System.out);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void detectDocumentText(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.DOCUMENT_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();
client.close();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
out.printf("Error: %s\n", res.getError().getMessage());
return;
}
TextAnnotation annotation = res.getFullTextAnnotation();
for (Page page : annotation.getPagesList()) {
String pageText = "";
for (Block block : page.getBlocksList()) {
String blockText = "";
for (Paragraph para : block.getParagraphsList()) {
String paraText = "";
for (Word word : para.getWordsList()) {
String wordText = "";
for (Symbol symbol : word.getSymbolsList()) {
wordText = wordText + symbol.getText();
out.format("Symbol text: %s (confidence: %f)\n",
symbol.getText(),symbol.getConfidence());
}
out.format("Word text: %s (confidence: %f)\n\n",wordText, word.getConfidence());
paraText = String.format("%s %s", paraText,wordText);
}
// Output Example using Paragraph:
out.println("\nParagraph: \n" + paraText);
out.format("Paragraph Confidence: %f\n",para.getConfidence());
blockText = blockText + paraText;
}
pageText = pageText + blockText;
}
}
out.println("\nComplete annotation:");
out.println(annotation.getText());
}
}
}
}
【问题讨论】:
标签: google-cloud-platform ocr google-cloud-vision