Skip to content

Commit 9b6717f

Browse files
nnegreyleahecolegguuss
authored
vision: update samples to throw errors if one occurs (#2725)
* vision: update samples to throw errors if one occurs * Add link to error page docs * Add link to error message Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Gus Class <[email protected]>
1 parent a9e1342 commit 9b6717f

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed

vision/cloud-client/detect/beta_snippets.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ def detect_handwritten_ocr(path):
130130
for symbol in word.symbols:
131131
print('\tSymbol: {} (confidence: {})'.format(
132132
symbol.text, symbol.confidence))
133+
134+
if response.error.message:
135+
raise Exception(
136+
'{}\nFor more info on error messages, check: '
137+
'https://cloud.google.com/apis/design/errors'.format(
138+
response.error.message))
133139
# [END vision_handwritten_ocr_beta]
134140

135141

@@ -174,6 +180,12 @@ def detect_handwritten_ocr_uri(uri):
174180
for symbol in word.symbols:
175181
print('\tSymbol: {} (confidence: {})'.format(
176182
symbol.text, symbol.confidence))
183+
184+
if response.error.message:
185+
raise Exception(
186+
'{}\nFor more info on error messages, check: '
187+
'https://cloud.google.com/apis/design/errors'.format(
188+
response.error.message))
177189
# [END vision_handwritten_ocr_gcs_beta]
178190

179191

vision/cloud-client/detect/detect.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def detect_faces(path):
6868
for vertex in face.bounding_poly.vertices])
6969

7070
print('face bounds: {}'.format(','.join(vertices)))
71+
72+
if response.error.message:
73+
raise Exception(
74+
'{}\nFor more info on error messages, check: '
75+
'https://cloud.google.com/apis/design/errors'.format(
76+
response.error.message))
7177
# [END vision_python_migration_face_detection]
7278
# [END vision_face_detection]
7379

@@ -99,6 +105,12 @@ def detect_faces_uri(uri):
99105
for vertex in face.bounding_poly.vertices])
100106

101107
print('face bounds: {}'.format(','.join(vertices)))
108+
109+
if response.error.message:
110+
raise Exception(
111+
'{}\nFor more info on error messages, check: '
112+
'https://cloud.google.com/apis/design/errors'.format(
113+
response.error.message))
102114
# [END vision_face_detection_gcs]
103115

104116

@@ -121,6 +133,12 @@ def detect_labels(path):
121133

122134
for label in labels:
123135
print(label.description)
136+
137+
if response.error.message:
138+
raise Exception(
139+
'{}\nFor more info on error messages, check: '
140+
'https://cloud.google.com/apis/design/errors'.format(
141+
response.error.message))
124142
# [END vision_python_migration_label_detection]
125143
# [END vision_label_detection]
126144

@@ -140,6 +158,12 @@ def detect_labels_uri(uri):
140158

141159
for label in labels:
142160
print(label.description)
161+
162+
if response.error.message:
163+
raise Exception(
164+
'{}\nFor more info on error messages, check: '
165+
'https://cloud.google.com/apis/design/errors'.format(
166+
response.error.message))
143167
# [END vision_label_detection_gcs]
144168

145169

@@ -166,6 +190,12 @@ def detect_landmarks(path):
166190
lat_lng = location.lat_lng
167191
print('Latitude {}'.format(lat_lng.latitude))
168192
print('Longitude {}'.format(lat_lng.longitude))
193+
194+
if response.error.message:
195+
raise Exception(
196+
'{}\nFor more info on error messages, check: '
197+
'https://cloud.google.com/apis/design/errors'.format(
198+
response.error.message))
169199
# [END vision_python_migration_landmark_detection]
170200
# [END vision_landmark_detection]
171201

@@ -185,6 +215,12 @@ def detect_landmarks_uri(uri):
185215

186216
for landmark in landmarks:
187217
print(landmark.description)
218+
219+
if response.error.message:
220+
raise Exception(
221+
'{}\nFor more info on error messages, check: '
222+
'https://cloud.google.com/apis/design/errors'.format(
223+
response.error.message))
188224
# [END vision_landmark_detection_gcs]
189225

190226

@@ -207,6 +243,12 @@ def detect_logos(path):
207243

208244
for logo in logos:
209245
print(logo.description)
246+
247+
if response.error.message:
248+
raise Exception(
249+
'{}\nFor more info on error messages, check: '
250+
'https://cloud.google.com/apis/design/errors'.format(
251+
response.error.message))
210252
# [END vision_python_migration_logo_detection]
211253
# [END vision_logo_detection]
212254

@@ -226,6 +268,12 @@ def detect_logos_uri(uri):
226268

227269
for logo in logos:
228270
print(logo.description)
271+
272+
if response.error.message:
273+
raise Exception(
274+
'{}\nFor more info on error messages, check: '
275+
'https://cloud.google.com/apis/design/errors'.format(
276+
response.error.message))
229277
# [END vision_logo_detection_gcs]
230278

231279

@@ -255,6 +303,12 @@ def detect_safe_search(path):
255303
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
256304
print('violence: {}'.format(likelihood_name[safe.violence]))
257305
print('racy: {}'.format(likelihood_name[safe.racy]))
306+
307+
if response.error.message:
308+
raise Exception(
309+
'{}\nFor more info on error messages, check: '
310+
'https://cloud.google.com/apis/design/errors'.format(
311+
response.error.message))
258312
# [END vision_python_migration_safe_search_detection]
259313
# [END vision_safe_search_detection]
260314

@@ -281,6 +335,12 @@ def detect_safe_search_uri(uri):
281335
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
282336
print('violence: {}'.format(likelihood_name[safe.violence]))
283337
print('racy: {}'.format(likelihood_name[safe.racy]))
338+
339+
if response.error.message:
340+
raise Exception(
341+
'{}\nFor more info on error messages, check: '
342+
'https://cloud.google.com/apis/design/errors'.format(
343+
response.error.message))
284344
# [END vision_safe_search_detection_gcs]
285345

286346

@@ -308,6 +368,12 @@ def detect_text(path):
308368
for vertex in text.bounding_poly.vertices])
309369

310370
print('bounds: {}'.format(','.join(vertices)))
371+
372+
if response.error.message:
373+
raise Exception(
374+
'{}\nFor more info on error messages, check: '
375+
'https://cloud.google.com/apis/design/errors'.format(
376+
response.error.message))
311377
# [END vision_python_migration_text_detection]
312378
# [END vision_text_detection]
313379

@@ -332,6 +398,12 @@ def detect_text_uri(uri):
332398
for vertex in text.bounding_poly.vertices])
333399

334400
print('bounds: {}'.format(','.join(vertices)))
401+
402+
if response.error.message:
403+
raise Exception(
404+
'{}\nFor more info on error messages, check: '
405+
'https://cloud.google.com/apis/design/errors'.format(
406+
response.error.message))
335407
# [END vision_text_detection_gcs]
336408

337409

@@ -358,6 +430,12 @@ def detect_properties(path):
358430
print('\tg: {}'.format(color.color.green))
359431
print('\tb: {}'.format(color.color.blue))
360432
print('\ta: {}'.format(color.color.alpha))
433+
434+
if response.error.message:
435+
raise Exception(
436+
'{}\nFor more info on error messages, check: '
437+
'https://cloud.google.com/apis/design/errors'.format(
438+
response.error.message))
361439
# [END vision_python_migration_image_properties]
362440
# [END vision_image_property_detection]
363441

@@ -381,6 +459,12 @@ def detect_properties_uri(uri):
381459
print('\tg: {}'.format(color.color.green))
382460
print('\tb: {}'.format(color.color.blue))
383461
print('\ta: {}'.format(color.color.alpha))
462+
463+
if response.error.message:
464+
raise Exception(
465+
'{}\nFor more info on error messages, check: '
466+
'https://cloud.google.com/apis/design/errors'.format(
467+
response.error.message))
384468
# [END vision_image_property_detection_gcs]
385469

386470

@@ -439,6 +523,12 @@ def detect_web(path):
439523

440524
for image in annotations.visually_similar_images:
441525
print('\tImage url : {}'.format(image.url))
526+
527+
if response.error.message:
528+
raise Exception(
529+
'{}\nFor more info on error messages, check: '
530+
'https://cloud.google.com/apis/design/errors'.format(
531+
response.error.message))
442532
# [END vision_python_migration_web_detection]
443533
# [END vision_web_detection]
444534

@@ -493,6 +583,12 @@ def detect_web_uri(uri):
493583

494584
for image in annotations.visually_similar_images:
495585
print('\tImage url : {}'.format(image.url))
586+
587+
if response.error.message:
588+
raise Exception(
589+
'{}\nFor more info on error messages, check: '
590+
'https://cloud.google.com/apis/design/errors'.format(
591+
response.error.message))
496592
# [END vision_web_detection_gcs]
497593

498594

@@ -519,6 +615,12 @@ def web_entities_include_geo_results(path):
519615
for entity in response.web_detection.web_entities:
520616
print('\n\tScore : {}'.format(entity.score))
521617
print(u'\tDescription: {}'.format(entity.description))
618+
619+
if response.error.message:
620+
raise Exception(
621+
'{}\nFor more info on error messages, check: '
622+
'https://cloud.google.com/apis/design/errors'.format(
623+
response.error.message))
522624
# [END vision_web_detection_include_geo]
523625

524626

@@ -543,6 +645,12 @@ def web_entities_include_geo_results_uri(uri):
543645
for entity in response.web_detection.web_entities:
544646
print('\n\tScore : {}'.format(entity.score))
545647
print(u'\tDescription: {}'.format(entity.description))
648+
649+
if response.error.message:
650+
raise Exception(
651+
'{}\nFor more info on error messages, check: '
652+
'https://cloud.google.com/apis/design/errors'.format(
653+
response.error.message))
546654
# [END vision_web_detection_include_geo_gcs]
547655

548656

@@ -572,6 +680,12 @@ def detect_crop_hints(path):
572680
for vertex in hint.bounding_poly.vertices])
573681

574682
print('bounds: {}'.format(','.join(vertices)))
683+
684+
if response.error.message:
685+
raise Exception(
686+
'{}\nFor more info on error messages, check: '
687+
'https://cloud.google.com/apis/design/errors'.format(
688+
response.error.message))
575689
# [END vision_python_migration_crop_hints]
576690
# [END vision_crop_hint_detection]
577691

@@ -598,6 +712,12 @@ def detect_crop_hints_uri(uri):
598712
for vertex in hint.bounding_poly.vertices])
599713

600714
print('bounds: {}'.format(','.join(vertices)))
715+
716+
if response.error.message:
717+
raise Exception(
718+
'{}\nFor more info on error messages, check: '
719+
'https://cloud.google.com/apis/design/errors'.format(
720+
response.error.message))
601721
# [END vision_crop_hint_detection_gcs]
602722

603723

@@ -634,6 +754,12 @@ def detect_document(path):
634754
for symbol in word.symbols:
635755
print('\tSymbol: {} (confidence: {})'.format(
636756
symbol.text, symbol.confidence))
757+
758+
if response.error.message:
759+
raise Exception(
760+
'{}\nFor more info on error messages, check: '
761+
'https://cloud.google.com/apis/design/errors'.format(
762+
response.error.message))
637763
# [END vision_python_migration_document_text_detection]
638764
# [END vision_fulltext_detection]
639765

@@ -667,6 +793,12 @@ def detect_document_uri(uri):
667793
for symbol in word.symbols:
668794
print('\tSymbol: {} (confidence: {})'.format(
669795
symbol.text, symbol.confidence))
796+
797+
if response.error.message:
798+
raise Exception(
799+
'{}\nFor more info on error messages, check: '
800+
'https://cloud.google.com/apis/design/errors'.format(
801+
response.error.message))
670802
# [END vision_fulltext_detection_gcs]
671803

672804

vision/cloud-client/detect/detect_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_labels(capsys):
3737

3838

3939
def test_labels_uri(capsys):
40-
file_name = 'gs://{}/vision/wakeupcat.jpg'.format(ASSET_BUCKET)
40+
file_name = 'gs://{}/vision/label/wakeupcat.jpg'.format(ASSET_BUCKET)
4141
detect.detect_labels_uri(file_name)
4242
out, _ = capsys.readouterr()
4343
assert 'Labels' in out

vision/cloud-client/detect/set_endpoint.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def set_endpoint():
3737

3838
print('bounds: {}\n'.format(','.join(vertices)))
3939

40+
if response.error.message:
41+
raise Exception(
42+
'{}\nFor more info on error messages, check: '
43+
'https://cloud.google.com/apis/design/errors'.format(
44+
response.error.message))
45+
4046

4147
if __name__ == '__main__':
4248
set_endpoint()

0 commit comments

Comments
 (0)