Skip to content

Commit e6ea013

Browse files
committed
Add system test for Vision Logo detection.
1 parent d775489 commit e6ea013

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

system_tests/attempt_system_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
'datastore',
6161
'storage',
6262
'speech',
63+
'vision',
6364
'bigquery',
6465
'pubsub',
6566
'language',

system_tests/data/logo.png

34.7 KB
Loading

system_tests/run_system_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import storage
2828
import system_test_utils
2929
import translate
30+
import vision
3031

3132

3233
TEST_MODULES = {
3334
'datastore': datastore,
3435
'speech': speech,
36+
'vision': vision,
3537
'storage': storage,
3638
'pubsub': pubsub,
3739
'bigquery': bigquery,

system_tests/vision.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""System tests for Vision API."""
16+
17+
import os
18+
import unittest
19+
20+
from google.cloud import exceptions
21+
from google.cloud import storage
22+
from google.cloud import vision
23+
from google.cloud.vision.entity import EntityAnnotation
24+
25+
from system_test_utils import unique_resource_id
26+
from retry import RetryErrors
27+
28+
29+
_SYS_TESTS_DIR = os.path.abspath(os.path.dirname(__file__))
30+
LOGO_FILE = os.path.join(_SYS_TESTS_DIR, 'data', 'logo.png')
31+
32+
33+
class Config(object):
34+
CLIENT = None
35+
TEST_BUCKET = None
36+
37+
38+
def setUpModule():
39+
Config.CLIENT = vision.Client()
40+
storage_client = storage.Client()
41+
bucket_name = 'new' + unique_resource_id()
42+
Config.TEST_BUCKET = storage_client.bucket(bucket_name)
43+
# 429 Too Many Requests in case API requests rate-limited.
44+
retry_429 = RetryErrors(exceptions.TooManyRequests)
45+
retry_429(Config.TEST_BUCKET.create)()
46+
47+
48+
def tearDownModule():
49+
# 409 Conflict if the bucket is full.
50+
# 429 Too Many Requests in case API requests rate-limited.
51+
bucket_retry = RetryErrors(
52+
(exceptions.TooManyRequests, exceptions.Conflict))
53+
bucket_retry(Config.TEST_BUCKET.delete)(force=True)
54+
55+
56+
class TestVisionClient(unittest.TestCase):
57+
def setUp(self):
58+
self.to_delete_by_case = []
59+
60+
def tearDown(self):
61+
for value in self.to_delete_by_case:
62+
value.delete()
63+
64+
def _assert_logo(self, logo):
65+
self.assertIsInstance(logo, EntityAnnotation)
66+
self.assertEqual(logo.description, 'Google')
67+
self.assertEqual(len(logo.bounds.vertices), 4)
68+
self.assertEqual(logo.bounds.vertices[0].x_coordinate, 40)
69+
self.assertEqual(logo.bounds.vertices[0].y_coordinate, 40)
70+
self.assertEqual(logo.bounds.vertices[1].x_coordinate, 959)
71+
self.assertEqual(logo.bounds.vertices[1].y_coordinate, 40)
72+
self.assertEqual(logo.bounds.vertices[2].x_coordinate, 959)
73+
self.assertEqual(logo.bounds.vertices[2].y_coordinate, 302)
74+
self.assertEqual(logo.bounds.vertices[3].x_coordinate, 40)
75+
self.assertEqual(logo.bounds.vertices[3].y_coordinate, 302)
76+
self.assertTrue(logo.score > 0.25)
77+
78+
def test_detect_logos_content(self):
79+
client = Config.CLIENT
80+
with open(LOGO_FILE, 'rb') as image_file:
81+
image = client.image(content=image_file.read())
82+
logos = image.detect_logos()
83+
self.assertEqual(len(logos), 1)
84+
logo = logos[0]
85+
self._assert_logo(logo)
86+
87+
def test_detect_logos_filename(self):
88+
client = Config.CLIENT
89+
image = client.image(filename=LOGO_FILE)
90+
logos = image.detect_logos()
91+
self.assertEqual(len(logos), 1)
92+
logo = logos[0]
93+
self._assert_logo(logo)
94+
95+
def test_detect_logos_gcs(self):
96+
bucket_name = Config.TEST_BUCKET.name
97+
blob_name = 'logo.png'
98+
blob = Config.TEST_BUCKET.blob(blob_name)
99+
self.to_delete_by_case.append(blob) # Clean-up.
100+
with open(LOGO_FILE, 'rb') as file_obj:
101+
blob.upload_from_file(file_obj)
102+
103+
source_uri = 'gs://%s/%s' % (bucket_name, blob_name)
104+
105+
client = Config.CLIENT
106+
image = client.image(source_uri=source_uri)
107+
logos = image.detect_logos()
108+
self.assertEqual(len(logos), 1)
109+
logo = logos[0]
110+
self._assert_logo(logo)

0 commit comments

Comments
 (0)