Search in sources :

Example 1 with ImageSource

use of com.google.api.services.vision.v1.model.ImageSource in project java-docs-samples by GoogleCloudPlatform.

the class DetectLandmark method identifyLandmark.

/**
 * Gets up to {@code maxResults} landmarks for an image stored at {@code uri}.
 */
public List<EntityAnnotation> identifyLandmark(String uri, int maxResults) throws IOException {
    AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().setSource(new ImageSource().setGcsImageUri(uri))).setFeatures(ImmutableList.of(new Feature().setType("LANDMARK_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);
    BatchAnnotateImagesResponse batchResponse = annotate.execute();
    assert batchResponse.getResponses().size() == 1;
    AnnotateImageResponse response = batchResponse.getResponses().get(0);
    if (response.getLandmarkAnnotations() == null) {
        throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
    }
    return response.getLandmarkAnnotations();
}
Also used : BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) ImageSource(com.google.api.services.vision.v1.model.ImageSource) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Aggregations

AnnotateImageRequest (com.google.api.services.vision.v1.model.AnnotateImageRequest)1 AnnotateImageResponse (com.google.api.services.vision.v1.model.AnnotateImageResponse)1 BatchAnnotateImagesRequest (com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest)1 BatchAnnotateImagesResponse (com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)1 Feature (com.google.api.services.vision.v1.model.Feature)1 Image (com.google.api.services.vision.v1.model.Image)1 ImageSource (com.google.api.services.vision.v1.model.ImageSource)1 IOException (java.io.IOException)1