|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +""" |
| 18 | +Google Cloud Vision API Python Beta Snippets |
| 19 | +
|
| 20 | +Example Usage: |
| 21 | +python beta_snippets.py -h |
| 22 | +python beta_snippets.py object-localization INPUT_IMAGE |
| 23 | +python beta_snippets.py object-localization-uri gs://... |
| 24 | +python beta_snippets.py handwritten-ocr INPUT_IMAGE |
| 25 | +python beta_snippets.py handwritten-ocr-uri gs://... |
| 26 | +
|
| 27 | +
|
| 28 | +For more information, the documentation at |
| 29 | +https://cloud.google.com/vision/docs. |
| 30 | +""" |
| 31 | + |
| 32 | +import argparse |
| 33 | +import io |
| 34 | + |
| 35 | + |
| 36 | +# [START vision_localize_objects] |
| 37 | +def localize_objects(path): |
| 38 | + """Localize objects in the local image. |
| 39 | +
|
| 40 | + Args: |
| 41 | + path: The path to the local file. |
| 42 | + """ |
| 43 | + from google.cloud import vision_v1p3beta1 as vision |
| 44 | + client = vision.ImageAnnotatorClient() |
| 45 | + |
| 46 | + with open(path, 'rb') as image_file: |
| 47 | + content = image_file.read() |
| 48 | + image = vision.types.Image(content=content) |
| 49 | + |
| 50 | + objects = client.object_localization( |
| 51 | + image=image).localized_object_annotations |
| 52 | + |
| 53 | + print('Number of objects found: {}'.format(len(objects))) |
| 54 | + for object_ in objects: |
| 55 | + print('\n{} (confidence: {})'.format(object_.name, object_.score)) |
| 56 | + print('Normalized bounding polygon vertices: ') |
| 57 | + for vertex in object_.bounding_poly.normalized_vertices: |
| 58 | + print(' - ({}, {})'.format(vertex.x, vertex.y)) |
| 59 | +# [END vision_localize_objects] |
| 60 | + |
| 61 | + |
| 62 | +# [START vision_localize_objects_uri] |
| 63 | +def localize_objects_uri(uri): |
| 64 | + """Localize objects in the image on Google Cloud Storage |
| 65 | +
|
| 66 | + Args: |
| 67 | + uri: The path to the file in Google Cloud Storage (gs://...) |
| 68 | + """ |
| 69 | + from google.cloud import vision_v1p3beta1 as vision |
| 70 | + client = vision.ImageAnnotatorClient() |
| 71 | + |
| 72 | + image = vision.types.Image() |
| 73 | + image.source.image_uri = uri |
| 74 | + |
| 75 | + objects = client.object_localization( |
| 76 | + image=image).localized_object_annotations |
| 77 | + |
| 78 | + print('Number of objects found: {}'.format(len(objects))) |
| 79 | + for object_ in objects: |
| 80 | + print('\n{} (confidence: {})'.format(object_.name, object_.score)) |
| 81 | + print('Normalized bounding polygon vertices: ') |
| 82 | + for vertex in object_.bounding_poly.normalized_vertices: |
| 83 | + print(' - ({}, {})'.format(vertex.x, vertex.y)) |
| 84 | +# [END vision_localize_objects_uri] |
| 85 | + |
| 86 | + |
| 87 | +# [START vision_handwritten_ocr] |
| 88 | +def detect_handwritten_ocr(path): |
| 89 | + """Detects handwritten characters in a local image. |
| 90 | +
|
| 91 | + Args: |
| 92 | + path: The path to the local file. |
| 93 | + """ |
| 94 | + from google.cloud import vision_v1p3beta1 as vision |
| 95 | + client = vision.ImageAnnotatorClient() |
| 96 | + |
| 97 | + with io.open(path, 'rb') as image_file: |
| 98 | + content = image_file.read() |
| 99 | + |
| 100 | + image = vision.types.Image(content=content) |
| 101 | + |
| 102 | + # Language hint codes for handwritten OCR: |
| 103 | + # en-t-i0-handwrit, mul-Latn-t-i0-handwrit |
| 104 | + # Note: Use only one language hint code per request for handwritten OCR. |
| 105 | + image_context = vision.types.ImageContext( |
| 106 | + language_hints=['en-t-i0-handwrit']) |
| 107 | + |
| 108 | + response = client.document_text_detection(image=image, |
| 109 | + image_context=image_context) |
| 110 | + |
| 111 | + print('Full Text: {}'.format(response.full_text_annotation.text)) |
| 112 | + for page in response.full_text_annotation.pages: |
| 113 | + for block in page.blocks: |
| 114 | + print('\nBlock confidence: {}\n'.format(block.confidence)) |
| 115 | + |
| 116 | + for paragraph in block.paragraphs: |
| 117 | + print('Paragraph confidence: {}'.format( |
| 118 | + paragraph.confidence)) |
| 119 | + |
| 120 | + for word in paragraph.words: |
| 121 | + word_text = ''.join([ |
| 122 | + symbol.text for symbol in word.symbols |
| 123 | + ]) |
| 124 | + print('Word text: {} (confidence: {})'.format( |
| 125 | + word_text, word.confidence)) |
| 126 | + |
| 127 | + for symbol in word.symbols: |
| 128 | + print('\tSymbol: {} (confidence: {})'.format( |
| 129 | + symbol.text, symbol.confidence)) |
| 130 | +# [END vision_handwritten_ocr] |
| 131 | + |
| 132 | + |
| 133 | +# [START vision_handwritten_ocr_uri] |
| 134 | +def detect_handwritten_ocr_uri(uri): |
| 135 | + """Detects handwritten characters in the file located in Google Cloud |
| 136 | + Storage. |
| 137 | +
|
| 138 | + Args: |
| 139 | + uri: The path to the file in Google Cloud Storage (gs://...) |
| 140 | + """ |
| 141 | + from google.cloud import vision_v1p3beta1 as vision |
| 142 | + client = vision.ImageAnnotatorClient() |
| 143 | + image = vision.types.Image() |
| 144 | + image.source.image_uri = uri |
| 145 | + |
| 146 | + # Language hint codes for handwritten OCR: |
| 147 | + # en-t-i0-handwrit, mul-Latn-t-i0-handwrit |
| 148 | + # Note: Use only one language hint code per request for handwritten OCR. |
| 149 | + image_context = vision.types.ImageContext( |
| 150 | + language_hints=['en-t-i0-handwrit']) |
| 151 | + |
| 152 | + response = client.document_text_detection(image=image, |
| 153 | + image_context=image_context) |
| 154 | + |
| 155 | + print('Full Text: {}'.format(response.full_text_annotation.text)) |
| 156 | + for page in response.full_text_annotation.pages: |
| 157 | + for block in page.blocks: |
| 158 | + print('\nBlock confidence: {}\n'.format(block.confidence)) |
| 159 | + |
| 160 | + for paragraph in block.paragraphs: |
| 161 | + print('Paragraph confidence: {}'.format( |
| 162 | + paragraph.confidence)) |
| 163 | + |
| 164 | + for word in paragraph.words: |
| 165 | + word_text = ''.join([ |
| 166 | + symbol.text for symbol in word.symbols |
| 167 | + ]) |
| 168 | + print('Word text: {} (confidence: {})'.format( |
| 169 | + word_text, word.confidence)) |
| 170 | + |
| 171 | + for symbol in word.symbols: |
| 172 | + print('\tSymbol: {} (confidence: {})'.format( |
| 173 | + symbol.text, symbol.confidence)) |
| 174 | +# [END vision_handwritten_ocr_uri] |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == '__main__': |
| 178 | + parser = argparse.ArgumentParser( |
| 179 | + description=__doc__, |
| 180 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 181 | + subparsers = parser.add_subparsers(dest='command') |
| 182 | + |
| 183 | + object_parser = subparsers.add_parser( |
| 184 | + 'object-localization', help=localize_objects.__doc__) |
| 185 | + object_parser.add_argument('path') |
| 186 | + |
| 187 | + object_uri_parser = subparsers.add_parser( |
| 188 | + 'object-localization-uri', help=localize_objects_uri.__doc__) |
| 189 | + object_uri_parser.add_argument('uri') |
| 190 | + |
| 191 | + handwritten_parser = subparsers.add_parser( |
| 192 | + 'handwritten-ocr', help=detect_handwritten_ocr.__doc__) |
| 193 | + handwritten_parser.add_argument('path') |
| 194 | + |
| 195 | + handwritten_uri_parser = subparsers.add_parser( |
| 196 | + 'handwritten-ocr-uri', help=detect_handwritten_ocr_uri.__doc__) |
| 197 | + handwritten_uri_parser.add_argument('uri') |
| 198 | + |
| 199 | + args = parser.parse_args() |
| 200 | + |
| 201 | + if 'uri' in args.command: |
| 202 | + if 'object-localization-uri' in args.command: |
| 203 | + localize_objects_uri(args.uri) |
| 204 | + elif 'handwritten-ocr-uri' in args.command: |
| 205 | + detect_handwritten_ocr_uri(args.uri) |
| 206 | + else: |
| 207 | + if 'object-localization' in args.command: |
| 208 | + localize_objects(args.path) |
| 209 | + elif 'handwritten-ocr' in args.command: |
| 210 | + detect_handwritten_ocr(args.path) |
0 commit comments