forked from googleapis/python-vision
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DO_NOT_MERGE] Add samples for object localization and handwritten oc…
…r [(#1572)](GoogleCloudPlatform/python-docs-samples#1572) * Add samples for object localization and handwritten ocr * Update to released lib * Update beta_snippets.py
- Loading branch information
Showing
8 changed files
with
320 additions
and
1 deletion.
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.