Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vision: update samples to throw errors if one occurs #2725

Merged
merged 7 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions vision/cloud-client/detect/beta_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def detect_handwritten_ocr(path):
for symbol in word.symbols:
print('\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))

if response.error.message:
raise Exception(response.error.message)
# [END vision_handwritten_ocr_beta]


Expand Down Expand Up @@ -174,6 +177,9 @@ def detect_handwritten_ocr_uri(uri):
for symbol in word.symbols:
print('\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))

if response.error.message:
raise Exception(response.error.message)
# [END vision_handwritten_ocr_gcs_beta]


Expand Down
66 changes: 66 additions & 0 deletions vision/cloud-client/detect/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def detect_faces(path):
for vertex in face.bounding_poly.vertices])

print('face bounds: {}'.format(','.join(vertices)))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_face_detection]
# [END vision_face_detection]

Expand Down Expand Up @@ -99,6 +102,9 @@ def detect_faces_uri(uri):
for vertex in face.bounding_poly.vertices])

print('face bounds: {}'.format(','.join(vertices)))

if response.error.message:
raise Exception(response.error.message)
# [END vision_face_detection_gcs]


Expand All @@ -121,6 +127,9 @@ def detect_labels(path):

for label in labels:
print(label.description)

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_label_detection]
# [END vision_label_detection]

Expand All @@ -140,6 +149,9 @@ def detect_labels_uri(uri):

for label in labels:
print(label.description)

if response.error.message:
raise Exception(response.error.message)
# [END vision_label_detection_gcs]


Expand All @@ -166,6 +178,9 @@ def detect_landmarks(path):
lat_lng = location.lat_lng
print('Latitude {}'.format(lat_lng.latitude))
print('Longitude {}'.format(lat_lng.longitude))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_landmark_detection]
# [END vision_landmark_detection]

Expand All @@ -185,6 +200,9 @@ def detect_landmarks_uri(uri):

for landmark in landmarks:
print(landmark.description)

if response.error.message:
raise Exception(response.error.message)
# [END vision_landmark_detection_gcs]


Expand All @@ -207,6 +225,9 @@ def detect_logos(path):

for logo in logos:
print(logo.description)

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_logo_detection]
# [END vision_logo_detection]

Expand All @@ -226,6 +247,9 @@ def detect_logos_uri(uri):

for logo in logos:
print(logo.description)

if response.error.message:
raise Exception(response.error.message)
# [END vision_logo_detection_gcs]


Expand Down Expand Up @@ -255,6 +279,9 @@ def detect_safe_search(path):
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
print('violence: {}'.format(likelihood_name[safe.violence]))
print('racy: {}'.format(likelihood_name[safe.racy]))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_safe_search_detection]
# [END vision_safe_search_detection]

Expand All @@ -281,6 +308,9 @@ def detect_safe_search_uri(uri):
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
print('violence: {}'.format(likelihood_name[safe.violence]))
print('racy: {}'.format(likelihood_name[safe.racy]))

if response.error.message:
raise Exception(response.error.message)
# [END vision_safe_search_detection_gcs]


Expand Down Expand Up @@ -308,6 +338,9 @@ def detect_text(path):
for vertex in text.bounding_poly.vertices])

print('bounds: {}'.format(','.join(vertices)))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_text_detection]
# [END vision_text_detection]

Expand All @@ -332,6 +365,9 @@ def detect_text_uri(uri):
for vertex in text.bounding_poly.vertices])

print('bounds: {}'.format(','.join(vertices)))

if response.error.message:
raise Exception(response.error.message)
# [END vision_text_detection_gcs]


Expand All @@ -358,6 +394,9 @@ def detect_properties(path):
print('\tg: {}'.format(color.color.green))
print('\tb: {}'.format(color.color.blue))
print('\ta: {}'.format(color.color.alpha))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_image_properties]
# [END vision_image_property_detection]

Expand All @@ -381,6 +420,9 @@ def detect_properties_uri(uri):
print('\tg: {}'.format(color.color.green))
print('\tb: {}'.format(color.color.blue))
print('\ta: {}'.format(color.color.alpha))

if response.error.message:
raise Exception(response.error.message)
# [END vision_image_property_detection_gcs]


Expand Down Expand Up @@ -439,6 +481,9 @@ def detect_web(path):

for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_web_detection]
# [END vision_web_detection]

Expand Down Expand Up @@ -493,6 +538,9 @@ def detect_web_uri(uri):

for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))

if response.error.message:
raise Exception(response.error.message)
# [END vision_web_detection_gcs]


Expand All @@ -519,6 +567,9 @@ def web_entities_include_geo_results(path):
for entity in response.web_detection.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))

if response.error.message:
raise Exception(response.error.message)
# [END vision_web_detection_include_geo]


Expand All @@ -543,6 +594,9 @@ def web_entities_include_geo_results_uri(uri):
for entity in response.web_detection.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))

if response.error.message:
raise Exception(response.error.message)
# [END vision_web_detection_include_geo_gcs]


Expand Down Expand Up @@ -572,6 +626,9 @@ def detect_crop_hints(path):
for vertex in hint.bounding_poly.vertices])

print('bounds: {}'.format(','.join(vertices)))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_crop_hints]
# [END vision_crop_hint_detection]

Expand All @@ -598,6 +655,9 @@ def detect_crop_hints_uri(uri):
for vertex in hint.bounding_poly.vertices])

print('bounds: {}'.format(','.join(vertices)))

if response.error.message:
raise Exception(response.error.message)
# [END vision_crop_hint_detection_gcs]


Expand Down Expand Up @@ -634,6 +694,9 @@ def detect_document(path):
for symbol in word.symbols:
print('\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))

if response.error.message:
raise Exception(response.error.message)
# [END vision_python_migration_document_text_detection]
# [END vision_fulltext_detection]

Expand Down Expand Up @@ -667,6 +730,9 @@ def detect_document_uri(uri):
for symbol in word.symbols:
print('\tSymbol: {} (confidence: {})'.format(
symbol.text, symbol.confidence))

if response.error.message:
raise Exception(response.error.message)
# [END vision_fulltext_detection_gcs]


Expand Down
2 changes: 1 addition & 1 deletion vision/cloud-client/detect/detect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_labels(capsys):


def test_labels_uri(capsys):
file_name = 'gs://{}/vision/wakeupcat.jpg'.format(ASSET_BUCKET)
file_name = 'gs://{}/vision/label/wakeupcat.jpg'.format(ASSET_BUCKET)
detect.detect_labels_uri(file_name)
out, _ = capsys.readouterr()
assert 'Labels' in out
Expand Down
3 changes: 3 additions & 0 deletions vision/cloud-client/detect/set_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def set_endpoint():

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

if response.error.message:
raise Exception(response.error.message)


if __name__ == '__main__':
set_endpoint()