Skip to content

Commit 37b6fca

Browse files
committed
Add Vision API quickstart.
1 parent f381e73 commit 37b6fca

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2017, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
// [START vision_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.vision.spi.v1.ImageAnnotatorClient;
22+
import com.google.cloud.vision.spi.v1.ImageAnnotatorSettings;
23+
import com.google.cloud.vision.v1.AnnotateImageRequest;
24+
import com.google.cloud.vision.v1.Image;
25+
import com.google.protobuf.ByteString;
26+
import com.google.protobuf.Descriptors.FieldDescriptor;
27+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
28+
import com.google.cloud.vision.v1.EntityAnnotation;
29+
import com.google.cloud.vision.v1.Feature;
30+
import com.google.cloud.vision.v1.Feature.Type;
31+
import com.google.cloud.vision.v1.AnnotateImageResponse;
32+
33+
import java.io.ByteArrayOutputStream;
34+
import java.io.FileInputStream;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.nio.file.Files;
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
40+
import java.util.ArrayList;
41+
import java.util.Iterator;
42+
import java.util.List;
43+
import java.util.Map;
44+
import java.util.Map.Entry;
45+
46+
import org.joda.time.Duration;
47+
48+
public class QuickstartSample {
49+
public static void main(String... args) throws Exception {
50+
// Instantiates a client
51+
ImageAnnotatorClient vision = ImageAnnotatorClient.create();
52+
53+
// The name of the image file to annotate
54+
String fileName = "./resources/wakeupcat.jpg";
55+
56+
List<AnnotateImageRequest> requests = new ArrayList<>();
57+
58+
Path path = Paths.get(fileName);
59+
byte[] data = Files.readAllBytes(path);
60+
ByteString imgBytes = ByteString.copyFrom(data);
61+
62+
Image img = Image.newBuilder().setContent(imgBytes).build();
63+
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
64+
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
65+
.addFeatures(feat)
66+
.setImage(img)
67+
.build();
68+
requests.add(request);
69+
70+
// Performs label detection on the image file
71+
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
72+
List<AnnotateImageResponse> responses = response.getResponsesList();
73+
74+
for (AnnotateImageResponse res : responses) {
75+
if (res.hasError()) System.out.printf("Error: %s\n", res.getError().getMessage());
76+
77+
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
78+
Map<FieldDescriptor, Object> fields = annotation.getAllFields();
79+
Iterator<Entry<FieldDescriptor, Object>> iter = fields.entrySet().iterator();
80+
while (iter.hasNext()) {
81+
Entry<FieldDescriptor, Object> entry = iter.next();
82+
System.out.append(entry.getKey().getJsonName());
83+
System.out.append(" : ").append('"');
84+
System.out.append(entry.getValue().toString());
85+
System.out.append("\"\n");
86+
}
87+
}
88+
}
89+
}
90+
}
91+
// [END vision_quickstart]

0 commit comments

Comments
 (0)