Skip to content

Commit

Permalink
samples: vision: address flakes due to collisions. (#1458)
Browse files Browse the repository at this point in the history
* vision: address flakes due to collisions.

* beta: convert batch future to actually wait for output properly.

* explicit imports

* additional explicit import
  • Loading branch information
shollyman authored and kurtisvg committed Jun 11, 2019
1 parent 6bcea63 commit cf6be3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<AnnotateImageRequest> requests = new ArrayList<>();

Expand Down Expand Up @@ -87,13 +86,14 @@ public static void asyncBatchAnnotateImagesGcs(String gcsSourcePath, String gcsD
.setOutputConfig(outputConfig)
.build();

ApiFuture<Operation> 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<AsyncBatchAnnotateImagesResponse, OperationMetadata> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cf6be3e

Please sign in to comment.