Search in sources :

Example 6 with Vision

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());
    }
}
Also used : EntityAnnotation(com.google.api.services.vision.v1.model.EntityAnnotation)

Example 7 with Vision

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]
}
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) 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)4 AnnotateImageResponse (com.google.api.services.vision.v1.model.AnnotateImageResponse)4 BatchAnnotateImagesRequest (com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest)4 BatchAnnotateImagesResponse (com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)4 Feature (com.google.api.services.vision.v1.model.Feature)4 Image (com.google.api.services.vision.v1.model.Image)4 IOException (java.io.IOException)4 Path (java.nio.file.Path)2 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)1 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)1 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)1 Vision (com.google.api.services.vision.v1.Vision)1 EntityAnnotation (com.google.api.services.vision.v1.model.EntityAnnotation)1 FaceAnnotation (com.google.api.services.vision.v1.model.FaceAnnotation)1 ImageSource (com.google.api.services.vision.v1.model.ImageSource)1 Status (com.google.api.services.vision.v1.model.Status)1 ImmutableList (com.google.common.collect.ImmutableList)1 BufferedImage (java.awt.image.BufferedImage)1