Skip to content

Commit

Permalink
[DO_NOT_MERGE] Add samples for object localization and handwritten oc…
Browse files Browse the repository at this point in the history
…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
nnegrey authored and dizcology committed Jul 19, 2018
1 parent be34b49 commit 6eb79f7
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 1 deletion.
52 changes: 52 additions & 0 deletions samples/snippets/detect/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,58 @@ To run this sample:
Beta Detect
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=vision/cloud-client/detect/beta_snippets.py,vision/cloud-client/detect/README.rst




To run this sample:

.. code-block:: bash
$ python beta_snippets.py
usage: beta_snippets.py [-h]
{object-localization,object-localization-uri,handwritten-ocr,handwritten-ocr-uri}
...
Google Cloud Vision API Python Beta Snippets
Example Usage:
python beta_snippets.py -h
python beta_snippets.py object-localizer INPUT_IMAGE
python beta_snippets.py object-localizer-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.
positional arguments:
{object-localization,object-localization-uri,handwritten-ocr,handwritten-ocr-uri}
object-localization
Localize objects in the local image. Args: path: The
path to the local file.
object-localization-uri
Localize objects in the image on Google Cloud Storage
Args: uri: The path to the file in Google Cloud
Storage (gs://...)
handwritten-ocr Detects handwritten characters in a local image. Args:
path: The path to the local file.
handwritten-ocr-uri
Detects handwritten characters in the file located in
Google Cloud Storage. Args: uri: The path to the file
in Google Cloud Storage (gs://...)
optional arguments:
-h, --help show this help message and exit
The client library
Expand Down
3 changes: 3 additions & 0 deletions samples/snippets/detect/README.rst.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ samples:
- name: Detect
file: detect.py
show_help: True
- name: Beta Detect
file: beta_snippets.py
show_help: True

cloud_client_library: true

Expand Down
210 changes: 210 additions & 0 deletions samples/snippets/detect/beta_snippets.py
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)
54 changes: 54 additions & 0 deletions samples/snippets/detect/beta_snippets_test.py
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
2 changes: 1 addition & 1 deletion samples/snippets/detect/requirements.txt
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.
Binary file added samples/snippets/detect/resources/puppies.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6eb79f7

Please sign in to comment.