Skip to content

Commit b963485

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

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.cloud.vision.v1.ImageSource;
26+
import com.google.cloud.vision.v1.LocationInfo;
27+
import com.google.cloud.vision.v1.SafeSearchAnnotation;
28+
import com.google.protobuf.ByteString;
29+
import com.google.protobuf.Descriptors.FieldDescriptor;
30+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
31+
import com.google.cloud.vision.v1.ColorInfo;
32+
import com.google.cloud.vision.v1.DominantColorsAnnotation;
33+
import com.google.cloud.vision.v1.EntityAnnotation;
34+
import com.google.cloud.vision.v1.FaceAnnotation;
35+
import com.google.cloud.vision.v1.Feature;
36+
import com.google.cloud.vision.v1.Feature.Type;
37+
import com.google.cloud.vision.v1.AnnotateImageResponse;
38+
39+
import java.io.ByteArrayOutputStream;
40+
import java.io.FileInputStream;
41+
import java.io.IOException;
42+
import java.io.InputStream;
43+
import java.io.PrintStream;
44+
import java.util.ArrayList;
45+
import java.util.Iterator;
46+
import java.util.List;
47+
import java.util.Map;
48+
import java.util.Map.Entry;
49+
50+
import org.joda.time.Duration;
51+
52+
public class QuickstartSample {
53+
/**
54+
* Helper for getting byte array given input stream.
55+
* @param is Input stream to get bytes for.
56+
* @return Byte array for the input stream contents.
57+
* @throws IOException
58+
*/
59+
static public byte[] toByteArray(InputStream is) throws IOException {
60+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
61+
62+
int nRead;
63+
byte[] data = new byte[16384];
64+
65+
while ((nRead = is.read(data, 0, data.length)) != -1) {
66+
buffer.write(data, 0, nRead);
67+
}
68+
69+
buffer.flush();
70+
71+
return buffer.toByteArray();
72+
}
73+
74+
public static void main(String... args) throws Exception {
75+
// Instantiates a client
76+
ImageAnnotatorClient vision = ImageAnnotatorClient.create();
77+
78+
// The name of the image file to annotate
79+
String fileName = "./resources/wakeupcat.jpg";
80+
81+
List<AnnotateImageRequest> requests = new ArrayList<>();
82+
83+
FileInputStream file = new FileInputStream(fileName);
84+
ByteString imgBytes = ByteString.copyFrom(toByteArray(file));
85+
86+
Image img = Image.newBuilder().setContent(imgBytes).build();
87+
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
88+
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
89+
.addFeatures(feat)
90+
.setImage(img)
91+
.build();
92+
requests.add(request);
93+
94+
// Performs label detection on the image file
95+
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
96+
List<AnnotateImageResponse> responses = response.getResponsesList();
97+
98+
for (AnnotateImageResponse res : responses) {
99+
if (res.hasError()) System.out.printf("Error: %s\n", res.getError().getMessage());
100+
101+
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
102+
Map<FieldDescriptor, Object> fields = annotation.getAllFields();
103+
Iterator<Entry<FieldDescriptor, Object>> iter = fields.entrySet().iterator();
104+
while (iter.hasNext()) {
105+
Entry<FieldDescriptor, Object> entry = iter.next();
106+
System.out.append(entry.getKey().getJsonName());
107+
System.out.append(" : ").append('"');
108+
System.out.append(entry.getValue().toString());
109+
System.out.append("\"\n");
110+
}
111+
}
112+
}
113+
}
114+
}
115+
// [END vision_quickstart]

0 commit comments

Comments
 (0)