34
34
from google .cloud import vision
35
35
from google .cloud .vision import types
36
36
37
- # Names of likelihood from google.cloud.vision.enums
38
- likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
39
- 'LIKELY' , 'VERY_LIKELY' )
40
-
41
37
38
+ # [START def_detect_faces]
42
39
def detect_faces (path ):
43
40
"""Detects faces in an image."""
44
- # Names of likelihood from google.cloud.vision.enums
45
- likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
46
- 'LIKELY' , 'VERY_LIKELY' )
47
-
48
41
client = vision .ImageAnnotatorClient ()
49
42
43
+ # [START migration_face_detection]
44
+ # [START migration_image_file]
50
45
with io .open (path , 'rb' ) as image_file :
51
46
content = image_file .read ()
52
47
53
48
image = types .Image (content = content )
49
+ # [END migration_image_file]
54
50
55
51
response = client .face_detection (image = image )
56
52
faces = response .face_annotations
53
+
54
+ # Names of likelihood from google.cloud.vision.enums
55
+ likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
56
+ 'LIKELY' , 'VERY_LIKELY' )
57
57
print ('Faces:' )
58
58
59
59
for face in faces :
@@ -65,20 +65,25 @@ def detect_faces(path):
65
65
for vertex in face .bounding_poly .vertices ])
66
66
67
67
print ('face bounds: {}' .format (',' .join (vertices )))
68
+ # [END migration_face_detection]
69
+ # [END def_detect_faces]
68
70
69
71
72
+ # [START def_detect_faces_uri]
70
73
def detect_faces_uri (uri ):
71
74
"""Detects faces in the file located in Google Cloud Storage or the web."""
72
- # Names of likelihood from google.cloud.vision.enums
73
- likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
74
- 'LIKELY' , 'VERY_LIKELY' )
75
-
76
75
client = vision .ImageAnnotatorClient ()
76
+ # [START migration_image_uri]
77
77
image = types .Image ()
78
78
image .source .image_uri = uri
79
+ # [END migration_image_uri]
79
80
80
81
response = client .face_detection (image = image )
81
82
faces = response .face_annotations
83
+
84
+ # Names of likelihood from google.cloud.vision.enums
85
+ likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
86
+ 'LIKELY' , 'VERY_LIKELY' )
82
87
print ('Faces:' )
83
88
84
89
for face in faces :
@@ -90,12 +95,15 @@ def detect_faces_uri(uri):
90
95
for vertex in face .bounding_poly .vertices ])
91
96
92
97
print ('face bounds: {}' .format (',' .join (vertices )))
98
+ # [END def_detect_faces_uri]
93
99
94
100
101
+ # [START def_detect_labels]
95
102
def detect_labels (path ):
96
103
"""Detects labels in the file."""
97
104
client = vision .ImageAnnotatorClient ()
98
105
106
+ # [START migration_label_detection]
99
107
with io .open (path , 'rb' ) as image_file :
100
108
content = image_file .read ()
101
109
@@ -107,8 +115,11 @@ def detect_labels(path):
107
115
108
116
for label in labels :
109
117
print (label .description )
118
+ # [END migration_label_detection]
119
+ # [END def_detect_labels]
110
120
111
121
122
+ # [START def_detect_labels_uri]
112
123
def detect_labels_uri (uri ):
113
124
"""Detects labels in the file located in Google Cloud Storage or on the
114
125
Web."""
@@ -122,12 +133,15 @@ def detect_labels_uri(uri):
122
133
123
134
for label in labels :
124
135
print (label .description )
136
+ # [END def_detect_labels_uri]
125
137
126
138
139
+ # [START def_detect_landmarks]
127
140
def detect_landmarks (path ):
128
141
"""Detects landmarks in the file."""
129
142
client = vision .ImageAnnotatorClient ()
130
143
144
+ # [START migration_landmark_detection]
131
145
with io .open (path , 'rb' ) as image_file :
132
146
content = image_file .read ()
133
147
@@ -139,8 +153,15 @@ def detect_landmarks(path):
139
153
140
154
for landmark in landmarks :
141
155
print (landmark .description )
156
+ for location in landmark .locations :
157
+ lat_lng = location .lat_lng
158
+ print ('Latitude' .format (lat_lng .latitude ))
159
+ print ('Longitude' .format (lat_lng .longitude ))
160
+ # [END migration_landmark_detection]
161
+ # [END def_detect_landmarks]
142
162
143
163
164
+ # [START def_detect_landmarks_uri]
144
165
def detect_landmarks_uri (uri ):
145
166
"""Detects landmarks in the file located in Google Cloud Storage or on the
146
167
Web."""
@@ -154,12 +175,15 @@ def detect_landmarks_uri(uri):
154
175
155
176
for landmark in landmarks :
156
177
print (landmark .description )
178
+ # [END def_detect_landmarks_uri]
157
179
158
180
181
+ # [START def_detect_logos]
159
182
def detect_logos (path ):
160
183
"""Detects logos in the file."""
161
184
client = vision .ImageAnnotatorClient ()
162
185
186
+ # [START migration_logo_detection]
163
187
with io .open (path , 'rb' ) as image_file :
164
188
content = image_file .read ()
165
189
@@ -171,8 +195,11 @@ def detect_logos(path):
171
195
172
196
for logo in logos :
173
197
print (logo .description )
198
+ # [END migration_logo_detection]
199
+ # [END def_detect_logos]
174
200
175
201
202
+ # [START def_detect_logos_uri]
176
203
def detect_logos_uri (uri ):
177
204
"""Detects logos in the file located in Google Cloud Storage or on the Web.
178
205
"""
@@ -186,54 +213,65 @@ def detect_logos_uri(uri):
186
213
187
214
for logo in logos :
188
215
print (logo .description )
216
+ # [END def_detect_logos_uri]
189
217
190
218
219
+ # [START def_detect_safe_search]
191
220
def detect_safe_search (path ):
192
221
"""Detects unsafe features in the file."""
193
- # Names of likelihood from google.cloud.vision.enums
194
- likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
195
- 'LIKELY' , 'VERY_LIKELY' )
196
-
197
222
client = vision .ImageAnnotatorClient ()
198
223
224
+ # [START migration_safe_search_detection]
199
225
with io .open (path , 'rb' ) as image_file :
200
226
content = image_file .read ()
201
227
202
228
image = types .Image (content = content )
203
229
204
230
response = client .safe_search_detection (image = image )
205
231
safe = response .safe_search_annotation
232
+
233
+ # Names of likelihood from google.cloud.vision.enums
234
+ likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
235
+ 'LIKELY' , 'VERY_LIKELY' )
206
236
print ('Safe search:' )
237
+
207
238
print ('adult: {}' .format (likelihood_name [safe .adult ]))
208
239
print ('medical: {}' .format (likelihood_name [safe .medical ]))
209
240
print ('spoofed: {}' .format (likelihood_name [safe .spoof ]))
210
241
print ('violence: {}' .format (likelihood_name [safe .violence ]))
242
+ # [END migration_safe_search_detection]
243
+ # [END def_detect_safe_search]
211
244
212
245
246
+ # [START def_detect_safe_search_uri]
213
247
def detect_safe_search_uri (uri ):
214
248
"""Detects unsafe features in the file located in Google Cloud Storage or
215
249
on the Web."""
216
- # Names of likelihood from google.cloud.vision.enums
217
- likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
218
- 'LIKELY' , 'VERY_LIKELY' )
219
-
220
250
client = vision .ImageAnnotatorClient ()
221
251
image = types .Image ()
222
252
image .source .image_uri = uri
223
253
224
254
response = client .safe_search_detection (image = image )
225
255
safe = response .safe_search_annotation
256
+
257
+ # Names of likelihood from google.cloud.vision.enums
258
+ likelihood_name = ('UNKNOWN' , 'VERY_UNLIKELY' , 'UNLIKELY' , 'POSSIBLE' ,
259
+ 'LIKELY' , 'VERY_LIKELY' )
226
260
print ('Safe search:' )
261
+
227
262
print ('adult: {}' .format (likelihood_name [safe .adult ]))
228
263
print ('medical: {}' .format (likelihood_name [safe .medical ]))
229
264
print ('spoofed: {}' .format (likelihood_name [safe .spoof ]))
230
265
print ('violence: {}' .format (likelihood_name [safe .violence ]))
266
+ # [END def_detect_safe_search_uri]
231
267
232
268
269
+ # [START def_detect_text]
233
270
def detect_text (path ):
234
271
"""Detects text in the file."""
235
272
client = vision .ImageAnnotatorClient ()
236
273
274
+ # [START migration_text_detection]
237
275
with io .open (path , 'rb' ) as image_file :
238
276
content = image_file .read ()
239
277
@@ -250,8 +288,11 @@ def detect_text(path):
250
288
for vertex in text .bounding_poly .vertices ])
251
289
252
290
print ('bounds: {}' .format (',' .join (vertices )))
291
+ # [END migration_text_detection]
292
+ # [END def_detect_text]
253
293
254
294
295
+ # [START def_detect_text_uri]
255
296
def detect_text_uri (uri ):
256
297
"""Detects text in the file located in Google Cloud Storage or on the Web.
257
298
"""
@@ -270,12 +311,15 @@ def detect_text_uri(uri):
270
311
for vertex in text .bounding_poly .vertices ])
271
312
272
313
print ('bounds: {}' .format (',' .join (vertices )))
314
+ # [END def_detect_text_uri]
273
315
274
316
317
+ # [START def_detect_properties]
275
318
def detect_properties (path ):
276
319
"""Detects image properties in the file."""
277
320
client = vision .ImageAnnotatorClient ()
278
321
322
+ # [START migration_image_properties]
279
323
with io .open (path , 'rb' ) as image_file :
280
324
content = image_file .read ()
281
325
@@ -291,8 +335,11 @@ def detect_properties(path):
291
335
print ('\t g: {}' .format (color .color .green ))
292
336
print ('\t b: {}' .format (color .color .blue ))
293
337
print ('\t a: {}' .format (color .color .alpha ))
338
+ # [END migration_image_properties]
339
+ # [END def_detect_properties]
294
340
295
341
342
+ # [START def_detect_properties_uri]
296
343
def detect_properties_uri (uri ):
297
344
"""Detects image properties in the file located in Google Cloud Storage or
298
345
on the Web."""
@@ -310,12 +357,15 @@ def detect_properties_uri(uri):
310
357
print ('\t g: {}' .format (color .color .green ))
311
358
print ('\t b: {}' .format (color .color .blue ))
312
359
print ('\t a: {}' .format (color .color .alpha ))
360
+ # [END def_detect_properties_uri]
313
361
314
362
363
+ # [START def_detect_web]
315
364
def detect_web (path ):
316
365
"""Detects web annotations given an image."""
317
366
client = vision .ImageAnnotatorClient ()
318
367
368
+ # [START migration_web_detection]
319
369
with io .open (path , 'rb' ) as image_file :
320
370
content = image_file .read ()
321
371
@@ -350,8 +400,11 @@ def detect_web(path):
350
400
for entity in notes .web_entities :
351
401
print ('Score : {}' .format (entity .score ))
352
402
print ('Description: {}' .format (entity .description ))
403
+ # [END migration_web_detection]
404
+ # [END def_detect_web]
353
405
354
406
407
+ # [START def_detect_web_uri]
355
408
def detect_web_uri (uri ):
356
409
"""Detects web annotations in the file located in Google Cloud Storage."""
357
410
client = vision .ImageAnnotatorClient ()
@@ -387,11 +440,15 @@ def detect_web_uri(uri):
387
440
for entity in notes .web_entities :
388
441
print ('Score : {}' .format (entity .score ))
389
442
print ('Description: {}' .format (entity .description ))
443
+ # [END def_detect_web_uri]
390
444
391
445
446
+ # [START def_detect_crop_hints]
392
447
def detect_crop_hints (path ):
393
448
"""Detects crop hints in an image."""
394
449
client = vision .ImageAnnotatorClient ()
450
+
451
+ # [START migration_crop_hints]
395
452
with io .open (path , 'rb' ) as image_file :
396
453
content = image_file .read ()
397
454
image = types .Image (content = content )
@@ -409,8 +466,11 @@ def detect_crop_hints(path):
409
466
for vertex in hint .bounding_poly .vertices ])
410
467
411
468
print ('bounds: {}' .format (',' .join (vertices )))
469
+ # [END migration_crop_hints]
470
+ # [END def_detect_crop_hints]
412
471
413
472
473
+ # [START def_detect_crop_hints_uri]
414
474
def detect_crop_hints_uri (uri ):
415
475
"""Detects crop hints in the file located in Google Cloud Storage."""
416
476
client = vision .ImageAnnotatorClient ()
@@ -430,12 +490,15 @@ def detect_crop_hints_uri(uri):
430
490
for vertex in hint .bounding_poly .vertices ])
431
491
432
492
print ('bounds: {}' .format (',' .join (vertices )))
493
+ # [END def_detect_crop_hints_uri]
433
494
434
495
496
+ # [START def_detect_document]
435
497
def detect_document (path ):
436
498
"""Detects document features in an image."""
437
499
client = vision .ImageAnnotatorClient ()
438
500
501
+ # [START migration_document_text_detection]
439
502
with io .open (path , 'rb' ) as image_file :
440
503
content = image_file .read ()
441
504
@@ -460,8 +523,11 @@ def detect_document(path):
460
523
461
524
print ('Block Content: {}' .format (block_text ))
462
525
print ('Block Bounds:\n {}' .format (block .bounding_box ))
526
+ # [END migration_document_text_detection]
527
+ # [END def_detect_document]
463
528
464
529
530
+ # [START def_detect_document_uri]
465
531
def detect_document_uri (uri ):
466
532
"""Detects document features in the file located in Google Cloud
467
533
Storage."""
@@ -488,6 +554,7 @@ def detect_document_uri(uri):
488
554
489
555
print ('Block Content: {}' .format (block_text ))
490
556
print ('Block Bounds:\n {}' .format (block .bounding_box ))
557
+ # [END def_detect_document_uri]
491
558
492
559
493
560
def run_local (args ):
0 commit comments