Skip to content

Commit d732d44

Browse files
gguussdpebot
authored andcommitted
Vision cloud client snippets (GoogleCloudPlatform#751)
1 parent 0fc7a44 commit d732d44

File tree

6 files changed

+534
-0
lines changed

6 files changed

+534
-0
lines changed

vision/cloud-client/detect.py

+386
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
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+
"""This application demonstrates how to perform basic operations with the
18+
Google Cloud Vision API.
19+
20+
For more information, the documentation at
21+
https://cloud.google.com/vision/docs.
22+
"""
23+
24+
import argparse
25+
import io
26+
import os
27+
28+
from google.cloud import vision
29+
30+
31+
def detect_faces(path):
32+
"""Detects faces in an image."""
33+
vision_client = vision.Client()
34+
35+
with io.open(path, 'rb') as image_file:
36+
content = image_file.read()
37+
38+
image = vision_client.image(content=content)
39+
40+
faces = image.detect_faces()
41+
42+
print('Faces:')
43+
for face in faces:
44+
print('anger: {}'.format(face.emotions.anger))
45+
print('joy: {}'.format(face.emotions.joy))
46+
print('surprise: {}'.format(face.emotions.surprise))
47+
48+
49+
def detect_faces_cloud_storage(uri):
50+
"""Detects faces in the file located in Google Cloud Storage."""
51+
vision_client = vision.Client()
52+
image = vision_client.image(source_uri=uri)
53+
54+
faces = image.detect_faces()
55+
56+
print('Faces:')
57+
for face in faces:
58+
print('anger: {}'.format(face.emotions.anger))
59+
print('joy: {}'.format(face.emotions.joy))
60+
print('surprise: {}'.format(face.emotions.surprise))
61+
62+
63+
def detect_labels(path):
64+
"""Detects labels in the file."""
65+
vision_client = vision.Client()
66+
67+
with io.open(path, 'rb') as image_file:
68+
content = image_file.read()
69+
70+
image = vision_client.image(content=content)
71+
72+
labels = image.detect_labels()
73+
74+
print('Labels:')
75+
for label in labels:
76+
print(label.description)
77+
78+
79+
def detect_labels_cloud_storage(uri):
80+
"""Detects labels in the file located in Google Cloud Storage."""
81+
vision_client = vision.Client()
82+
image = vision_client.image(source_uri=uri)
83+
84+
labels = image.detect_labels()
85+
86+
print('Labels:')
87+
for label in labels:
88+
print(label.description)
89+
90+
91+
def detect_landmarks(path):
92+
"""Detects landmarks in the file."""
93+
vision_client = vision.Client()
94+
95+
with io.open(path, 'rb') as image_file:
96+
content = image_file.read()
97+
98+
image = vision_client.image(content=content)
99+
100+
landmarks = image.detect_landmarks()
101+
102+
print('Landmarks:')
103+
for landmark in landmarks:
104+
print(landmark.description)
105+
106+
107+
def detect_landmarks_cloud_storage(uri):
108+
"""Detects landmarks in the file located in Google Cloud Storage."""
109+
vision_client = vision.Client()
110+
image = vision_client.image(source_uri=uri)
111+
112+
landmarks = image.detect_landmarks()
113+
114+
print('Landmarks:')
115+
for landmark in landmarks:
116+
print(landmark.description)
117+
118+
119+
def detect_logos(path):
120+
"""Detects logos in the file."""
121+
vision_client = vision.Client()
122+
123+
with io.open(path, 'rb') as image_file:
124+
content = image_file.read()
125+
126+
image = vision_client.image(content=content)
127+
128+
logos = image.detect_logos()
129+
130+
print('Logos:')
131+
for logo in logos:
132+
print(logo.description)
133+
134+
135+
def detect_logos_cloud_storage(uri):
136+
"""Detects logos in the file located in Google Cloud Storage."""
137+
vision_client = vision.Client()
138+
image = vision_client.image(source_uri=uri)
139+
140+
logos = image.detect_logos()
141+
142+
print('Logos:')
143+
for logo in logos:
144+
print(logo.description)
145+
146+
147+
def detect_safe_search(path):
148+
"""Detects unsafe features in the file."""
149+
vision_client = vision.Client()
150+
151+
with io.open(path, 'rb') as image_file:
152+
content = image_file.read()
153+
154+
image = vision_client.image(content=content)
155+
156+
safe_searches = image.detect_safe_search()
157+
print('Safe search:')
158+
for safe in safe_searches:
159+
print('adult: {}'.format(safe.adult))
160+
print('medical: {}'.format(safe.medical))
161+
print('spoofed: {}'.format(safe.spoof))
162+
print('violence: {}'.format(safe.violence))
163+
164+
165+
def detect_safe_search_cloud_storage(uri):
166+
"""Detects unsafe features in the file located in Google Cloud Storage."""
167+
vision_client = vision.Client()
168+
image = vision_client.image(source_uri=uri)
169+
170+
safe_searches = image.detect_safe_search()
171+
print('Safe search:')
172+
for safe in safe_searches:
173+
print('adult: {}'.format(safe.adult))
174+
print('medical: {}'.format(safe.medical))
175+
print('spoofed: {}'.format(safe.spoof))
176+
print('violence: {}'.format(safe.violence))
177+
178+
179+
def detect_text(path):
180+
"""Detects text in the file."""
181+
vision_client = vision.Client()
182+
183+
with io.open(path, 'rb') as image_file:
184+
content = image_file.read()
185+
186+
image = vision_client.image(content=content)
187+
188+
texts = image.detect_text()
189+
print('Texts:')
190+
for text in texts:
191+
print(text.description)
192+
193+
194+
def detect_text_cloud_storage(uri):
195+
"""Detects text in the file located in Google Cloud Storage."""
196+
vision_client = vision.Client()
197+
image = vision_client.image(source_uri=uri)
198+
199+
texts = image.detect_text()
200+
print('Texts:')
201+
for text in texts:
202+
print(text.description)
203+
204+
205+
def detect_properties(path):
206+
"""Detects image properties in the file."""
207+
vision_client = vision.Client()
208+
209+
with io.open(path, 'rb') as image_file:
210+
content = image_file.read()
211+
212+
image = vision_client.image(content=content)
213+
214+
properties = image.detect_properties()
215+
print('Properties:')
216+
for prop in properties:
217+
color = prop.colors[0]
218+
print('fraction: {}'.format(color.pixel_fraction))
219+
print('r: {}'.format(color.color.red))
220+
print('g: {}'.format(color.color.green))
221+
print('g: {}'.format(color.color.blue))
222+
223+
224+
def detect_properties_cloud_storage(uri):
225+
"""Detects image properties in the file located in Google Cloud Storage."""
226+
vision_client = vision.Client()
227+
image = vision_client.image(source_uri=uri)
228+
229+
properties = image.detect_properties()
230+
for prop in properties:
231+
color = prop.colors[0]
232+
print('fraction: {}'.format(color.pixel_fraction))
233+
print('r: {}'.format(color.color.red))
234+
print('g: {}'.format(color.color.green))
235+
print('g: {}'.format(color.color.blue))
236+
237+
238+
def run_all_local():
239+
"""Runs all available detection operations on the local resources."""
240+
file_name = os.path.join(
241+
os.path.dirname(__file__),
242+
'resources/wakeupcat.jpg')
243+
detect_labels(file_name)
244+
245+
file_name = os.path.join(
246+
os.path.dirname(__file__),
247+
'resources/landmark.jpg')
248+
detect_landmarks(file_name)
249+
250+
file_name = os.path.join(
251+
os.path.dirname(__file__),
252+
'resources/face_no_surprise.jpg')
253+
detect_faces(file_name)
254+
255+
file_name = os.path.join(
256+
os.path.dirname(__file__),
257+
'resources/logos.png')
258+
detect_logos(file_name)
259+
260+
file_name = os.path.join(
261+
os.path.dirname(__file__),
262+
'resources/wakeupcat.jpg')
263+
detect_safe_search(file_name)
264+
265+
''' TODO: Uncomment when https://goo.gl/c47YwV is fixed
266+
file_name = os.path.join(
267+
os.path.dirname(__file__),
268+
'resources/text.jpg')
269+
detect_text(file_name)
270+
'''
271+
272+
file_name = os.path.join(
273+
os.path.dirname(__file__),
274+
'resources/landmark.jpg')
275+
detect_properties(file_name)
276+
277+
278+
def run_local(args):
279+
if args.command == 'all-local':
280+
run_all_local()
281+
elif args.command == 'faces':
282+
detect_faces(args.path)
283+
elif args.command == 'labels':
284+
detect_labels(args.path)
285+
elif args.command == 'landmarks':
286+
detect_landmarks(args.path)
287+
elif args.command == 'text':
288+
detect_text(args.path)
289+
elif args.command == 'logos':
290+
detect_logos(args.path)
291+
elif args.command == 'safe-search':
292+
detect_safe_search(args.path)
293+
elif args.command == 'properties':
294+
detect_properties(args.path)
295+
296+
297+
def run_cloud_storage(args):
298+
if args.command == 'text-gcs':
299+
detect_text_cloud_storage(args.cloud_storage_uri)
300+
elif args.command == 'faces-gcs':
301+
detect_faces_cloud_storage(args.cloud_storage_uri)
302+
elif args.command == 'labels-gcs':
303+
detect_labels_cloud_storage(args.cloud_storage_uri)
304+
elif args.command == 'landmarks-gcs':
305+
detect_landmarks_cloud_storage(args.cloud_storage_uri)
306+
elif args.command == 'logos-gcs':
307+
detect_logos_cloud_storage(args.cloud_storage_uri)
308+
elif args.command == 'safe-search-gcs':
309+
detect_safe_search_cloud_storage(args.cloud_storage_uri)
310+
elif args.command == 'properties-gcs':
311+
detect_properties_cloud_storage(args.cloud_storage_uri)
312+
313+
314+
if __name__ == '__main__':
315+
parser = argparse.ArgumentParser(
316+
description=__doc__,
317+
formatter_class=argparse.RawDescriptionHelpFormatter)
318+
subparsers = parser.add_subparsers(dest='command')
319+
320+
run_local_parser = subparsers.add_parser(
321+
'all-local', help=run_all_local.__doc__)
322+
323+
detect_faces_parser = subparsers.add_parser(
324+
'faces', help=detect_faces.__doc__)
325+
detect_faces_parser.add_argument('path')
326+
327+
faces_file_parser = subparsers.add_parser(
328+
'faces-gcs', help=detect_faces_cloud_storage.__doc__)
329+
faces_file_parser.add_argument('cloud_storage_uri')
330+
331+
detect_labels_parser = subparsers.add_parser(
332+
'labels', help=detect_labels.__doc__)
333+
detect_labels_parser.add_argument('path')
334+
335+
labels_file_parser = subparsers.add_parser(
336+
'labels-gcs', help=detect_labels_cloud_storage.__doc__)
337+
labels_file_parser.add_argument('cloud_storage_uri')
338+
339+
detect_landmarks_parser = subparsers.add_parser(
340+
'landmarks', help=detect_landmarks.__doc__)
341+
detect_landmarks_parser.add_argument('path')
342+
343+
landmark_file_parser = subparsers.add_parser(
344+
'landmarks-gcs', help=detect_landmarks_cloud_storage.__doc__)
345+
landmark_file_parser.add_argument('cloud_storage_uri')
346+
347+
detect_text_parser = subparsers.add_parser(
348+
'text', help=detect_text.__doc__)
349+
detect_text_parser.add_argument('path')
350+
351+
text_file_parser = subparsers.add_parser(
352+
'text-gcs', help=detect_text_cloud_storage.__doc__)
353+
text_file_parser.add_argument('cloud_storage_uri')
354+
355+
detect_logos_parser = subparsers.add_parser(
356+
'logos', help=detect_logos.__doc__)
357+
detect_logos_parser.add_argument('path')
358+
359+
logos_file_parser = subparsers.add_parser(
360+
'logos-gcs', help=detect_logos_cloud_storage.__doc__)
361+
logos_file_parser.add_argument('cloud_storage_uri')
362+
363+
safe_search_parser = subparsers.add_parser(
364+
'safe-search', help=detect_safe_search.__doc__)
365+
safe_search_parser.add_argument('path')
366+
367+
safe_search_file_parser = subparsers.add_parser(
368+
'safe-search-gcs',
369+
help=detect_safe_search_cloud_storage.__doc__)
370+
safe_search_file_parser.add_argument('cloud_storage_uri')
371+
372+
properties_parser = subparsers.add_parser(
373+
'properties', help=detect_safe_search.__doc__)
374+
properties_parser.add_argument('path')
375+
376+
properties_file_parser = subparsers.add_parser(
377+
'properties-gcs',
378+
help=detect_properties_cloud_storage.__doc__)
379+
properties_file_parser.add_argument('cloud_storage_uri')
380+
381+
args = parser.parse_args()
382+
383+
if ('gcs' in args.command):
384+
run_cloud_storage(args)
385+
else:
386+
run_local(args)

0 commit comments

Comments
 (0)