use of com.google.api.services.vision.v1.Vision in project java-docs-samples by GoogleCloudPlatform.
the class DetectLandmark method main.
// [START run_application]
/**
* Annotates an image using the Vision API.
*/
public static void main(String[] args) throws IOException, GeneralSecurityException {
if (args.length != 1) {
System.err.println("Usage:");
System.err.printf("\tjava %s gs://<bucket_name>/<object_name>\n", DetectLandmark.class.getCanonicalName());
System.exit(1);
} else if (!args[0].toLowerCase().startsWith("gs://")) {
System.err.println("Google Cloud Storage url must start with 'gs://'.");
System.exit(1);
}
DetectLandmark app = new DetectLandmark(getVisionService());
List<EntityAnnotation> landmarks = app.identifyLandmark(args[0], MAX_RESULTS);
System.out.printf("Found %d landmark%s\n", landmarks.size(), landmarks.size() == 1 ? "" : "s");
for (EntityAnnotation annotation : landmarks) {
System.out.printf("\t%s\n", annotation.getDescription());
}
}
use of com.google.api.services.vision.v1.Vision 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