use of com.google.api.services.vision.v1.model.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.
the class TextApp method extractDescriptions.
/**
* Extracts as a combinded string, all the descriptions from text annotations on an {@code image}.
*/
public Word extractDescriptions(ImageText image) {
String document = "";
for (EntityAnnotation text : image.textAnnotations()) {
document += text.getDescription();
}
if (document.equals("")) {
System.out.printf("%s had no discernible text.\n", image.path());
}
// Output a progress indicator.
System.out.print('.');
System.out.flush();
return Word.builder().path(image.path()).word(document).build();
}
use of com.google.api.services.vision.v1.model.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.
the class LabelApp method labelImage.
/**
* Gets up to {@code maxResults} labels for an image stored at {@code path}.
*/
public List<EntityAnnotation> labelImage(Path path, int maxResults) throws IOException {
// [START construct_request]
byte[] data = Files.readAllBytes(path);
AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("LABEL_DETECTION").setMaxResults(maxResults)));
Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);
// [END construct_request]
// [START parse_response]
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getLabelAnnotations() == null) {
throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
}
return response.getLabelAnnotations();
// [END parse_response]
}
Aggregations