Skip to content

Commit eb4ef64

Browse files
committed
add region tags
1 parent 7fb9d82 commit eb4ef64

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

vision/cloud-client/detect/detect.py

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,26 @@
3434
from google.cloud import vision
3535
from google.cloud.vision import types
3636

37-
# Names of likelihood from google.cloud.vision.enums
38-
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
39-
'LIKELY', 'VERY_LIKELY')
40-
4137

38+
# [START def_detect_faces]
4239
def detect_faces(path):
4340
"""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-
4841
client = vision.ImageAnnotatorClient()
4942

43+
# [START migration_face_detection]
44+
# [START migration_image_file]
5045
with io.open(path, 'rb') as image_file:
5146
content = image_file.read()
5247

5348
image = types.Image(content=content)
49+
# [END migration_image_file]
5450

5551
response = client.face_detection(image=image)
5652
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')
5757
print('Faces:')
5858

5959
for face in faces:
@@ -65,20 +65,25 @@ def detect_faces(path):
6565
for vertex in face.bounding_poly.vertices])
6666

6767
print('face bounds: {}'.format(','.join(vertices)))
68+
# [END migration_face_detection]
69+
# [END def_detect_faces]
6870

6971

72+
# [START def_detect_faces_uri]
7073
def detect_faces_uri(uri):
7174
"""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-
7675
client = vision.ImageAnnotatorClient()
76+
# [START migration_image_uri]
7777
image = types.Image()
7878
image.source.image_uri = uri
79+
# [END migration_image_uri]
7980

8081
response = client.face_detection(image=image)
8182
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')
8287
print('Faces:')
8388

8489
for face in faces:
@@ -90,12 +95,15 @@ def detect_faces_uri(uri):
9095
for vertex in face.bounding_poly.vertices])
9196

9297
print('face bounds: {}'.format(','.join(vertices)))
98+
# [END def_detect_faces_uri]
9399

94100

101+
# [START def_detect_labels]
95102
def detect_labels(path):
96103
"""Detects labels in the file."""
97104
client = vision.ImageAnnotatorClient()
98105

106+
# [START migration_label_detection]
99107
with io.open(path, 'rb') as image_file:
100108
content = image_file.read()
101109

@@ -107,8 +115,11 @@ def detect_labels(path):
107115

108116
for label in labels:
109117
print(label.description)
118+
# [END migration_label_detection]
119+
# [END def_detect_labels]
110120

111121

122+
# [START def_detect_labels_uri]
112123
def detect_labels_uri(uri):
113124
"""Detects labels in the file located in Google Cloud Storage or on the
114125
Web."""
@@ -122,12 +133,15 @@ def detect_labels_uri(uri):
122133

123134
for label in labels:
124135
print(label.description)
136+
# [END def_detect_labels_uri]
125137

126138

139+
# [START def_detect_landmarks]
127140
def detect_landmarks(path):
128141
"""Detects landmarks in the file."""
129142
client = vision.ImageAnnotatorClient()
130143

144+
# [START migration_landmark_detection]
131145
with io.open(path, 'rb') as image_file:
132146
content = image_file.read()
133147

@@ -139,8 +153,15 @@ def detect_landmarks(path):
139153

140154
for landmark in landmarks:
141155
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]
142162

143163

164+
# [START def_detect_landmarks_uri]
144165
def detect_landmarks_uri(uri):
145166
"""Detects landmarks in the file located in Google Cloud Storage or on the
146167
Web."""
@@ -154,12 +175,15 @@ def detect_landmarks_uri(uri):
154175

155176
for landmark in landmarks:
156177
print(landmark.description)
178+
# [END def_detect_landmarks_uri]
157179

158180

181+
# [START def_detect_logos]
159182
def detect_logos(path):
160183
"""Detects logos in the file."""
161184
client = vision.ImageAnnotatorClient()
162185

186+
# [START migration_logo_detection]
163187
with io.open(path, 'rb') as image_file:
164188
content = image_file.read()
165189

@@ -171,8 +195,11 @@ def detect_logos(path):
171195

172196
for logo in logos:
173197
print(logo.description)
198+
# [END migration_logo_detection]
199+
# [END def_detect_logos]
174200

175201

202+
# [START def_detect_logos_uri]
176203
def detect_logos_uri(uri):
177204
"""Detects logos in the file located in Google Cloud Storage or on the Web.
178205
"""
@@ -186,54 +213,65 @@ def detect_logos_uri(uri):
186213

187214
for logo in logos:
188215
print(logo.description)
216+
# [END def_detect_logos_uri]
189217

190218

219+
# [START def_detect_safe_search]
191220
def detect_safe_search(path):
192221
"""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-
197222
client = vision.ImageAnnotatorClient()
198223

224+
# [START migration_safe_search_detection]
199225
with io.open(path, 'rb') as image_file:
200226
content = image_file.read()
201227

202228
image = types.Image(content=content)
203229

204230
response = client.safe_search_detection(image=image)
205231
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')
206236
print('Safe search:')
237+
207238
print('adult: {}'.format(likelihood_name[safe.adult]))
208239
print('medical: {}'.format(likelihood_name[safe.medical]))
209240
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
210241
print('violence: {}'.format(likelihood_name[safe.violence]))
242+
# [END migration_safe_search_detection]
243+
# [END def_detect_safe_search]
211244

212245

246+
# [START def_detect_safe_search_uri]
213247
def detect_safe_search_uri(uri):
214248
"""Detects unsafe features in the file located in Google Cloud Storage or
215249
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-
220250
client = vision.ImageAnnotatorClient()
221251
image = types.Image()
222252
image.source.image_uri = uri
223253

224254
response = client.safe_search_detection(image=image)
225255
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')
226260
print('Safe search:')
261+
227262
print('adult: {}'.format(likelihood_name[safe.adult]))
228263
print('medical: {}'.format(likelihood_name[safe.medical]))
229264
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
230265
print('violence: {}'.format(likelihood_name[safe.violence]))
266+
# [END def_detect_safe_search_uri]
231267

232268

269+
# [START def_detect_text]
233270
def detect_text(path):
234271
"""Detects text in the file."""
235272
client = vision.ImageAnnotatorClient()
236273

274+
# [START migration_text_detection]
237275
with io.open(path, 'rb') as image_file:
238276
content = image_file.read()
239277

@@ -250,8 +288,11 @@ def detect_text(path):
250288
for vertex in text.bounding_poly.vertices])
251289

252290
print('bounds: {}'.format(','.join(vertices)))
291+
# [END migration_text_detection]
292+
# [END def_detect_text]
253293

254294

295+
# [START def_detect_text_uri]
255296
def detect_text_uri(uri):
256297
"""Detects text in the file located in Google Cloud Storage or on the Web.
257298
"""
@@ -270,12 +311,15 @@ def detect_text_uri(uri):
270311
for vertex in text.bounding_poly.vertices])
271312

272313
print('bounds: {}'.format(','.join(vertices)))
314+
# [END def_detect_text_uri]
273315

274316

317+
# [START def_detect_properties]
275318
def detect_properties(path):
276319
"""Detects image properties in the file."""
277320
client = vision.ImageAnnotatorClient()
278321

322+
# [START migration_image_properties]
279323
with io.open(path, 'rb') as image_file:
280324
content = image_file.read()
281325

@@ -291,8 +335,11 @@ def detect_properties(path):
291335
print('\tg: {}'.format(color.color.green))
292336
print('\tb: {}'.format(color.color.blue))
293337
print('\ta: {}'.format(color.color.alpha))
338+
# [END migration_image_properties]
339+
# [END def_detect_properties]
294340

295341

342+
# [START def_detect_properties_uri]
296343
def detect_properties_uri(uri):
297344
"""Detects image properties in the file located in Google Cloud Storage or
298345
on the Web."""
@@ -310,12 +357,15 @@ def detect_properties_uri(uri):
310357
print('\tg: {}'.format(color.color.green))
311358
print('\tb: {}'.format(color.color.blue))
312359
print('\ta: {}'.format(color.color.alpha))
360+
# [END def_detect_properties_uri]
313361

314362

363+
# [START def_detect_web]
315364
def detect_web(path):
316365
"""Detects web annotations given an image."""
317366
client = vision.ImageAnnotatorClient()
318367

368+
# [START migration_web_detection]
319369
with io.open(path, 'rb') as image_file:
320370
content = image_file.read()
321371

@@ -350,8 +400,11 @@ def detect_web(path):
350400
for entity in notes.web_entities:
351401
print('Score : {}'.format(entity.score))
352402
print('Description: {}'.format(entity.description))
403+
# [END migration_web_detection]
404+
# [END def_detect_web]
353405

354406

407+
# [START def_detect_web_uri]
355408
def detect_web_uri(uri):
356409
"""Detects web annotations in the file located in Google Cloud Storage."""
357410
client = vision.ImageAnnotatorClient()
@@ -387,11 +440,15 @@ def detect_web_uri(uri):
387440
for entity in notes.web_entities:
388441
print('Score : {}'.format(entity.score))
389442
print('Description: {}'.format(entity.description))
443+
# [END def_detect_web_uri]
390444

391445

446+
# [START def_detect_crop_hints]
392447
def detect_crop_hints(path):
393448
"""Detects crop hints in an image."""
394449
client = vision.ImageAnnotatorClient()
450+
451+
# [START migration_crop_hints]
395452
with io.open(path, 'rb') as image_file:
396453
content = image_file.read()
397454
image = types.Image(content=content)
@@ -409,8 +466,11 @@ def detect_crop_hints(path):
409466
for vertex in hint.bounding_poly.vertices])
410467

411468
print('bounds: {}'.format(','.join(vertices)))
469+
# [END migration_crop_hints]
470+
# [END def_detect_crop_hints]
412471

413472

473+
# [START def_detect_crop_hints_uri]
414474
def detect_crop_hints_uri(uri):
415475
"""Detects crop hints in the file located in Google Cloud Storage."""
416476
client = vision.ImageAnnotatorClient()
@@ -430,12 +490,15 @@ def detect_crop_hints_uri(uri):
430490
for vertex in hint.bounding_poly.vertices])
431491

432492
print('bounds: {}'.format(','.join(vertices)))
493+
# [END def_detect_crop_hints_uri]
433494

434495

496+
# [START def_detect_document]
435497
def detect_document(path):
436498
"""Detects document features in an image."""
437499
client = vision.ImageAnnotatorClient()
438500

501+
# [START migration_document_text_detection]
439502
with io.open(path, 'rb') as image_file:
440503
content = image_file.read()
441504

@@ -460,8 +523,11 @@ def detect_document(path):
460523

461524
print('Block Content: {}'.format(block_text))
462525
print('Block Bounds:\n {}'.format(block.bounding_box))
526+
# [END migration_document_text_detection]
527+
# [END def_detect_document]
463528

464529

530+
# [START def_detect_document_uri]
465531
def detect_document_uri(uri):
466532
"""Detects document features in the file located in Google Cloud
467533
Storage."""
@@ -488,6 +554,7 @@ def detect_document_uri(uri):
488554

489555
print('Block Content: {}'.format(block_text))
490556
print('Block Bounds:\n {}'.format(block.bounding_box))
557+
# [END def_detect_document_uri]
491558

492559

493560
def run_local(args):

vision/cloud-client/face_detection/faces.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from PIL import Image, ImageDraw
2626

2727

28+
# [START def_detect_face]
2829
def detect_face(face_file, max_results=4):
2930
"""Uses the Vision API to detect faces in the given file.
3031
@@ -42,8 +43,10 @@ def detect_face(face_file, max_results=4):
4243
image = types.Image(content=content)
4344

4445
return client.face_detection(image=image).face_annotations
46+
# [END def_detect_face]
4547

4648

49+
# [START def_highlight_faces]
4750
def highlight_faces(image, faces, output_filename):
4851
"""Draws a polygon around the faces, then saves to output_filename.
4952
@@ -63,8 +66,10 @@ def highlight_faces(image, faces, output_filename):
6366
draw.line(box + [box[0]], width=5, fill='#00ff00')
6467

6568
im.save(output_filename)
69+
# [END def_highlight_faces]
6670

6771

72+
# [START def_main]
6873
def main(input_filename, output_filename, max_results):
6974
with open(input_filename, 'rb') as image:
7075
faces = detect_face(image, max_results)
@@ -75,6 +80,7 @@ def main(input_filename, output_filename, max_results):
7580
# Reset the file pointer, so we can read the file again
7681
image.seek(0)
7782
highlight_faces(image, faces, output_filename)
83+
# [END def_main]
7884

7985

8086
if __name__ == '__main__':

0 commit comments

Comments
 (0)