-
Notifications
You must be signed in to change notification settings - Fork 6.5k
<
8000
div id="files_bucket" class="files-bucket files-next-bucket clearfix pull-request-tab-content is-visible js-multi-line-comments-enabled">
Show file tree
Hide file tree
Sep 21, 2017
Sep 25, 2017
Sep 25, 2017
Oct 13, 2017
Nov 10, 2017
Nov 18, 2017
Nov 18, 2017
Nov 18, 2017
Nov 20, 2017
Nov 29, 2017
Nov 29, 2017
Dec 1, 2017
Dec 1, 2017
Dec 4, 2017
Loading
Vision beta #1211
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
Merged
Merged
Vision beta #1211
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d6aa9fb
add racy_likelihood to safe_search
dizcology 924440d
update web_detect to group by webpages
dizcology dab2061
add web_entities.py
dizcology 5ccccae
update web_entities test
dizcology b83cccc
update web_entities for geo_results
dizcology fca6959
revert changes since v1 samples should remain the same
dizcology e4233c8
add beta samples in its own directory
dizcology bc99c72
flake
dizcology f50f495
upate README
dizcology 5f4dd61
move code snippets for beta features into a single file
dizcology ad35c10
flake
dizcology d29b70d
use the correct code snippet for web detect samples
dizcology 081791e
flake
dizcology 6d839a2
client library version
dizcology File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
google-cloud-vision==0.28.0 | ||
google-cloud-vision==0.29.0 | ||
pillow==4.3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.