在Google Vision中以表格格式获取表格或文档中的正确文本

时间:2019-09-17 14:24:34

标签: java google-api google-vision vision-api

Screenshot of excel

您好,我正在尝试从上面的图像(是表格)中获取文本。运行以下代码后得到的输出是

    String text = StringUtils.EMPTY;
    List<AnnotateImageRequest> requests = new ArrayList<>();
    FileInputStream f = new FileInputStream("Path of img");
    ByteString imgBytes = ByteString.readFrom(f);
    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()) {
                LOGGER.info("Error: %s\n" + res.getError().getMessage());
            }
            TextAnnotation annotation = res.getFullTextAnnotation();
            LOGGER.info("\nComplete annotation:");
            text = annotation.getText();
            System.out.println(text);
        }
    }

我得到的输出是

15
16
Data Below
Invoice Date Due Date Amount Size
1 01-02-2019 #
2 02-02-2019 08-02-2020
3 03-02-2019 09-02-2020 26
4 04-02-2019 10-02-2020 28
5 05-02-2019 11-02-2020
6 $
12-02-2020
30
19

0 个答案:

没有答案