Skip to content

Commit 318cff2

Browse files
nnegreydizcology
authored andcommitted
[DO_NOT_MERGE] Add samples for object localization and handwritten ocr (#1572)
* Add samples for object localization and handwritten ocr * Update to released lib * Update beta_snippets.py
1 parent 7d9e380 commit 318cff2

File tree

8 files changed

+320
-1
lines changed

8 files changed

+320
-1
lines changed

vision/cloud-client/detect/README.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,58 @@ To run this sample:
143143
144144
145145
146+
Beta Detect
147+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
148+
149+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
150+
: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
151+
152+
153+
154+
155+
To run this sample:
156+
157+
.. code-block:: bash
158+
159+
$ python beta_snippets.py
160+
161+
usage: beta_snippets.py [-h]
162+
{object-localization,object-localization-uri,handwritten-ocr,handwritten-ocr-uri}
163+
...
164+
165+
Google Cloud Vision API Python Beta Snippets
166+
167+
Example Usage:
168+
python beta_snippets.py -h
169+
python beta_snippets.py object-localizer INPUT_IMAGE
170+
python beta_snippets.py object-localizer-uri gs://...
171+
python beta_snippets.py handwritten-ocr INPUT_IMAGE
172+
python beta_snippets.py handwritten-ocr-uri gs://...
173+
174+
For more information, the documentation at
175+
https://cloud.google.com/vision/docs.
176+
177+
positional arguments:
178+
{object-localization,object-localization-uri,handwritten-ocr,handwritten-ocr-uri}
179+
object-localization
180+
Localize objects in the local image. Args: path: The
181+
path to the local file.
182+
object-localization-uri
183+
Localize objects in the image on Google Cloud Storage
184+
Args: uri: The path to the file in Google Cloud
185+
Storage (gs://...)
186+
handwritten-ocr Detects handwritten characters in a local image. Args:
187+
path: The path to the local file.
188+
handwritten-ocr-uri
189+
Detects handwritten characters in the file located in
190+
Google Cloud Storage. Args: uri: The path to the file
191+
in Google Cloud Storage (gs://...)
192+
193+
optional arguments:
194+
-h, --help show this help message and exit
195+
196+
197+
146198
147199
148200
The client library

vision/cloud-client/detect/README.rst.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ samples:
2424
- name: Detect
2525
file: detect.py
2626
show_help: True
27+
- name: Beta Detect
28+
file: beta_snippets.py
29+
show_help: True
2730

2831
cloud_client_library: true
2932

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2018 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
import beta_snippets
17+
18+
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
19+
20+
21+
def test_localize_objects(capsys):
22+
path = os.path.join(RESOURCES, 'puppies.jpg')
23+
24+
beta_snippets.localize_objects(path)
25+
26+
out, _ = capsys.readouterr()
27+
assert 'Dog' in out
28+
29+
30+
def test_localize_objects_uri(capsys):
31+
uri = 'gs://cloud-samples-data/vision/puppies.jpg'
32+
33+
beta_snippets.localize_objects_uri(uri)
34+
35+
out, _ = capsys.readouterr()
36+
assert 'Dog' in out
37+
38+
39+
def test_handwritten_ocr(capsys):
40+
path = os.path.join(RESOURCES, 'handwritten.jpg')
41+
42+
beta_snippets.detect_handwritten_ocr(path)
43+
44+
out, _ = capsys.readouterr()
45+
assert 'Cloud Vision API' in out
46+
47+
48+
def test_handwritten_ocr_uri(capsys):
49+
uri = 'gs://cloud-samples-data/vision/handwritten.jpg'
50+
51+
beta_snippets.detect_handwritten_ocr_uri(uri)
52+
53+
out, _ = capsys.readouterr()
54+
assert 'Cloud Vision API' in out
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-vision==0.32.0
1+
google-cloud-vision==0.33.0
22
google-cloud-storage==1.6.0
Loading
Loading
Loading

0 commit comments

Comments
 (0)