我按照谷歌的Vision API文档编写了一个在图像上运行TEXT_DETECTION的程序。与文档和他们网站上的Google Vision测试人员不同,我得到的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文件?
发布于 2020-01-21 15:50:52
您可以添加
com.google.protobuf.util.JsonFormat.printer().print(responses)
之后
List<AnnotateImageResponse> responses = response.getResponsesList();
这将为您的响应对象提供JSON字符串。
https://stackoverflow.com/questions/57439013
复制相似问题