|
15 | 15 | """System tests for Vision API.""" |
16 | 16 |
|
17 | 17 | import io |
| 18 | +import json |
18 | 19 | import os |
| 20 | +import time |
19 | 21 | import unittest |
20 | 22 |
|
21 | 23 | from google.cloud import exceptions |
@@ -120,6 +122,62 @@ def test_detect_logos_gcs(self): |
120 | 122 | assert len(response.logo_annotations) == 1 |
121 | 123 | assert response.logo_annotations[0].description == "google" |
122 | 124 |
|
| 125 | + def test_detect_logos_async(self): |
| 126 | + # Upload the image to Google Cloud Storage. |
| 127 | + blob_name = "logo_async.png" |
| 128 | + blob = self.test_bucket.blob(blob_name) |
| 129 | + self.to_delete_by_case.append(blob) |
| 130 | + with io.open(LOGO_FILE, "rb") as image_file: |
| 131 | + blob.upload_from_file(image_file) |
| 132 | + |
| 133 | + # Make the request. |
| 134 | + request = { |
| 135 | + "image": { |
| 136 | + "source": { |
| 137 | + "image_uri": "gs://{bucket}/{blob}".format( |
| 138 | + bucket=self.test_bucket.name, blob=blob_name |
| 139 | + ) |
| 140 | + } |
| 141 | + }, |
| 142 | + "features": [{"type": vision.enums.Feature.Type.LOGO_DETECTION}], |
| 143 | + } |
| 144 | + method_name = "test_detect_logos_async" |
| 145 | + output_gcs_uri_prefix = "gs://{bucket}/{method_name}".format( |
| 146 | + bucket=self.test_bucket.name, method_name=method_name |
| 147 | + ) |
| 148 | + output_config = {"gcs_destination": {"uri": output_gcs_uri_prefix}} |
| 149 | + response = self.client.async_batch_annotate_images([request], output_config) |
| 150 | + |
| 151 | + # Wait for the operation to complete. |
| 152 | + lro_waiting_seconds = 60 |
| 153 | + start_time = time.time() |
| 154 | + while not response.done() and (time.time() - start_time) < lro_waiting_seconds: |
| 155 | + time.sleep(1) |
| 156 | + |
| 157 | + if not response.done(): |
| 158 | + self.fail( |
| 159 | + "{method_name} timed out after {lro_waiting_seconds} seconds".format( |
| 160 | + method_name=method_name, lro_waiting_seconds=lro_waiting_seconds |
| 161 | + ) |
| 162 | + ) |
| 163 | + |
| 164 | + # Make sure getting the result is not an error. |
| 165 | + response.result() |
| 166 | + |
| 167 | + # There should be exactly 1 output file in gcs at the prefix output_gcs_uri_prefix. |
| 168 | + blobs = list(self.test_bucket.list_blobs(prefix=method_name)) |
| 169 | + assert len(blobs) == 1 |
| 170 | + blob = blobs[0] |
| 171 | + |
| 172 | + # Download the output file and verify the result |
| 173 | + result_str = blob.download_as_string().decode("utf8") |
| 174 | + result = json.loads(result_str) |
| 175 | + responses = result["responses"] |
| 176 | + assert len(responses) == 1 |
| 177 | + logo_annotations = responses[0]["logoAnnotations"] |
| 178 | + assert len(logo_annotations) == 1 |
| 179 | + assert logo_annotations[0]["description"] == "google" |
| 180 | + |
123 | 181 |
|
124 | 182 | @unittest.skipUnless(PROJECT_ID, "PROJECT_ID not set in environment.") |
125 | 183 | class TestVisionClientProductSearch(VisionSystemTestBase): |
|
0 commit comments