Skip to content
Prev Previous commit
Next Next commit
use the correct code snippet for web detect samples
  • Loading branch information
dizcology committed Dec 4, 2017
commit d29b70d38316ef3051bd119f34c788522491d63f
233 changes: 131 additions & 102 deletions vision/cloud-client/detect/beta_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,81 +31,7 @@
# [END imports]


# [START vision_web_entities_uri]
def web_entities_uri(uri):
client = vision.ImageAnnotatorClient()

image = vision.types.Image()
image.source.image_uri = uri

response = client.web_detection(image=image)

for entity in response.web_detection.web_entities:
print(u'\nDescription: {}'.format(entity.description))
print('Score: {}'.format(entity.score))
# [END vision_web_entities_uri]


# [START vision_web_entities_file]
def web_entities_file(path):
client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as image_file:
content = image_file.read()

image = vision.types.Image(content=content)

response = client.web_detection(image=image)

for entity in response.web_detection.web_entities:
print(u'\nDescription: {}'.format(entity.description))
print('Score: {}'.format(entity.score))
# [END vision_web_entities_file]


# [START vision_web_entities_include_geo_results_uri]
def web_entities_include_geo_results_uri(uri):
client = vision.ImageAnnotatorClient()

image = vision.types.Image()
image.source.image_uri = uri

web_detection_params = vision.types.WebDetectionParams(
include_geo_results=True)
image_context = vision.types.ImageContext(
web_detection_params=web_detection_params)

response = client.web_detection(image=image, image_context=image_context)

for entity in response.web_detection.web_entities:
print(u'\nDescription: {}'.format(entity.description))
print('Score: {}'.format(entity.score))
# [END vision_web_entities_include_geo_results_uri]


# [START vision_web_entities_include_geo_results_file]
def web_entities_include_geo_results_file(path):
client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as image_file:
content = image_file.read()

image = vision.types.Image(content=content)

web_detection_params = vision.types.WebDetectionParams(
include_geo_results=True)
image_context = vision.types.ImageContext(
web_detection_params=web_detection_params)

response = client.web_detection(image=image, image_context=image_context)

for entity in response.web_detection.web_entities:
print(u'\nDescription: {}'.format(entity.description))
print('Score: {}'.format(entity.score))
# [END vision_web_entities_include_geo_results_file]


# [START def_detect_document]
# [START vision_detect_document]
def detect_document(path):
"""Detects document features in an image."""
client = vision.ImageAnnotatorClient()
Expand Down Expand Up @@ -141,10 +67,10 @@ def detect_document(path):

print(u'Block Content: {}\n'.format(block_text))
print(u'Block Confidence:\n {}\n'.format(block.confidence))
# [END def_detect_document]
# [END vision_detect_document]


# [START def_detect_document_uri]
# [START vision_detect_document_uri]
def detect_document_uri(uri):
"""Detects document features in the file located in Google Cloud
Storage."""
Expand Down Expand Up @@ -178,10 +104,10 @@ def detect_document_uri(uri):

print(u'Block Content: {}\n'.format(block_text))
print(u'Block Confidence:\n {}\n'.format(block.confidence))
# [END def_detect_document_uri]
# [END vision_detect_document_uri]


# [START def_detect_safe_search]
# [START vision_detect_safe_search]
def detect_safe_search(path):
"""Detects unsafe features in the file."""
client = vision.ImageAnnotatorClient()
Expand All @@ -204,10 +130,10 @@ 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]))
# [END def_detect_safe_search]
# [END vision_detect_safe_search]


# [START def_detect_safe_search_uri]
# [START vision_detect_safe_search_uri]
def detect_safe_search_uri(uri):
"""Detects unsafe features in the file located in Google Cloud Storage or
on the Web."""
Expand All @@ -228,34 +154,74 @@ 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]))
# [END def_detect_safe_search_uri]
# [END vision_detect_safe_search_uri]


def annotate(path):
"""Returns web annotations given the path to an image."""
# [START get_annotations]
# [START vision_detect_web]
def detect_web(path):
"""Detects web annotations given an image."""
client = vision.ImageAnnotatorClient()

if path.startswith('http') or path.startswith('gs:'):
image = vision.types.Image()
image.source.image_uri = path

else:
with io.open(path, 'rb') as image_file:
content = image_file.read()
with io.open(path, 'rb') as image_file:
content = image_file.read()

image = vision.types.Image(content=content)
image = vision.types.Image(content=content)

response = client.web_detection(image=image)
web_detection = response.web_detection
# [END get_annotations]
annotations = response.web_detection

if annotations.best_guess_labels:
for label in annotations.best_guess_labels:
print('\nBest guess label: {}'.format(label.label))

return web_detection
if annotations.pages_with_matching_images:
print('\n{} Pages with matching images found:'.format(
len(annotations.pages_with_matching_images)))

for page in annotations.pages_with_matching_images:
print('\n\tPage url : {}'.format(page.url))

if page.full_matching_images:
print('\t{} Full Matches found: '.format(
len(page.full_matching_images)))

for image in page.full_matching_images:
print('\t\tImage url : {}'.format(image.url))

if page.partial_matching_images:
print('\t{} Partial Matches found: '.format(
len(page.partial_matching_images)))

for image in page.partial_matching_images:
print('\t\tImage url : {}'.format(image.url))

if annotations.web_entities:
print('\n{} Web entities found: '.format(
len(annotations.web_entities)))

for entity in annotations.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))

if annotations.visually_similar_images:
print('\n{} visually similar images found:\n'.format(
len(annotations.visually_similar_images)))

for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))
# [END vision_detect_web]


# [START vision_detect_web_uri]
def detect_web_uri(uri):
"""Detects web annotations in the file located in Google Cloud Storage."""
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri

response = client.web_detection(image=image)
annotations = response.web_detection

def report(annotations):
"""Prints detected features in the provided web annotations."""
# [START print_annotations]
if annotations.best_guess_labels:
for label in annotations.best_guess_labels:
print('\nBest guess label: {}'.format(label.label))
Expand Down Expand Up @@ -287,15 +253,72 @@ def report(annotations):

for entity in annotations.web_entities:
print('\n\tScore : {}'.format(entity.score))
print('\tDescription: {}'.format(entity.description))
print(u'\tDescription: {}'.format(entity.description))

if annotations.visually_similar_images:
print('\n{} visually similar images found:\n'.format(
len(annotations.visually_similar_images)))

for image in annotations.visually_similar_images:
print('\tImage url : {}'.format(image.url))
# [END print_annotations]
# [END vision_detect_web_uri]


def web_entities(path):
client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as image_file:
content = image_file.read()

image = vision.types.Image(content=content)

response = client.web_detection(image=image)

for entity in response.web_detection.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))


# [START vision_web_entities_include_geo_results]
def web_entities_include_geo_results(path):
client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as image_file:
content = image_file.read()

image = vision.types.Image(content=content)

web_detection_params = vision.types.WebDetectionParams(
include_geo_results=True)
image_context = vision.types.ImageContext(
web_detection_params=web_detection_params)

response = client.web_detection(image=image, image_context=image_context)

for entity in response.web_detection.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))
# [END vision_web_entities_include_geo_results]


# [START vision_web_entities_include_geo_results_uri]
def web_entities_include_geo_results_uri(uri):
client = vision.ImageAnnotatorClient()

image = vision.types.Image()
image.source.image_uri = uri

web_detection_params = vision.types.WebDetectionParams(
include_geo_results=True)
image_context = vision.types.ImageContext(
web_detection_params=web_detection_params)

response = client.web_detection(image=image, image_context=image_context)

for entity in response.web_detection.web_entities:
print('\n\tScore : {}'.format(entity.score))
print(u'\tDescription: {}'.format(entity.description))
# [END vision_web_entities_include_geo_results_uri]


if __name__ == '__main__':
Expand Down Expand Up @@ -332,6 +355,10 @@ def report(annotations):
'web-detect')
web_detect_parser.add_argument('path')

web_detect_uri_parser = subparsers.add_parser(
'web-detect-uri')
web_detect_uri_parser.add_argument('uri')

args = parser.parse_args()

if args.command == 'web-entities':
Expand All @@ -347,4 +374,6 @@ def report(annotations):
elif args.command == 'safe-search-uri':
detect_safe_search_uri(args.uri)
elif args.command == 'web-detect':
report(annotate(args.path))
detect_web(args.path)
elif args.command == 'web-detect-uri':
detect_web(args.uri)
13 changes: 6 additions & 7 deletions vision/cloud-client/detect/beta_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
import os

from beta_snippets import (
annotate,
detect_document,
detect_safe_search,
report,
web_entities_file,
web_entities_include_geo_results_file,
detect_web,
web_entities,
web_entities_include_geo_results,
web_entities_include_geo_results_uri
)

Expand All @@ -29,7 +28,7 @@

def test_file_with_geo(capsys):
path = 'resources/city.jpg'
web_entities_include_geo_results_file(path)
web_entities_include_geo_results(path)
out, _ = capsys.readouterr()

assert 'Zepra' in out
Expand All @@ -45,7 +44,7 @@ def test_gcsuri_with_geo(capsys):

def test_file_without_geo(capsys):
path = 'resources/city.jpg'
web_entities_file(path)
web_entities(path)
out, _ = capsys.readouterr()

assert 'Zepra' not in out
Expand All @@ -70,7 +69,7 @@ def test_safe_search(capsys):

def test_detect_file(capsys):
path = 'resources/landmark.jpg'
report(annotate(path))
detect_web(path)
out, _ = capsys.readouterr()

assert 'Description: Palace of Fine Arts Theatre' in out
Expand Down