diff --git a/samples/snippets/detect/README.rst b/samples/snippets/detect/README.rst index 0f6f5003..6074419c 100644 --- a/samples/snippets/detect/README.rst +++ b/samples/snippets/detect/README.rst @@ -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 diff --git a/samples/snippets/detect/README.rst.in b/samples/snippets/detect/README.rst.in index 82be88ff..0d105411 100644 --- a/samples/snippets/detect/README.rst.in +++ b/samples/snippets/detect/README.rst.in @@ -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 diff --git a/samples/snippets/detect/beta_snippets.py b/samples/snippets/detect/beta_snippets.py new file mode 100644 index 00000000..0668b377 --- /dev/null +++ b/samples/snippets/detect/beta_snippets.py @@ -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) diff --git a/samples/snippets/detect/beta_snippets_test.py b/samples/snippets/detect/beta_snippets_test.py new file mode 100644 index 00000000..8b4c4078 --- /dev/null +++ b/samples/snippets/detect/beta_snippets_test.py @@ -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 diff --git a/samples/snippets/detect/requirements.txt b/samples/snippets/detect/requirements.txt index c7b2576f..2888f5fb 100644 --- a/samples/snippets/detect/requirements.txt +++ b/samples/snippets/detect/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-vision==0.32.0 +google-cloud-vision==0.33.0 google-cloud-storage==1.6.0 diff --git a/samples/snippets/detect/resources/duck_and_truck.jpg b/samples/snippets/detect/resources/duck_and_truck.jpg new file mode 100644 index 00000000..5c560fe7 Binary files /dev/null and b/samples/snippets/detect/resources/duck_and_truck.jpg differ diff --git a/samples/snippets/detect/resources/handwritten.jpg b/samples/snippets/detect/resources/handwritten.jpg new file mode 100644 index 00000000..50a9575b Binary files /dev/null and b/samples/snippets/detect/resources/handwritten.jpg differ diff --git a/samples/snippets/detect/resources/puppies.jpg b/samples/snippets/detect/resources/puppies.jpg new file mode 100644 index 00000000..1bfbbc9c Binary files /dev/null and b/samples/snippets/detect/resources/puppies.jpg differ