首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从TEXT_DETECTION返回的JSON文件格式不正确

从TEXT_DETECTION返回的JSON文件格式不正确
EN

Stack Overflow用户
提问于 2019-08-10 11:11:14
回答 2查看 283关注 0票数 0

我按照谷歌的Vision API文档编写了一个在图像上运行TEXT_DETECTION的程序。与文档和他们网站上的Google Vision测试人员不同,我得到的JSON文件没有对象之间的逗号,也没有JSONArrays的括号。

我使用下面这行代码来获取JSON响应。

代码语言:javascript
复制
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);

以下是我的TEXT_DETECTION代码。

代码语言:javascript
复制
    public static final Type SEARCH_TYPE = Type.TEXT_DETECTION;

    public static void main(String... args) throws Exception {
        try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
            String fileName = "resources/testPic.jpg";

            Path path = Paths.get(fileName);
            byte[] data = Files.readAllBytes(path);
            ByteString imgBytes = ByteString.copyFrom(data);
            List<AnnotateImageRequest> requests = new ArrayList<>();
            Image img = Image.newBuilder().setContent(imgBytes).build();
            Feature feat = Feature.newBuilder().setType(SEARCH_TYPE).build();
            AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
            requests.add(request);

            BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
            List<AnnotateImageResponse> responses = response.getResponsesList();

下面是我从BatchAnnotateImagesResponse得到的响应的一小段。

代码语言:javascript
复制
 text_annotations {
    description: "almora"
    bounding_poly {
      vertices {
        x: 2307
        y: 713
      }
      vertices {
        x: 2480
        y: 711
      }
      vertices {
        x: 2480
        y: 727
      }
      vertices {
        x: 2307
        y: 729
      }
    }
  }
  text_annotations {
    description: "ryan"
    bounding_poly {
      vertices {
        x: 2458
        y: 906
      }
      vertices {
        x: 2489
        y: 933
      }
      vertices {
        x: 2476
        y: 947
      }
      vertices {
        x: 2446
        y: 920
      }
    }
  }
  text_annotations {
    description: "flanco"
    bounding_poly {
      vertices {
        x: 2441
        y: 890
      }
      vertices {
        x: 2458
        y: 905
      }
      vertices {
        x: 2443
        y: 922
      }
      vertices {
        x: 2426
        y: 907
      }
    }
  }
  text_annotations {
    description: "garbanzo beans"
    bounding_poly {
      vertices {
        x: 3780
        y: 1051
      }
      vertices {
        x: 3824
        y: 1050
      }
      vertices {
        x: 3824
        y: 1063
      }
      vertices {
        x: 3780
        y: 1064
      }
    }
  }
  text_annotations {
    description: "roberto"
    bounding_poly {
      vertices {
        x: 2111
        y: 906
      }
      vertices {
        x: 2163
        y: 905
      }
      vertices {
        x: 2163
        y: 920
      }
      vertices {
        x: 2111
        y: 921
      }
    }
  }
  text_annotations {
    description: "A10"
    bounding_poly {
      vertices {
        x: 2398
        y: 935
      }
      vertices {
        x: 2442
        y: 972
      }
      vertices {
        x: 2424
        y: 994
      }
      vertices {
        x: 2380
        y: 956
      }
    }
  }

如何更改或修复它,以使返回的文件是格式正确的JSON文件?

EN

Stack Overflow用户

发布于 2020-01-21 15:50:52

您可以添加

com.google.protobuf.util.JsonFormat.printer().print(responses)

之后

List<AnnotateImageResponse> responses = response.getResponsesList();

这将为您的响应对象提供JSON字符串。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57439013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档