Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vision: address flakes due to collisions. #1458

Merged
merged 5 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 @@ -42,7 +43,7 @@ public class DetectIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String ASSET_BUCKET = "cloud-samples-data";
private static final String OUTPUT_BUCKET = PROJECT_ID;
private static final String OUTPUT_PREFIX = "OCR_PDF_TEST_OUTPUT";
private static final String OUTPUT_PREFIX = "OCR_PDF_TEST_OUTPUT_" + UUID.randomUUID().toString();

@Before
public void setUp() throws IOException {
Expand Down Expand Up @@ -363,13 +364,13 @@ public void testDetectDocumentsGcs() throws Exception {

// Assert
String got = bout.toString();

assertThat(got).contains("OIL, GAS AND MINERAL LEASE");

Storage storage = StorageOptions.getDefaultInstance().getService();

Page<Blob> blobs = storage.list(OUTPUT_BUCKET, BlobListOption.currentDirectory(),
BlobListOption.prefix(OUTPUT_PREFIX + "/"));

for (Blob blob : blobs.iterateAll()) {
blob.delete();
}
Expand Down Expand Up @@ -397,4 +398,4 @@ public void testDetectLocalizedObjectsGcs() throws Exception {
String got = bout.toString().toLowerCase();
assertThat(got).contains("dog");
}
}
}