java - 从 TEXT_DETECTION 返回的 JSON 文件格式不正确
问题描述
我按照 Google 的 Vision API 文档编写了一个在图像上运行 TEXT_DETECTION 的程序。与文档和 Google Vision 不同,他们网站上的测试人员返回格式正确的 JSON,我得到的 JSON 文件在对象之间缺少逗号,并且缺少 JSONArrays 的括号。
我使用以下行来获取 JSON 响应。
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
这是我的 TEXT_DETECTION 代码。
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 中回复的内容片段。
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 文件?
解决方案
使用annotate时,没有json文件。响应是 BatchAnnotateImagesResponse 对象。
如果要生成 json 文件,可以使用asyncBatchAnnotate代替。asyncBatchAnnotate 在完成时将 json 文件写入您的 gcs 存储桶。
推荐阅读
- python - 在元组列表的组件内随机化一个值
- reactjs - 在输入组件中添加清除按钮
- flutter - 如何将自动生成的 Id 从 Firestore 获取到 Flutter 中?
- google-apps-script - 使用脚本将信息从一张纸复制到谷歌表格中的另一张纸
- tailwind-css - Tailwind Netlify - 本地站点看起来与部署不同
- time - 为什么时间需要是一个宏?
- ios - 如何从 TabBarViewController 中的按钮开始和停止脉冲动画
- javascript - 如何创建像 Phillips Hue 这样的颜色选择器圆圈
- android - 在保存状态(变量值)的同时在片段之间切换 [edittexts 和 textviews]
- javascript - 如何覆盖 JavaScript 中的对象?