Search in sources :

Example 6 with EntityAnnotation

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

Example 7 with EntityAnnotation

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]
}
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

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