From cf6be3ec0eb20af61fae59db5dae523ad7f2ce9b Mon Sep 17 00:00:00 2001 From: shollyman Date: Tue, 11 Jun 2019 16:50:28 -0700 Subject: [PATCH] samples: vision: address flakes due to collisions. (#1458) * vision: address flakes due to collisions. * beta: convert batch future to actually wait for output properly. * explicit imports * additional explicit import --- .../vision/AsyncBatchAnnotateImagesGcs.java | 18 +++++++++--------- .../test/java/com/example/vision/DetectIT.java | 7 ++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/vision/snippets/src/main/java/com/example/vision/AsyncBatchAnnotateImagesGcs.java b/vision/snippets/src/main/java/com/example/vision/AsyncBatchAnnotateImagesGcs.java index 8454227621b..5fc863ffde3 100644 --- a/vision/snippets/src/main/java/com/example/vision/AsyncBatchAnnotateImagesGcs.java +++ b/vision/snippets/src/main/java/com/example/vision/AsyncBatchAnnotateImagesGcs.java @@ -17,7 +17,7 @@ package com.example.vision; // [START vision_async_batch_annotate_images_beta] -import com.google.api.core.ApiFuture; +import com.google.api.gax.longrunning.OperationFuture; import com.google.api.gax.paging.Page; import com.google.cloud.storage.Blob; import com.google.cloud.storage.Bucket; @@ -26,17 +26,17 @@ import com.google.cloud.storage.StorageOptions; import com.google.cloud.vision.v1p4beta1.AnnotateImageRequest; import com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesRequest; -import com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse.Builder; +import com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesResponse; import com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse.Builder; import com.google.cloud.vision.v1p4beta1.Feature; import com.google.cloud.vision.v1p4beta1.Feature.Type; import com.google.cloud.vision.v1p4beta1.GcsDestination; import com.google.cloud.vision.v1p4beta1.Image; import com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient; import com.google.cloud.vision.v1p4beta1.ImageSource; +import com.google.cloud.vision.v1p4beta1.OperationMetadata; import com.google.cloud.vision.v1p4beta1.OutputConfig; - -import com.google.longrunning.Operation; import com.google.protobuf.util.JsonFormat; import java.util.ArrayList; import java.util.List; @@ -51,7 +51,6 @@ public static void asyncBatchAnnotateImagesGcs(String gcsSourcePath, String gcsD throws Exception { // String gcsSourcePath = "gs://YOUR_BUCKET_ID/path_to_your_data"; // String gcsDestinationPath = "gs://YOUR_BUCKET_ID/path_to_store_annotation"; - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { List requests = new ArrayList<>(); @@ -87,13 +86,14 @@ public static void asyncBatchAnnotateImagesGcs(String gcsSourcePath, String gcsD .setOutputConfig(outputConfig) .build(); - ApiFuture future = client.asyncBatchAnnotateImagesCallable().futureCall(request); - // Wait for the request to finish. (The result is not used, since the API saves the result to - // the specified location on GCS.) - Operation response = future.get(180, TimeUnit.SECONDS); + OperationFuture response = + client.asyncBatchAnnotateImagesAsync(request); System.out.println("Waiting for the operation to finish."); + // we're not processing the response, since we'll be reading the output from GCS. + response.get(180, TimeUnit.SECONDS); + // Once the request has completed and the output has been // written to GCS, we can list all the output files. Storage storage = StorageOptions.getDefaultInstance().getService(); diff --git a/vision/snippets/src/test/java/com/example/vision/DetectIT.java b/vision/snippets/src/test/java/com/example/vision/DetectIT.java index 65d492fe5d1..544c6f93173 100644 --- a/vision/snippets/src/test/java/com/example/vision/DetectIT.java +++ b/vision/snippets/src/test/java/com/example/vision/DetectIT.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; @@ -33,9 +34,9 @@ @SuppressWarnings("checkstyle:abbreviationaswordinname") public class DetectIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - // private static final String BUCKET = PROJECT_ID; private static final String BUCKET = "java-docs-samples-testing"; - private static final String OUTPUT_PREFIX = "OCR_PDF_TEST_OUTPUT"; + private static final String OUTPUT_BUCKET = PROJECT_ID; + private static final String OUTPUT_PREFIX = "OUTPUT_VISION_BETA_" + UUID.randomUUID().toString(); private ByteArrayOutputStream bout; private PrintStream out; private Detect app; @@ -125,7 +126,7 @@ public void testAsyncBatchAnnotateImagesGcs() throws Exception { // Act AsyncBatchAnnotateImagesGcs.asyncBatchAnnotateImagesGcs( "gs://cloud-samples-data/vision/label/wakeupcat.jpg", - "gs://" + BUCKET + "/" + OUTPUT_PREFIX + "/"); + "gs://" + OUTPUT_BUCKET + "/" + OUTPUT_PREFIX + "/"); // Assert String got = bout.toString();