Skip to content

Commit f7770ce

Browse files
beccasaurusJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Revert "Vision API features update [(#1339)](GoogleCloudPlatform/python-docs-samples#1339)" [(#1351)](GoogleCloudPlatform/python-docs-samples#1351)
This reverts commit fba66ee.
1 parent fba66ee commit f7770ce

File tree

4 files changed

+541
-212
lines changed

4 files changed

+541
-212
lines changed
Lines changed: 379 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,379 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
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+
"""Demonstrates beta features using the Google Cloud Vision API.
18+
19+
Example usage:
20+
python beta_snippets.py web-entities resources/city.jpg
21+
python beta_snippets.py detect-document resources/text.jpg
22+
python beta_snippets.py safe-search resources/wakeupcat.jpg
23+
python beta_snippets.py web-detect resources/landmark.jpg
24+
"""
25+
26+
# [START imports]
27+
import argparse
28+
import io
29+
30+
from google.cloud import vision_v1p1beta1 as vision
31+
# [END imports]
32+
33+
34+
# [START vision_detect_document]
35+
def detect_document(path):
36+
"""Detects document features in an image."""
37+
client = vision.ImageAnnotatorClient()
38+
39+
with io.open(path, 'rb') as image_file:
40+
content = image_file.read()
41+
42+
image = vision.types.Image(content=content)
43+
44+
response = client.document_text_detection(image=image)
45+
46+
for page in response.full_text_annotation.pages:
47+
for block in page.blocks:
48+
block_words = []
49+
for paragraph in block.paragraphs:
50+
block_words.extend(paragraph.words)
51+
print(u'Paragraph Confidence: {}\n'.format(
52+
paragraph.confidence))
53+
54+
block_text = ''
55+
block_symbols = []
56+
for word in block_words:
57+
block_symbols.extend(word.symbols)
58+
word_text = ''
59+
for symbol in word.symbols:
60+
word_text = word_text + symbol.text
61+
print(u'\tSymbol text: {} (confidence: {})'.format(
62+
symbol.text, symbol.confidence))
63+
print(u'Word text: {} (confidence: {})\n'.format(
64+
word_text, word.confidence))
65+
66+
block_text += ' ' + word_text
67+
68+
print(u'Block Content: {}\n'.format(block_text))
69+
print(u'Block Confidence:\n {}\n'.format(block.confidence))
70+
# [END vision_detect_document]
71+
72+
73+
# [START vision_detect_document_uri]
74+
def detect_document_uri(uri):
75+
"""Detects document features in the file located in Google Cloud
76+
Storage."""
77+
client = vision.ImageAnnotatorClient()
78+
image = vision.types.Image()
79+
image.source.image_uri = uri
80+
81+
response = client.document_text_detection(image=image)
82+
83+
for page in response.full_text_annotation.pages:
84+
for block in page.blocks:
85+
block_words = []
86+
for paragraph in block.paragraphs:
87+
block_words.extend(paragraph.words)
88+
print(u'Paragraph Confidence: {}\n'.format(
89+
paragraph.confidence))
90+
91+
block_text = ''
92+
block_symbols = []
93+
for word in block_words:
94+
block_symbols.extend(word.symbols)
95+
word_text = ''
96+
for symbol in word.symbols:
97+
word_text = word_text + symbol.text
98+
print(u'\tSymbol text: {} (confidence: {})'.format(
99+
symbol.text, symbol.confidence))
100+
print(u'Word text: {} (confidence: {})\n'.format(
101+
word_text, word.confidence))
102+
103+
block_text += ' ' + word_text
104+
105+
print(u'Block Content: {}\n'.format(block_text))
106+
print(u'Block Confidence:\n {}\n'.format(block.confidence))
107+
# [END vision_detect_document_uri]
108+
109+
110+
# [START vision_detect_safe_search]
111+
def detect_safe_search(path):
112+
"""Detects unsafe features in the file."""
113+
client = vision.ImageAnnotatorClient()
114+
115+
with io.open(path, 'rb') as image_file:
116+
content = image_file.read()
117+
118+
image = vision.types.Image(content=content)
119+
120+
response = client.safe_search_detection(image=image)
121+
safe = response.safe_search_annotation
122+
123+
# Names of likelihood from google.cloud.vision.enums
124+
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
125+
'LIKELY', 'VERY_LIKELY')
126+
print('Safe search:')
127+
128+
print('adult: {}'.format(likelihood_name[safe.adult]))
129+
print('medical: {}'.format(likelihood_name[safe.medical]))
130+
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
131+
print('violence: {}'.format(likelihood_name[safe.violence]))
132+
print('racy: {}'.format(likelihood_name[safe.racy]))
133+
# [END vision_detect_safe_search]
134+
135+
136+
# [START vision_detect_safe_search_uri]
137+
def detect_safe_search_uri(uri):
138+
"""Detects unsafe features in the file located in Google Cloud Storage or
139+
on the Web."""
140+
client = vision.ImageAnnotatorClient()
141+
image = vision.types.Image()
142+
image.source.image_uri = uri
143+
144+
response = client.safe_search_detection(image=image)
145+
safe = response.safe_search_annotation
146+
147+
# Names of likelihood from google.cloud.vision.enums
148+
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
149+
'LIKELY', 'VERY_LIKELY')
150+
print('Safe search:')
151+
152+
print('adult: {}'.format(likelihood_name[safe.adult]))
153+
print('medical: {}'.format(likelihood_name[safe.medical]))
154+
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
155+
print('violence: {}'.format(likelihood_name[safe.violence]))
156+
print('racy: {}'.format(likelihood_name[safe.racy]))
157+
# [END vision_detect_safe_search_uri]
158+
159+
160+
# [START vision_detect_web]
161+
def detect_web(path):
162+
"""Detects web annotations given an image."""
163+
client = vision.ImageAnnotatorClient()
164+
165+
with io.open(path, 'rb') as image_file:
166+
content = image_file.read()
167+
168+
image = vision.types.Image(content=content)
169+
170+
response = client.web_detection(image=image)
171+
annotations = response.web_detection
172+
173+
if annotations.best_guess_labels:
174+
for label in annotations.best_guess_labels:
175+
print('\nBest guess label: {}'.format(label.label))
176+
177+
if annotations.pages_with_matching_images:
178+
print('\n{} Pages with matching images found:'.format(
179+
len(annotations.pages_with_matching_images)))
180+
181+
for page in annotations.pages_with_matching_images:
182+
print('\n\tPage url : {}'.format(page.url))
183+
184+
if page.full_matching_images:
185+
print('\t{} Full Matches found: '.format(
186+
len(page.full_matching_images)))
187+
188+
for image in page.full_matching_images:
189+
print('\t\tImage url : {}'.format(image.url))
190+
191+
if page.partial_matching_images:
192+
print('\t{} Partial Matches found: '.format(
193+
len(page.partial_matching_images)))
194+
195+
for image in page.partial_matching_images:
196+
print('\t\tImage url : {}'.format(image.url))
197+
198+
if annotations.web_entities:
199+
print('\n{} Web entities found: '.format(
200+
len(annotations.web_entities)))
201+
202+
for entity in annotations.web_entities:
203+
print('\n\tScore : {}'.format(entity.score))
204+
print(u'\tDescription: {}'.format(entity.description))
205+
206+
if annotations.visually_similar_images:
207+
print('\n{} visually similar images found:\n'.format(
208+
len(annotations.visually_similar_images)))
209+
210+
for image in annotations.visually_similar_images:
211+
print('\tImage url : {}'.format(image.url))
212+
# [END vision_detect_web]
213+
214+
215+
# [START vision_detect_web_uri]
216+
def detect_web_uri(uri):
217+
"""Detects web annotations in the file located in Google Cloud Storage."""
218+
client = vision.ImageAnnotatorClient()
219+
image = vision.types.Image()
220+
image.source.image_uri = uri
221+
222+
response = client.web_detection(image=image)
223+
annotations = response.web_detection
224+
225+
if annotations.best_guess_labels:
226+
for label in annotations.best_guess_labels:
227+
print('\nBest guess label: {}'.format(label.label))
228+
229+
if annotations.pages_with_matching_images:
230+
print('\n{} Pages with matching images found:'.format(
231+
len(annotations.pages_with_matching_images)))
232+
233+
for page in annotations.pages_with_matching_images:
234+
print('\n\tPage url : {}'.format(page.url))
235+
236+
if page.full_matching_images:
237+
print('\t{} Full Matches found: '.format(
238+
len(page.full_matching_images)))
239+
240+
for image in page.full_matching_images:
241+
print('\t\tImage url : {}'.format(image.url))
242+
243+
if page.partial_matching_images:
244+
print('\t{} Partial Matches found: '.format(
245+
len(page.partial_matching_images)))
246+
247+
for image in page.partial_matching_images:
248+
print('\t\tImage url : {}'.format(image.url))
249+
250+
if annotations.web_entities:
251+
print('\n{} Web entities found: '.format(
252+
len(annotations.web_entities)))
253+
254+
for entity in annotations.web_entities:
255+
print('\n\tScore : {}'.format(entity.score))
256+
print(u'\tDescription: {}'.format(entity.description))
257+
258+
if annotations.visually_similar_images:
259+
print('\n{} visually similar images found:\n'.format(
260+
len(annotations.visually_similar_images)))
261+
262+
for image in annotations.visually_similar_images:
263+
print('\tImage url : {}'.format(image.url))
264+
# [END vision_detect_web_uri]
265+
266+
267+
def web_entities(path):
268+
client = vision.ImageAnnotatorClient()
269+
270+
with io.open(path, 'rb') as image_file:
271+
content = image_file.read()
272+
273+
image = vision.types.Image(content=content)
274+
275+
response = client.web_detection(image=image)
276+
277+
for entity in response.web_detection.web_entities:
278+
print('\n\tScore : {}'.format(entity.score))
279+
print(u'\tDescription: {}'.format(entity.description))
280+
281+
282+
# [START vision_web_entities_include_geo_results]
283+
def web_entities_include_geo_results(path):
284+
client = vision.ImageAnnotatorClient()
285+
286+
with io.open(path, 'rb') as image_file:
287+
content = image_file.read()
288+
289+
image = vision.types.Image(content=content)
290+
291+
web_detection_params = vision.types.WebDetectionParams(
292+
include_geo_results=True)
293+
image_context = vision.types.ImageContext(
294+
web_detection_params=web_detection_params)
295+
296+
response = client.web_detection(image=image, image_context=image_context)
297+
298+
for entity in response.web_detection.web_entities:
299+
print('\n\tScore : {}'.format(entity.score))
300+
print(u'\tDescription: {}'.format(entity.description))
301+
# [END vision_web_entities_include_geo_results]
302+
303+
304+
# [START vision_web_entities_include_geo_results_uri]
305+
def web_entities_include_geo_results_uri(uri):
306+
client = vision.ImageAnnotatorClient()
307+
308+
image = vision.types.Image()
309+
image.source.image_uri = uri
310+
311+
web_detection_params = vision.types.WebDetectionParams(
312+
include_geo_results=True)
313+
image_context = vision.types.ImageContext(
314+
web_detection_params=web_detection_params)
315+
316+
response = client.web_detection(image=image, image_context=image_context)
317+
318+
for entity in response.web_detection.web_entities:
319+
print('\n\tScore : {}'.format(entity.score))
320+
print(u'\tDescription: {}'.format(entity.description))
321+
# [END vision_web_entities_include_geo_results_uri]
322+
323+
324+
if __name__ == '__main__':
325+
parser = argparse.ArgumentParser(
326+
description=__doc__,
327+
formatter_class=argparse.RawDescriptionHelpFormatter)
328+
subparsers = parser.add_subparsers(dest='command')
329+
330+
web_entities_parser = subparsers.add_parser(
331+
'web-entities')
332+
web_entities_parser.add_argument('path')
333+
334+
web_entities_uri_parser = subparsers.add_parser(
335+
'web-entities-uri')
336+
web_entities_uri_parser.add_argument('uri')
337+
338+
detect_document_parser = subparsers.add_parser(
339+
'detect-document')
340+
detect_document_parser.add_argument('path')
341+
342+
detect_document_uri_parser = subparsers.add_parser(
343+
'detect-document-uri')
344+
detect_document_uri_parser.add_argument('uri')
345+
346+
safe_search_parser = subparsers.add_parser(
347+
'safe-search')
348+
safe_search_parser.add_argument('path')
349+
350+
safe_search_uri_parser = subparsers.add_parser(
351+
'safe-search-uri')
352+
safe_search_uri_parser.add_argument('uri')
353+
354+
web_detect_parser = subparsers.add_parser(
355+
'web-detect')
356+
web_detect_parser.add_argument('path')
357+
358+
web_detect_uri_parser = subparsers.add_parser(
359+
'web-detect-uri')
360+
web_detect_uri_parser.add_argument('uri')
361+
362+
args = parser.parse_args()
363+
364+
if args.command == 'web-entities':
365+
web_entities_include_geo_results(args.path)
366+
elif args.command == 'web-entities-uri':
367+
web_entities_include_geo_results_uri(args.uri)
368+
elif args.command == 'detect-document':
369+
detect_document(args.path)
370+
elif args.command == 'detect-document-uri':
371+
detect_document_uri(args.uri)
372+
elif args.command == 'safe-search':
373+
detect_safe_search(args.path)
374+
elif args.command == 'safe-search-uri':
375+
detect_safe_search_uri(args.uri)
376+
elif args.command == 'web-detect':
377+
detect_web(args.path)
378+
elif args.command == 'web-detect-uri':
379+
detect_web(args.uri)

0 commit comments

Comments
 (0)