diff --git a/vision/cloud-client/crop_hints/requirements.txt b/vision/cloud-client/crop_hints/requirements.txt index fb98495c164..c09c005e1d0 100644 --- a/vision/cloud-client/crop_hints/requirements.txt +++ b/vision/cloud-client/crop_hints/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-vision==0.28.0 +google-cloud-vision==0.29.0 pillow==4.3.0 diff --git a/vision/cloud-client/detect/beta_snippets.py b/vision/cloud-client/detect/beta_snippets.py new file mode 100644 index 00000000000..feb881568d9 --- /dev/null +++ b/vision/cloud-client/detect/beta_snippets.py @@ -0,0 +1,379 @@ +#!/usr/bin/env python + +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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. + +"""Demonstrates beta features using the Google Cloud Vision API. + +Example usage: + python beta_snippets.py web-entities resources/city.jpg + python beta_snippets.py detect-document resources/text.jpg + python beta_snippets.py safe-search resources/wakeupcat.jpg + python beta_snippets.py web-detect resources/landmark.jpg +""" + +# [START imports] +import argparse +import io + +from google.cloud import vision_v1p1beta1 as vision +# [END imports] + + +# [START vision_detect_document] +def detect_document(path): + """Detects document features in an image.""" + client = vision.ImageAnnotatorClient() + + with io.open(path, 'rb') as image_file: + content = image_file.read() + + image = vision.types.Image(content=content) + + response = client.document_text_detection(image=image) + + for page in response.full_text_annotation.pages: + for block in page.blocks: + block_words = [] + for paragraph in block.paragraphs: + block_words.extend(paragraph.words) + print(u'Paragraph Confidence: {}\n'.format( + paragraph.confidence)) + + block_text = '' + block_symbols = [] + for word in block_words: + block_symbols.extend(word.symbols) + word_text = '' + for symbol in word.symbols: + word_text = unicode(word_text) + unicode(symbol.text) + print(u'\tSymbol text: {} (confidence: {})'.format( + symbol.text, symbol.confidence)) + print(u'Word text: {} (confidence: {})\n'.format( + word_text, word.confidence)) + + block_text += ' ' + word_text + + print(u'Block Content: {}\n'.format(block_text)) + print(u'Block Confidence:\n {}\n'.format(block.confidence)) +# [END vision_detect_document] + + +# [START vision_detect_document_uri] +def detect_document_uri(uri): + """Detects document features in the file located in Google Cloud + Storage.""" + client = vision.ImageAnnotatorClient() + image = vision.types.Image() + image.source.image_uri = uri + + response = client.document_text_detection(image=image) + + for page in response.full_text_annotation.pages: + for block in page.blocks: + block_words = [] + for paragraph in block.paragraphs: + block_words.extend(paragraph.words) + print(u'Paragraph Confidence: {}\n'.format( + paragraph.confidence)) + + block_text = '' + block_symbols = [] + for word in block_words: + block_symbols.extend(word.symbols) + word_text = '' + for symbol in word.symbols: + word_text = unicode(word_text) + unicode(symbol.text) + print(u'\tSymbol text: {} (confidence: {})'.format( + symbol.text, symbol.confidence)) + print(u'Word text: {} (confidence: {})\n'.format( + word_text, word.confidence)) + + block_text += ' ' + word_text + + print(u'Block Content: {}\n'.format(block_text)) + print(u'Block Confidence:\n {}\n'.format(block.confidence)) +# [END vision_detect_document_uri] + + +# [START vision_detect_safe_search] +def detect_safe_search(path): + """Detects unsafe features in the file.""" + client = vision.ImageAnnotatorClient() + + with io.open(path, 'rb') as image_file: + content = image_file.read() + + image = vision.types.Image(content=content) + + response = client.safe_search_detection(image=image) + safe = response.safe_search_annotation + + # Names of likelihood from google.cloud.vision.enums + likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', + 'LIKELY', 'VERY_LIKELY') + print('Safe search:') + + print('adult: {}'.format(likelihood_name[safe.adult])) + print('medical: {}'.format(likelihood_name[safe.medical])) + print('spoofed: {}'.format(likelihood_name[safe.spoof])) + print('violence: {}'.format(likelihood_name[safe.violence])) + print('racy: {}'.format(likelihood_name[safe.racy])) +# [END vision_detect_safe_search] + + +# [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.""" + client = vision.ImageAnnotatorClient() + image = vision.types.Image() + image.source.image_uri = uri + + response = client.safe_search_detection(image=image) + safe = response.safe_search_annotation + + # Names of likelihood from google.cloud.vision.enums + likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', + 'LIKELY', 'VERY_LIKELY') + print('Safe search:') + + print('adult: {}'.format(likelihood_name[safe.adult])) + print('medical: {}'.format(likelihood_name[safe.medical])) + print('spoofed: {}'.format(likelihood_name[safe.spoof])) + print('violence: {}'.format(likelihood_name[safe.violence])) + print('racy: {}'.format(likelihood_name[safe.racy])) +# [END vision_detect_safe_search_uri] + + +# [START vision_detect_web] +def detect_web(path): + """Detects web annotations given an image.""" + 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) + annotations = response.web_detection + + if annotations.best_guess_labels: + for label in annotations.best_guess_labels: + print('\nBest guess label: {}'.format(label.label)) + + 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 + + if annotations.best_guess_labels: + for label in annotations.best_guess_labels: + print('\nBest guess label: {}'.format(label.label)) + + 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_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__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + subparsers = parser.add_subparsers(dest='command') + + web_entities_parser = subparsers.add_parser( + 'web-entities') + web_entities_parser.add_argument('path') + + web_entities_uri_parser = subparsers.add_parser( + 'web-entities-uri') + web_entities_uri_parser.add_argument('uri') + + detect_document_parser = subparsers.add_parser( + 'detect-document') + detect_document_parser.add_argument('path') + + detect_document_uri_parser = subparsers.add_parser( + 'detect-document-uri') + detect_document_uri_parser.add_argument('uri') + + safe_search_parser = subparsers.add_parser( + 'safe-search') + safe_search_parser.add_argument('path') + + safe_search_uri_parser = subparsers.add_parser( + 'safe-search-uri') + safe_search_uri_parser.add_argument('uri') + + web_detect_parser = subparsers.add_parser( + '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': + web_entities_include_geo_results(args.path) + elif args.command == 'web-entities-uri': + web_entities_include_geo_results_uri(args.uri) + elif args.command == 'detect-document': + detect_document(args.path) + elif args.command == 'detect-document-uri': + detect_document_uri(args.uri) + elif args.command == 'safe-search': + detect_safe_search(args.path) + elif args.command == 'safe-search-uri': + detect_safe_search_uri(args.uri) + elif args.command == 'web-detect': + detect_web(args.path) + elif args.command == 'web-detect-uri': + detect_web(args.uri) diff --git a/vision/cloud-client/detect/beta_snippets_test.py b/vision/cloud-client/detect/beta_snippets_test.py new file mode 100644 index 00000000000..9213b45637b --- /dev/null +++ b/vision/cloud-client/detect/beta_snippets_test.py @@ -0,0 +1,76 @@ +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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. + +import os + +from beta_snippets import ( + detect_document, + detect_safe_search, + detect_web, + web_entities, + web_entities_include_geo_results, + web_entities_include_geo_results_uri +) + +BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] + + +def test_file_with_geo(capsys): + path = 'resources/city.jpg' + web_entities_include_geo_results(path) + out, _ = capsys.readouterr() + + assert 'Zepra' in out + + +def test_gcsuri_with_geo(capsys): + uri = 'gs://{}/vision/landmark.jpg'.format(BUCKET) + web_entities_include_geo_results_uri(uri) + out, _ = capsys.readouterr() + + assert 'Description: Palace of Fine Arts Theatre' in out + + +def test_file_without_geo(capsys): + path = 'resources/city.jpg' + web_entities(path) + out, _ = capsys.readouterr() + + assert 'Zepra' not in out + + +def test_detect_document_path(capsys): + path = 'resources/text.jpg' + detect_document(path) + out, _ = capsys.readouterr() + + assert 'Word text: class (confidence:' in out + + +def test_safe_search(capsys): + path = 'resources/wakeupcat.jpg' + detect_safe_search(path) + out, _ = capsys.readouterr() + + assert 'VERY_LIKELY' in out + assert 'racy: ' in out + + +def test_detect_file(capsys): + path = 'resources/landmark.jpg' + detect_web(path) + out, _ = capsys.readouterr() + + assert 'Description: Palace of Fine Arts Theatre' in out + assert 'Best guess label: palace of fine arts' in out diff --git a/vision/cloud-client/detect/requirements.txt b/vision/cloud-client/detect/requirements.txt index 83e41e121fa..9dc181e7a21 100644 --- a/vision/cloud-client/detect/requirements.txt +++ b/vision/cloud-client/detect/requirements.txt @@ -1 +1 @@ -google-cloud-vision==0.28.0 +google-cloud-vision==0.29.0 diff --git a/vision/cloud-client/detect/resources/city.jpg b/vision/cloud-client/detect/resources/city.jpg new file mode 100644 index 00000000000..b14282e7539 Binary files /dev/null and b/vision/cloud-client/detect/resources/city.jpg differ diff --git a/vision/cloud-client/document_text/requirements.txt b/vision/cloud-client/document_text/requirements.txt index fb98495c164..c09c005e1d0 100644 --- a/vision/cloud-client/document_text/requirements.txt +++ b/vision/cloud-client/document_text/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-vision==0.28.0 +google-cloud-vision==0.29.0 pillow==4.3.0 diff --git a/vision/cloud-client/face_detection/requirements.txt b/vision/cloud-client/face_detection/requirements.txt index e1c2190f3fd..b0c1c169af1 100644 --- a/vision/cloud-client/face_detection/requirements.txt +++ b/vision/cloud-client/face_detection/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-vision==0.28.0 +google-cloud-vision==0.29.0 Pillow==4.3.0 diff --git a/vision/cloud-client/quickstart/requirements.txt b/vision/cloud-client/quickstart/requirements.txt index 83e41e121fa..9dc181e7a21 100644 --- a/vision/cloud-client/quickstart/requirements.txt +++ b/vision/cloud-client/quickstart/requirements.txt @@ -1 +1 @@ -google-cloud-vision==0.28.0 +google-cloud-vision==0.29.0 diff --git a/vision/cloud-client/web/requirements.txt b/vision/cloud-client/web/requirements.txt index 83e41e121fa..9dc181e7a21 100644 --- a/vision/cloud-client/web/requirements.txt +++ b/vision/cloud-client/web/requirements.txt @@ -1 +1 @@ -google-cloud-vision==0.28.0 +google-cloud-vision==0.29.0