From 37b6fca23cea60958de5b6c3c391b1ea6242d487 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Thu, 12 Jan 2017 15:25:09 -0800 Subject: [PATCH] Add Vision API quickstart. --- .../com/example/vision/QuickstartSample.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java diff --git a/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java b/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java new file mode 100644 index 00000000000..2df9043fec8 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java @@ -0,0 +1,91 @@ +/* + Copyright 2017, Google, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.example.vision; + +// [START vision_quickstart] +// Imports the Google Cloud client library +import com.google.cloud.vision.spi.v1.ImageAnnotatorClient; +import com.google.cloud.vision.spi.v1.ImageAnnotatorSettings; +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.Image; +import com.google.protobuf.ByteString; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Feature.Type; +import com.google.cloud.vision.v1.AnnotateImageResponse; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.joda.time.Duration; + +public class QuickstartSample { + public static void main(String... args) throws Exception { + // Instantiates a client + ImageAnnotatorClient vision = ImageAnnotatorClient.create(); + + // The name of the image file to annotate + String fileName = "./resources/wakeupcat.jpg"; + + List requests = new ArrayList<>(); + + Path path = Paths.get(fileName); + byte[] data = Files.readAllBytes(path); + ByteString imgBytes = ByteString.copyFrom(data); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); + AnnotateImageRequest request = AnnotateImageRequest.newBuilder() + .addFeatures(feat) + .setImage(img) + .build(); + requests.add(request); + + // Performs label detection on the image file + BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) System.out.printf("Error: %s\n", res.getError().getMessage()); + + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + Map fields = annotation.getAllFields(); + Iterator> iter = fields.entrySet().iterator(); + while (iter.hasNext()) { + Entry entry = iter.next(); + System.out.append(entry.getKey().getJsonName()); + System.out.append(" : ").append('"'); + System.out.append(entry.getValue().toString()); + System.out.append("\"\n"); + } + } + } + } +} +// [END vision_quickstart]