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