Reputation: 131
I followed Google's documentation for their Vision API to write a program that runs TEXT_DETECTION on an image. Unlike the documentation and Google Vision the tester on their website which returns a properly formatted JSON, the JSON file I get in return is missing commas in between Objects and is missing brackets for JSONArrays.
I use the following line to get the JSON response.
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
Here is my code for the 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();
Here is a snippet of what my responses look like from 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
}
}
}
How can I change it or fix it so that the returned file is a properly formatted JSON file?
Upvotes: 0
Views: 414
Reputation: 81
You can add
com.google.protobuf.util.JsonFormat.printer().print(responses)
after
List<AnnotateImageResponse> responses = response.getResponsesList();
This will give you JSON string for your response object.
Upvotes: 1
Reputation: 1060
When using annotate, there is no json file. The response is a BatchAnnotateImagesResponse object.
If you want to generate a json file, you can use asyncBatchAnnotate instead. asyncBatchAnnotate writes json files to your gcs bucket when it finishes.
Upvotes: 1