-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[DO_NOT_MERGE] Add samples for object localization and handwritten ocr #1572
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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,210 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2018 Google LLC | ||
# | ||
# 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. | ||
|
||
""" | ||
Google Cloud Vision API Python Beta Snippets | ||
|
||
Example Usage: | ||
python beta_snippets.py -h | ||
python beta_snippets.py object-localization INPUT_IMAGE | ||
python beta_snippets.py object-localization-uri gs://... | ||
python beta_snippets.py handwritten-ocr INPUT_IMAGE | ||
python beta_snippets.py handwritten-ocr-uri gs://... | ||
|
||
|
||
For more information, the documentation at | ||
https://cloud.google.com/vision/docs. | ||
""" | ||
|
||
import argparse | ||
import io | ||
|
||
|
||
# [START vision_localize_objects] | ||
def localize_objects(path): | ||
"""Localize objects in the local image. | ||
|
||
Args: | ||
path: The path to the local file. | ||
""" | ||
from google.cloud import vision_v1p3beta1 as vision | ||
client = vision.ImageAnnotatorClient() | ||
|
||
with open(path, 'rb') as image_file: | ||
content = image_file.read() | ||
image = vision.types.Image(content=content) | ||
|
||
objects = client.object_localization( | ||
image=image).localized_object_annotations | ||
|
||
print('Number of objects found: {}'.format(len(objects))) | ||
for object in objects: | ||
print('\n{} (confidence: {})'.format(object.name, object.score)) | ||
print('Normalized bounding polygon vertices: ') | ||
for vertex in object.bounding_poly.normalized_vertices: | ||
print(' - ({}, {})'.format(vertex.x, vertex.y)) | ||
# [END vision_localize_objects] | ||
|
||
|
||
# [START vision_localize_objects_uri] | ||
def localize_objects_uri(uri): | ||
"""Localize objects in the image on Google Cloud Storage | ||
|
||
Args: | ||
uri: The path to the file in Google Cloud Storage (gs://...) | ||
""" | ||
from google.cloud import vision_v1p3beta1 as vision | ||
client = vision.ImageAnnotatorClient() | ||
|
||
image = vision.types.Image() | ||
image.source.image_uri = uri | ||
|
||
objects = client.object_localization( | ||
image=image).localized_object_annotations | ||
|
||
print('Number of objects found: {}'.format(len(objects))) | ||
for object in objects: | ||
print('\n{} (confidence: {})'.format(object.name, object.score)) | ||
print('Normalized bounding polygon vertices: ') | ||
for vertex in object.bounding_poly.normalized_vertices: | ||
print(' - ({}, {})'.format(vertex.x, vertex.y)) | ||
# [END vision_localize_objects_uri] | ||
|
||
|
||
# [START vision_handwritten_ocr] | ||
def detect_handwritten_ocr(path): | ||
"""Detects handwritten characters in a local image. | ||
|
||
Args: | ||
path: The path to the local file. | ||
""" | ||
from google.cloud import vision_v1p3beta1 as vision | ||
client = vision.ImageAnnotatorClient() | ||
|
||
with io.open(path, 'rb') as image_file: | ||
content = image_file.read() | ||
|
||
image = vision.types.Image(content=content) | ||
|
||
# Language hint codes for handwritten OCR: | ||
# en-t-i0-handwrit, mul-Latn-t-i0-handwrit | ||
# Note: Use only one language hint code per request for handwritten OCR. | ||
image_context = vision.types.ImageContext( | ||
language_hints=['en-t-i0-handwrit']) | ||
|
||
response = client.document_text_detection(image=image, | ||
image_context=image_context) | ||
|
||
print('Full Text: {}'.format(response.full_text_annotation.text)) | ||
for page in response.full_text_annotation.pages: | ||
for block in page.blocks: | ||
print('\nBlock confidence: {}\n'.format(block.confidence)) | ||
|
||
for paragraph in block.paragraphs: | ||
print('Paragraph confidence: {}'.format( | ||
paragraph.confidence)) | ||
|
||
for word in paragraph.words: | ||
word_text = ''.join([ | ||
symbol.text for symbol in word.symbols | ||
]) | ||
print('Word text: {} (confidence: {})'.format( | ||
word_text, word.confidence)) | ||
|
||
for symbol in word.symbols: | ||
print('\tSymbol: {} (confidence: {})'.format( | ||
symbol.text, symbol.confidence)) | ||
# [END vision_handwritten_ocr] | ||
|
||
|
||
# [START vision_handwritten_ocr_uri] | ||
def detect_handwritten_ocr_uri(uri): | ||
"""Detects handwritten characters in the file located in Google Cloud | ||
Storage. | ||
|
||
Args: | ||
uri: The path to the file in Google Cloud Storage (gs://...) | ||
""" | ||
from google.cloud import vision_v1p3beta1 as vision | ||
client = vision.ImageAnnotatorClient() | ||
image = vision.types.Image() | ||
image.source.image_uri = uri | ||
|
||
# Language hint codes for handwritten OCR: | ||
# en-t-i0-handwrit, mul-Latn-t-i0-handwrit | ||
# Note: Use only one language hint code per request for handwritten OCR. | ||
image_context = vision.types.ImageContext( | ||
language_hints=['en-t-i0-handwrit']) | ||
|
||
response = client.document_text_detection(image=image, | ||
image_context=image_context) | ||
|
||
print('Full Text: {}'.format(response.full_text_annotation.text)) | ||
for page in response.full_text_annotation.pages: | ||
for block in page.blocks: | ||
print('\nBlock confidence: {}\n'.format(block.confidence)) | ||
|
||
for paragraph in block.paragraphs: | ||
print('Paragraph confidence: {}'.format( | ||
paragraph.confidence)) | ||
|
||
for word in paragraph.words: | ||
word_text = ''.join([ | ||
symbol.text for symbol in word.symbols | ||
]) | ||
print('Word text: {} (confidence: {})'.format( | ||
word_text, word.confidence)) | ||
|
||
for symbol in word.symbols: | ||
print('\tSymbol: {} (confidence: {})'.format( | ||
symbol.text, symbol.confidence)) | ||
# [END vision_handwritten_ocr_uri] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
subparsers = parser.add_subparsers(dest='command') | ||
|
||
object_parser = subparsers.add_parser( | ||
'object-localization', help=localize_objects.__doc__) | ||
object_parser.add_argument('path') | ||
|
||
object_uri_parser = subparsers.add_parser( | ||
'object-localization-uri', help=localize_objects_uri.__doc__) | ||
object_uri_parser.add_argument('uri') | ||
|
||
handwritten_parser = subparsers.add_parser( | ||
'handwritten-ocr', help=detect_handwritten_ocr.__doc__) | ||
handwritten_parser.add_argument('path') | ||
|
||
handwritten_uri_parser = subparsers.add_parser( | ||
'handwritten-ocr-uri', help=detect_handwritten_ocr_uri.__doc__) | ||
handwritten_uri_parser.add_argument('uri') | ||
|
||
args = parser.parse_args() | ||
|
||
if 'uri' in args.command: | ||
if 'object-localization-uri' in args.command: | ||
localize_objects_uri(args.uri) | ||
elif 'handwritten-ocr-uri' in args.command: | ||
detect_handwritten_ocr_uri(args.uri) | ||
else: | ||
if 'object-localization' in args.command: | ||
localize_objects(args.path) | ||
elif 'handwritten-ocr' in args.command: | ||
detect_handwritten_ocr(args.path) |
This file contains 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,54 @@ | ||
# Copyright 2018 Google LLC 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 | ||
|
||
import beta_snippets | ||
|
||
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources') | ||
|
||
|
||
def test_localize_objects(capsys): | ||
path = os.path.join(RESOURCES, 'puppies.jpg') | ||
|
||
beta_snippets.localize_objects(path) | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'Dog' in out | ||
|
||
|
||
def test_localize_objects_uri(capsys): | ||
uri = 'gs://cloud-samples-data/vision/puppies.jpg' | ||
|
||
beta_snippets.localize_objects_uri(uri) | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'Dog' in out | ||
|
||
|
||
def test_handwritten_ocr(capsys): | ||
path = os.path.join(RESOURCES, 'handwritten.jpg') | ||
|
||
beta_snippets.detect_handwritten_ocr(path) | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'Cloud Vision API' in out | ||
|
||
|
||
def test_handwritten_ocr_uri(capsys): | ||
uri = 'gs://cloud-samples-data/vision/handwritten.jpg' | ||
|
||
beta_snippets.detect_handwritten_ocr_uri(uri) | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'Cloud Vision API' in out |
This file contains 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.32.0 | ||
google-cloud-vision==0.33.0 | ||
google-cloud-storage==1.6.0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because
object
is a Pyhton built-in global variable, perhaps useobject_
instead. the pluralobjects
is fine though.