Search in sources :

Example 1 with EntityAnnotation

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

the class LabelAppIT method labelImage_cat_returnsCatDescription.

@Test
public void labelImage_cat_returnsCatDescription() throws Exception {
    List<EntityAnnotation> labels = appUnderTest.labelImage(Paths.get("data/cat.jpg"), MAX_LABELS);
    ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    for (EntityAnnotation label : labels) {
        builder.add(label.getDescription());
    }
    ImmutableSet<String> descriptions = builder.build();
    assertThat(descriptions).named("cat.jpg labels").contains("cat");
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) EntityAnnotation(com.google.api.services.vision.v1.model.EntityAnnotation) Test(org.junit.Test)

Example 2 with EntityAnnotation

use of com.google.api.services.vision.v1.model.EntityAnnotation 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)

Example 3 with EntityAnnotation

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

the class TextApp method detectText.

/**
 * Gets up to {@code maxResults} text annotations for images stored at {@code paths}.
 */
public ImmutableList<ImageText> detectText(List<Path> paths) {
    ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder();
    try {
        for (Path path : paths) {
            byte[] data;
            data = Files.readAllBytes(path);
            requests.add(new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("TEXT_DETECTION").setMaxResults(MAX_RESULTS))));
        }
        Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(requests.build()));
        // 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() == paths.size();
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (int i = 0; i < paths.size(); i++) {
            Path path = paths.get(i);
            AnnotateImageResponse response = batchResponse.getResponses().get(i);
            output.add(ImageText.builder().path(path).textAnnotations(MoreObjects.firstNonNull(response.getTextAnnotations(), ImmutableList.<EntityAnnotation>of())).error(response.getError()).build());
        }
        return output.build();
    } catch (IOException ex) {
        // Got an exception, which means the whole batch had an error.
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (Path path : paths) {
            output.add(ImageText.builder().path(path).textAnnotations(ImmutableList.<EntityAnnotation>of()).error(new Status().setMessage(ex.getMessage())).build());
        }
        return output.build();
    }
}
Also used : Path(java.nio.file.Path) Status(com.google.api.services.vision.v1.model.Status) BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 4 with EntityAnnotation

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

the class LabelAppTest method printLabels_manyLabels_printsLabels.

@Test
public void printLabels_manyLabels_printsLabels() throws Exception {
    // Arrange
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(bout);
    ImmutableList<EntityAnnotation> labels = ImmutableList.of(new EntityAnnotation().setDescription("dog").setScore(0.7564f), new EntityAnnotation().setDescription("husky").setScore(0.67891f), new EntityAnnotation().setDescription("poodle").setScore(0.1233f));
    // Act
    LabelApp.printLabels(out, Paths.get("path/to/some/image.jpg"), labels);
    // Assert
    String got = bout.toString();
    assertThat(got).contains("dog (score: 0.756)");
    assertThat(got).contains("husky (score: 0.679)");
    assertThat(got).contains("poodle (score: 0.123)");
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EntityAnnotation(com.google.api.services.vision.v1.model.EntityAnnotation) Test(org.junit.Test)

Example 5 with EntityAnnotation

use of com.google.api.services.vision.v1.model.EntityAnnotation 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)

Aggregations

EntityAnnotation (com.google.api.services.vision.v1.model.EntityAnnotation)4 AnnotateImageRequest (com.google.api.services.vision.v1.model.AnnotateImageRequest)3 AnnotateImageResponse (com.google.api.services.vision.v1.model.AnnotateImageResponse)3 BatchAnnotateImagesRequest (com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest)3 BatchAnnotateImagesResponse (com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)3 Feature (com.google.api.services.vision.v1.model.Feature)3 Image (com.google.api.services.vision.v1.model.Image)3 IOException (java.io.IOException)3 Test (org.junit.Test)2 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 ImmutableSet (com.google.common.collect.ImmutableSet)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1