Skip to content

Commit fe97d4f

Browse files
brendanlundybusunkim96
authored andcommitted
Add a system test for Vision asyncBatchAnnotateImages (googleapis#8197)
1 parent 79a4313 commit fe97d4f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

vision/tests/system.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"""System tests for Vision API."""
1616

1717
import io
18+
import json
1819
import os
20+
import time
1921
import unittest
2022

2123
from google.cloud import exceptions
@@ -120,6 +122,62 @@ def test_detect_logos_gcs(self):
120122
assert len(response.logo_annotations) == 1
121123
assert response.logo_annotations[0].description == "google"
122124

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+
123181

124182
@unittest.skipUnless(PROJECT_ID, "PROJECT_ID not set in environment.")
125183
class TestVisionClientProductSearch(VisionSystemTestBase):

0 commit comments

Comments
 (0)