Skip to content

Commit

Permalink
Add system test for Vision Logo detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Nov 30, 2016
1 parent 68ccb76 commit bd0270b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions system_tests/attempt_system_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
'datastore',
'storage',
'speech',
'vision',
'bigquery',
'pubsub',
'language',
Expand Down
Binary file added system_tests/data/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions system_tests/run_system_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import storage
import system_test_utils
import translate
import vision


TEST_MODULES = {
'datastore': datastore,
'speech': speech,
'vision': vision,
'storage': storage,
'pubsub': pubsub,
'bigquery': bigquery,
Expand Down
98 changes: 98 additions & 0 deletions system_tests/vision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""System tests for Vision API."""

import os
import unittest

from google.cloud import exceptions
from google.cloud import storage
from google.cloud import vision

from system_test_utils import unique_resource_id
from retry import RetryErrors


class Config(object):
CLIENT = None
TEST_BUCKET = None

LOGO_FILE = os.path.join(os.path.dirname(__file__), 'data', 'logo.png')


def setUpModule():
Config.CLIENT = vision.Client()
storage_client = storage.Client()
bucket_name = 'new' + unique_resource_id()
Config.TEST_BUCKET = storage_client.bucket(bucket_name)
# 429 Too Many Requests in case API requests rate-limited.
retry_429 = RetryErrors(exceptions.TooManyRequests)
retry_429(Config.TEST_BUCKET.create)()


def tearDownModule():
# 409 Conflict if the bucket is full.
# 429 Too Many Requests in case API requests rate-limited.
bucket_retry = RetryErrors(
(exceptions.TooManyRequests, exceptions.Conflict))
bucket_retry(Config.TEST_BUCKET.delete)(force=True)


class TestSpeechClient(unittest.TestCase):
def setUp(self):
self.to_delete_by_case = []

def tearDown(self):
for value in self.to_delete_by_case:
value.delete()

def _assert_logo(self, logo):
self.assertEqual(logo.description, 'Google')
self.assertEqual(len(logo.bounds.vertices), 4)
self.assertEqual(logo.bounds.vertices[0].x_coordinate, 40)
self.assertEqual(logo.bounds.vertices[0].y_coordinate, 40)
self.assertEqual(logo.bounds.vertices[1].x_coordinate, 959)
self.assertEqual(logo.bounds.vertices[1].y_coordinate, 40)
self.assertEqual(logo.bounds.vertices[2].x_coordinate, 959)
self.assertEqual(logo.bounds.vertices[2].y_coordinate, 302)
self.assertEqual(logo.bounds.vertices[3].x_coordinate, 40)
self.assertEqual(logo.bounds.vertices[3].y_coordinate, 302)
self.assertTrue(logo.score > 0.25)

def test_detect_logos_content(self):
client = Config.CLIENT
with open(Config.LOGO_FILE, 'rb') as image_file:
image = client.image(content=image_file.read())
logos = image.detect_logos()
self.assertEqual(len(logos), 1)
logo = logos[0]
self._assert_logo(logo)

def test_detect_logos_gcs(self):
bucket_name = Config.TEST_BUCKET.name
blob_name = 'logo.png'
blob = Config.TEST_BUCKET.blob(blob_name)
self.to_delete_by_case.append(blob) # Clean-up.
with open(Config.LOGO_FILE, 'rb') as file_obj:
blob.upload_from_file(file_obj)

source_uri = 'gs://%s/%s' % (bucket_name, blob_name)

client = Config.CLIENT
image = client.image(source_uri=source_uri)
logos = image.detect_logos()
self.assertEqual(len(logos), 1)
logo = logos[0]
self._assert_logo(logo)

0 comments on commit bd0270b

Please sign in to comment.