Skip to content

Commit

Permalink
Updates snippets so the API client is created in examples (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss authored and lesv committed Mar 20, 2017
1 parent ec39827 commit 11135e9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
82 changes: 46 additions & 36 deletions vision/cloud-client/src/main/java/com/example/vision/Detect.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException
out.printf(
"\tjava %s \"<command>\" \"<path-to-image>\"\n"
+ "Commands:\n"
+ "\tall-local | faces | labels | landmarks | logos | text | safe-search | properties"
+ "\tfaces | labels | landmarks | logos | text | safe-search | properties"
+ "| web | crop \n"
+ "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage "
+ "resource (gs://...)\n",
Expand All @@ -81,16 +81,8 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException
String command = args[0];
String path = args.length > 1 ? args[1] : "";

Detect app = new Detect(ImageAnnotatorClient.create());
if (command.equals("all-local")) {
detectFaces("resources/face_no_surprise.jpg", out);
detectLabels("resources/wakeupcat.jpg", out);
detectLandmarks("resources/landmark.jpg", out);
detectLogos("resources/logos.png", out);
detectText("resources/text.jpg", out);
detectProperties("resources/landmark.jpg", out);
detectSafeSearch("resources/wakeupcat.jpg", out);
} else if (command.equals("faces")) {
Detect app = new Detect();
if (command.equals("faces")) {
if (path.startsWith("gs://")) {
detectFacesGcs(path, out);
} else {
Expand Down Expand Up @@ -155,15 +147,12 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException
}
}

private static ImageAnnotatorClient visionApi;

/**
* Constructs a {@link Detect} which connects to the Cloud Vision API.
*
* @param client The Vision API client.
*/
public Detect(ImageAnnotatorClient client) {
visionApi = client;
public Detect() {
}

/**
Expand All @@ -184,7 +173,8 @@ public static void detectFaces(String filePath, PrintStream out) throws IOExcept
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -271,7 +261,8 @@ public static void detectLabels(String filePath, PrintStream out) throws IOExcep
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -304,7 +295,8 @@ public static void detectLabelsGcs(String gcsPath, PrintStream out) throws IOExc
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand All @@ -315,7 +307,8 @@ public static void detectLabelsGcs(String gcsPath, PrintStream out) throws IOExc

// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
annotation.getAllFields().forEach((k, v) ->
out.printf("%s : %s\n", k, v.toString()));
}
}
}
Expand All @@ -337,7 +330,8 @@ public static void detectLandmarks(String filePath, PrintStream out) throws IOEx
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -371,7 +365,8 @@ public static void detectLandmarksUrl(String url, PrintStream out) throws IOExce
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -405,7 +400,8 @@ public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws IO
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -440,7 +436,8 @@ public static void detectLogos(String filePath, PrintStream out) throws IOExcept
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -473,7 +470,8 @@ public static void detectLogosGcs(String gcsPath, PrintStream out) throws IOExce
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -507,7 +505,8 @@ public static void detectText(String filePath, PrintStream out) throws IOExcepti
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -541,7 +540,8 @@ public static void detectTextGcs(String gcsPath, PrintStream out) throws IOExcep
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -576,7 +576,8 @@ public static void detectProperties(String filePath, PrintStream out) throws IOE
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -615,7 +616,8 @@ public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws I
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -655,7 +657,8 @@ public static void detectSafeSearch(String filePath, PrintStream out) throws IOE
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -692,7 +695,8 @@ public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws I
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -730,7 +734,8 @@ public static void detectWebDetections(String filePath, PrintStream out) throws
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -781,7 +786,8 @@ public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throw
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -833,7 +839,8 @@ public static void detectCropHints(String filePath, PrintStream out) throws IOEx
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -867,7 +874,8 @@ public static void detectCropHintsGcs(String gcsPath, PrintStream out) throws IO
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -902,7 +910,8 @@ public static void detectDocumentText(String filePath, PrintStream out) throws I
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down Expand Up @@ -955,7 +964,8 @@ public static void detectDocumentTextGcs(String gcsPath, PrintStream out) throws
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
BatchAnnotateImagesResponse response =
ImageAnnotatorClient.create().batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.vision.spi.v1.ImageAnnotatorClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -42,7 +41,7 @@ public void setUp() throws IOException {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
app = new Detect(ImageAnnotatorClient.create());
app = new Detect();
}

@After
Expand Down

0 comments on commit 11135e9

Please sign in to comment.