2424
2525from system_test_utils import unique_resource_id
2626from retry import RetryErrors
27+ import six
2728
2829
2930_SYS_TESTS_DIR = os .path .abspath (os .path .dirname (__file__ ))
3031LOGO_FILE = os .path .join (_SYS_TESTS_DIR , 'data' , 'logo.png' )
3132FACE_FILE = os .path .join (_SYS_TESTS_DIR , 'data' , 'faces.jpg' )
33+ LABEL_FILE = os .path .join (_SYS_TESTS_DIR , 'data' , 'car.jpg' )
3234
3335
3436class Config (object ):
@@ -54,6 +56,14 @@ def tearDownModule():
5456 bucket_retry (Config .TEST_BUCKET .delete )(force = True )
5557
5658
59+ class BaseVisionTestCase (unittest .TestCase ):
60+ def _assert_coordinate (self , coordinate ):
61+ if coordinate is None :
62+ return
63+ self .assertIsInstance (coordinate , (int , float ))
64+ self .assertNotEqual (coordinate , 0.0 )
65+
66+
5767class TestVisionClientLogo (unittest .TestCase ):
5868 def setUp (self ):
5969 self .to_delete_by_case = []
@@ -111,20 +121,14 @@ def test_detect_logos_gcs(self):
111121 self ._assert_logo (logo )
112122
113123
114- class TestVisionClientFace (unittest . TestCase ):
124+ class TestVisionClientFace (BaseVisionTestCase ):
115125 def setUp (self ):
116126 self .to_delete_by_case = []
117127
118128 def tearDown (self ):
119129 for value in self .to_delete_by_case :
120130 value .delete ()
121131
122- def _assert_coordinate (self , coordinate ):
123- if coordinate is None :
124- return
125- self .assertIsInstance (coordinate , (int , float ))
126- self .assertGreater (abs (coordinate ), 0.0 )
127-
128132 def _assert_likelihood (self , likelihood ):
129133 from google .cloud .vision .likelihood import Likelihood
130134
@@ -215,3 +219,66 @@ def test_detect_faces_filename(self):
215219 self .assertEqual (len (faces ), 5 )
216220 for face in faces :
217221 self ._assert_face (face )
222+
223+
224+ class TestVisionClientLabel (BaseVisionTestCase ):
225+ DESCRIPTIONS = (
226+ 'car' ,
227+ 'vehicle' ,
228+ 'land vehicle' ,
229+ 'automotive design' ,
230+ 'wheel' ,
231+ 'automobile make' ,
232+ 'luxury vehicle' ,
233+ 'sports car' ,
234+ 'performance car' ,
235+ 'automotive exterior' ,
236+ )
237+
238+ def setUp (self ):
239+ self .to_delete_by_case = []
240+
241+ def tearDown (self ):
242+ for value in self .to_delete_by_case :
243+ value .delete ()
244+
245+ def _assert_label (self , label ):
246+
247+ self .assertIsInstance (label , EntityAnnotation )
248+ self .assertIn (label .description , self .DESCRIPTIONS )
249+ self .assertIsInstance (label .mid , six .text_type )
250+ self .assertGreater (label .score , 0.0 )
251+
252+ def test_detect_labels_content (self ):
253+ client = Config .CLIENT
254+ with open (LABEL_FILE , 'rb' ) as image_file :
255+ image = client .image (content = image_file .read ())
256+ labels = image .detect_labels ()
257+ self .assertEqual (len (labels ), 10 )
258+ for label in labels :
259+ self ._assert_label (label )
260+
261+ def test_detect_labels_gcs (self ):
262+ bucket_name = Config .TEST_BUCKET .name
263+ blob_name = 'car.jpg'
264+ blob = Config .TEST_BUCKET .blob (blob_name )
265+ self .to_delete_by_case .append (blob ) # Clean-up.
266+ with open (LABEL_FILE , 'rb' ) as file_obj :
267+ blob .upload_from_file (file_obj )
268+
269+ source_uri = 'gs://%s/%s' % (bucket_name , blob_name )
270+
271+ client = Config .CLIENT
272+ image = client .image (source_uri = source_uri )
273+ labels = image .detect_labels ()
274+ self .assertEqual (len (labels ), 10 )
275+ for label in labels :
276+ self ._assert_label (label )
277+
278+ def test_detect_labels_filename (self ):
279+ client = Config .CLIENT
280+ image = client .image (filename = LABEL_FILE )
281+ labels = image .detect_labels ()
282+ self .assertEqual (len (labels ), 10 )
283+ for label in labels :
284+ self ._assert_label (label )
0 commit comments