Skip to content

Commit 10bc194

Browse files
gguusschingor13
authored andcommitted
samples: Updates snippets so the API client is created in examples (#572)
1 parent 07854d2 commit 10bc194

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

vision/snippets/src/main/java/com/example/vision/Detect.java

+46-36
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException
7171
out.printf(
7272
"\tjava %s \"<command>\" \"<path-to-image>\"\n"
7373
+ "Commands:\n"
74-
+ "\tall-local | faces | labels | landmarks | logos | text | safe-search | properties"
74+
+ "\tfaces | labels | landmarks | logos | text | safe-search | properties"
7575
+ "| web | crop \n"
7676
+ "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage "
7777
+ "resource (gs://...)\n",
@@ -81,16 +81,8 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException
8181
String command = args[0];
8282
String path = args.length > 1 ? args[1] : "";
8383

84-
Detect app = new Detect(ImageAnnotatorClient.create());
85-
if (command.equals("all-local")) {
86-
detectFaces("resources/face_no_surprise.jpg", out);
87-
detectLabels("resources/wakeupcat.jpg", out);
88-
detectLandmarks("resources/landmark.jpg", out);
89-
detectLogos("resources/logos.png", out);
90-
detectText("resources/text.jpg", out);
91-
detectProperties("resources/landmark.jpg", out);
92-
detectSafeSearch("resources/wakeupcat.jpg", out);
93-
} else if (command.equals("faces")) {
84+
Detect app = new Detect();
85+
if (command.equals("faces")) {
9486
if (path.startsWith("gs://")) {
9587
detectFacesGcs(path, out);
9688
} else {
@@ -155,15 +147,12 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException
155147
}
156148
}
157149

158-
private static ImageAnnotatorClient visionApi;
159-
160150
/**
161151
* Constructs a {@link Detect} which connects to the Cloud Vision API.
162152
*
163153
* @param client The Vision API client.
164154
*/
165-
public Detect(ImageAnnotatorClient client) {
166-
visionApi = client;
155+
public Detect() {
167156
}
168157

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

187-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
176+
BatchAnnotateImagesResponse response =
177+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
188178
List<AnnotateImageResponse> responses = response.getResponsesList();
189179

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

274-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
264+
BatchAnnotateImagesResponse response =
265+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
275266
List<AnnotateImageResponse> responses = response.getResponsesList();
276267

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

307-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
298+
BatchAnnotateImagesResponse response =
299+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
308300
List<AnnotateImageResponse> responses = response.getResponsesList();
309301

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

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

340-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
333+
BatchAnnotateImagesResponse response =
334+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
341335
List<AnnotateImageResponse> responses = response.getResponsesList();
342336

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

374-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
368+
BatchAnnotateImagesResponse response =
369+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
375370
List<AnnotateImageResponse> responses = response.getResponsesList();
376371

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

408-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
403+
BatchAnnotateImagesResponse response =
404+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
409405
List<AnnotateImageResponse> responses = response.getResponsesList();
410406

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

443-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
439+
BatchAnnotateImagesResponse response =
440+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
444441
List<AnnotateImageResponse> responses = response.getResponsesList();
445442

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

476-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
473+
BatchAnnotateImagesResponse response =
474+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
477475
List<AnnotateImageResponse> responses = response.getResponsesList();
478476

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

510-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
508+
BatchAnnotateImagesResponse response =
509+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
511510
List<AnnotateImageResponse> responses = response.getResponsesList();
512511

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

544-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
543+
BatchAnnotateImagesResponse response =
544+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
545545
List<AnnotateImageResponse> responses = response.getResponsesList();
546546

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

579-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
579+
BatchAnnotateImagesResponse response =
580+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
580581
List<AnnotateImageResponse> responses = response.getResponsesList();
581582

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

618-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
619+
BatchAnnotateImagesResponse response =
620+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
619621
List<AnnotateImageResponse> responses = response.getResponsesList();
620622

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

658-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
660+
BatchAnnotateImagesResponse response =
661+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
659662
List<AnnotateImageResponse> responses = response.getResponsesList();
660663

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

695-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
698+
BatchAnnotateImagesResponse response =
699+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
696700
List<AnnotateImageResponse> responses = response.getResponsesList();
697701

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

733-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
737+
BatchAnnotateImagesResponse response =
738+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
734739
List<AnnotateImageResponse> responses = response.getResponsesList();
735740

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

784-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
789+
BatchAnnotateImagesResponse response =
790+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
785791
List<AnnotateImageResponse> responses = response.getResponsesList();
786792

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

836-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
842+
BatchAnnotateImagesResponse response =
843+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
837844
List<AnnotateImageResponse> responses = response.getResponsesList();
838845

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

870-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
877+
BatchAnnotateImagesResponse response =
878+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
871879
List<AnnotateImageResponse> responses = response.getResponsesList();
872880

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

905-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
913+
BatchAnnotateImagesResponse response =
914+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
906915
List<AnnotateImageResponse> responses = response.getResponsesList();
907916

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

958-
BatchAnnotateImagesResponse response = visionApi.batchAnnotateImages(requests);
967+
BatchAnnotateImagesResponse response =
968+
ImageAnnotatorClient.create().batchAnnotateImages(requests);
959969
List<AnnotateImageResponse> responses = response.getResponsesList();
960970

961971
for (AnnotateImageResponse res : responses) {

vision/snippets/src/test/java/com/example/vision/DetectIT.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

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

21-
import com.google.cloud.vision.spi.v1.ImageAnnotatorClient;
2221
import org.junit.After;
2322
import org.junit.Before;
2423
import org.junit.Test;
@@ -42,7 +41,7 @@ public void setUp() throws IOException {
4241
bout = new ByteArrayOutputStream();
4342
out = new PrintStream(bout);
4443
System.setOut(out);
45-
app = new Detect(ImageAnnotatorClient.create());
44+
app = new Detect();
4645
}
4746

4847
@After

0 commit comments

Comments
 (0)