-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
executable file
·698 lines (529 loc) · 21.8 KB
/
detect.py
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/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.
Example Usage:
python detect.py text ./resources/wakeupcat.jpg
python detect.py labels ./resources/landmark.jpg
python detect.py web ./resources/landmark.jpg
python detect.py web-uri http://wheresgus.com/dog.JPG
python detect.py faces-uri gs://your-bucket/file.jpg
For more information, the documentation at
https://cloud.google.com/vision/docs.
"""
import sys
import argparse
import io
import re
from google.cloud import vision
from google.cloud.vision import types
reload(sys)
sys.setdefaultencoding('utf-8')
lista=['bradesco','santander', 'banking','agencia','conta','itau','IQ','banco', 'seguro','bank']
# [START def_detect_faces]
def detect_faces(path):
"""Detects faces in an image."""
client = vision.ImageAnnotatorClient()
# [START migration_face_detection]
# [START migration_image_file]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# [END migration_image_file]
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Faces:')
for face in faces:
print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print('face bounds: {}'.format(','.join(vertices)))
# [END migration_face_detection]
# [END def_detect_faces]
# [START def_detect_faces_uri]
def detect_faces_uri(uri):
"""Detects faces in the file located in Google Cloud Storage or the web."""
client = vision.ImageAnnotatorClient()
# [START migration_image_uri]
image = types.Image()
image.source.image_uri = uri
# [END migration_image_uri]
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Faces:')
for face in faces:
print('anger: {}'.format(likelihood_name[face.anger_likelihood]))
print('joy: {}'.format(likelihood_name[face.joy_likelihood]))
print('surprise: {}'.format(likelihood_name[face.surprise_likelihood]))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices])
print('face bounds: {}'.format(','.join(vertices)))
# [END def_detect_faces_uri]
# [START def_detect_labels]
def detect_labels(path):
"""Detects labels in the file."""
client = vision.ImageAnnotatorClient()
# [START migration_label_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
# [END migration_label_detection]
# [END def_detect_labels]
# [START def_detect_labels_uri]
def detect_labels_uri(uri):
"""Detects labels in the file located in Google Cloud Storage or on the
Web."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
# [END def_detect_labels_uri]
# [START def_detect_landmarks]
def detect_landmarks(path):
"""Detects landmarks in the file."""
client = vision.ImageAnnotatorClient()
# [START migration_landmark_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.landmark_detection(image=image)
landmarks = response.landmark_annotations
print('Landmarks:')
for landmark in landmarks:
print(landmark.description)
for location in landmark.locations:
lat_lng = location.lat_lng
print('Latitude'.format(lat_lng.latitude))
print('Longitude'.format(lat_lng.longitude))
# [END migration_landmark_detection]
# [END def_detect_landmarks]
# [START def_detect_landmarks_uri]
def detect_landmarks_uri(uri):
"""Detects landmarks in the file located in Google Cloud Storage or on the
Web."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.landmark_detection(image=image)
landmarks = response.landmark_annotations
print('Landmarks:')
for landmark in landmarks:
print(landmark.description)
# [END def_detect_landmarks_uri]
# [START def_detect_logos]
def detect_logos(path):
"""Detects logos in the file."""
client = vision.ImageAnnotatorClient()
# [START migration_logo_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.logo_detection(image=image)
logos = response.logo_annotations
print('Logos:')
for logo in logos:
print(logo.description)
# [END migration_logo_detection]
# [END def_detect_logos]
# [START def_detect_logos_uri]
def detect_logos_uri(uri):
"""Detects logos in the file located in Google Cloud Storage or on the Web.
"""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.logo_detection(image=image)
logos = response.logo_annotations
print('Logos:')
for logo in logos:
print(logo.description)
# [END def_detect_logos_uri]
# [START def_detect_safe_search]
def detect_safe_search(path):
"""Detects unsafe features in the file."""
client = vision.ImageAnnotatorClient()
# [START migration_safe_search_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.safe_search_detection(image=image)
safe = response.safe_search_annotation
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Safe search:')
print('adult: {}'.format(likelihood_name[safe.adult]))
print('medical: {}'.format(likelihood_name[safe.medical]))
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
print('violence: {}'.format(likelihood_name[safe.violence]))
# [END migration_safe_search_detection]
# [END def_detect_safe_search]
# [START def_detect_safe_search_uri]
def detect_safe_search_uri(uri):
"""Detects unsafe features in the file located in Google Cloud Storage or
on the Web."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.safe_search_detection(image=image)
safe = response.safe_search_annotation
# Names of likelihood from google.cloud.vision.enums
likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Safe search:')
print('adult: {}'.format(likelihood_name[safe.adult]))
print('medical: {}'.format(likelihood_name[safe.medical]))
print('spoofed: {}'.format(likelihood_name[safe.spoof]))
print('violence: {}'.format(likelihood_name[safe.violence]))
# [END def_detect_safe_search_uri]
# [START def_detect_text]
def detect_text(path):
"""Detects text in the file."""
client = vision.ImageAnnotatorClient()
# [START migration_text_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
# [END migration_text_detection]
# [END def_detect_text]
# [START def_detect_text_uri]
def detect_text_uri(uri):
"""Detects text in the file located in Google Cloud Storage or on the Web.
"""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
# [END def_detect_text_uri]
# [START def_detect_properties]
def detect_properties(path):
"""Detects image properties in the file."""
client = vision.ImageAnnotatorClient()
# [START migration_image_properties]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.image_properties(image=image)
props = response.image_properties_annotation
print('Properties:')
for color in props.dominant_colors.colors:
print('fraction: {}'.format(color.pixel_fraction))
print('\tr: {}'.format(color.color.red))
print('\tg: {}'.format(color.color.green))
print('\tb: {}'.format(color.color.blue))
print('\ta: {}'.format(color.color.alpha))
# [END migration_image_properties]
# [END def_detect_properties]
# [START def_detect_properties_uri]
def detect_properties_uri(uri):
"""Detects image properties in the file located in Google Cloud Storage or
on the Web."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.image_properties(image=image)
props = response.image_properties_annotation
print('Properties:')
for color in props.dominant_colors.colors:
print('frac: {}'.format(color.pixel_fraction))
print('\tr: {}'.format(color.color.red))
print('\tg: {}'.format(color.color.green))
print('\tb: {}'.format(color.color.blue))
print('\ta: {}'.format(color.color.alpha))
# [END def_detect_properties_uri]
# [START def_detect_web]
def detect_web(path):
"""Detects web annotations given an image."""
client = vision.ImageAnnotatorClient()
# [START migration_web_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.web_detection(image=image)
notes = response.web_detection
if notes.pages_with_matching_images:
print('\n{} Pages with matching images retrieved')
for page in notes.pages_with_matching_images:
print('Url : {}'.format(page.url))
if notes.full_matching_images:
print ('\n{} Full Matches found: '.format(
len(notes.full_matching_images)))
for image in notes.full_matching_images:
print('Url : {}'.format(image.url))
if notes.partial_matching_images:
print ('\n{} Partial Matches found: '.format(
len(notes.partial_matching_images)))
for image in notes.partial_matching_images:
print('Url : {}'.format(image.url))
if notes.web_entities:
#print ('\n{} Web entities found: '.format(len(notes.web_entities)))
for entity in notes.web_entities:
print('Score : {}'.format(entity.score))
print('Description: {}'.format(entity.description))
# [END migration_web_detection]
# [END def_detect_web]
# [START def_detect_web_uri]
def detect_web_uri(uri):
"""Detects web annotations in the file located in Google Cloud Storage."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.web_detection(image=image)
notes = response.web_detection
if notes.web_entities:
#print ('\n{} Web entities found: '.format(len(notes.web_entities)))
for entity in notes.web_entities:
#print('Score : {}'.format(entity.score))
#print('Description: {}'.format(entity.description))
#uriel
scoreMatch = (entity.score)
descri = str(entity.description)
for i in lista:
if re.search(i, descri, re.IGNORECASE):
print("found " + i)
# [END def_detect_web_uri]
# [START def_detect_crop_hints]
def detect_crop_hints(path):
"""Detects crop hints in an image."""
client = vision.ImageAnnotatorClient()
# [START migration_crop_hints]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
image_context = types.ImageContext(crop_hints_params=crop_hints_params)
response = client.crop_hints(image=image, image_context=image_context)
hints = response.crop_hints_annotation.crop_hints
for n, hint in enumerate(hints):
print('\nCrop Hint: {}'.format(n))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in hint.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
# [END migration_crop_hints]
# [END def_detect_crop_hints]
# [START def_detect_crop_hints_uri]
def detect_crop_hints_uri(uri):
"""Detects crop hints in the file located in Google Cloud Storage."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
crop_hints_params = types.CropHintsParams(aspect_ratios=[1.77])
image_context = types.ImageContext(crop_hints_params=crop_hints_params)
response = client.crop_hints(image=image, image_context=image_context)
hints = response.crop_hints_annotation.crop_hints
for n, hint in enumerate(hints):
print('\nCrop Hint: {}'.format(n))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in hint.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
# [END def_detect_crop_hints_uri]
# [START def_detect_document]
def detect_document(path):
"""Detects document features in an image."""
client = vision.ImageAnnotatorClient()
# [START migration_document_text_detection]
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.document_text_detection(image=image)
document = response.full_text_annotation
for page in document.pages:
for block in page.blocks:
block_words = []
for paragraph in block.paragraphs:
block_words.extend(paragraph.words)
block_symbols = []
for word in block_words:
block_symbols.extend(word.symbols)
block_text = ''
for symbol in block_symbols:
block_text = block_text + symbol.text
print('Block Content: {}'.format(block_text))
print('Block Bounds:\n {}'.format(block.bounding_box))
# [END migration_document_text_detection]
# [END def_detect_document]
# [START def_detect_document_uri]
def detect_document_uri(uri):
"""Detects document features in the file located in Google Cloud
Storage."""
client = vision.ImageAnnotatorClient()
image = types.Image()
image.source.image_uri = uri
response = client.document_text_detection(image=image)
document = response.full_text_annotation
for page in document.pages:
for block in page.blocks:
block_words = []
for paragraph in block.paragraphs:
block_words.extend(paragraph.words)
block_symbols = []
for word in block_words:
block_symbols.extend(word.symbols)
block_text = ''
for symbol in block_symbols:
block_text = block_text + symbol.text
print('Block Content: {}'.format(block_text))
print('Block Bounds:\n {}'.format(block.bounding_box))
# [END def_detect_document_uri]
def run_local(args):
if 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)
elif args.command == 'web':
detect_web(args.path)
elif args.command == 'crophints':
detect_crop_hints(args.path)
elif args.command == 'document':
detect_document(args.path)
def run_uri(args):
if args.command == 'text-uri':
detect_text_uri(args.uri)
elif args.command == 'faces-uri':
detect_faces_uri(args.uri)
elif args.command == 'labels-uri':
detect_labels_uri(args.uri)
elif args.command == 'landmarks-uri':
detect_landmarks_uri(args.uri)
elif args.command == 'logos-uri':
detect_logos_uri(args.uri)
elif args.command == 'safe-search-uri':
detect_safe_search_uri(args.uri)
elif args.command == 'properties-uri':
detect_properties_uri(args.uri)
elif args.command == 'web-uri':
detect_web_uri(args.uri)
elif args.command == 'crophints-uri':
detect_crop_hints_uri(args.uri)
elif args.command == 'document-uri':
detect_document_uri(args.uri)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='command')
detect_faces_parser = subparsers.add_parser(
'faces', help=detect_faces.__doc__)
detect_faces_parser.add_argument('path')
faces_file_parser = subparsers.add_parser(
'faces-uri', help=detect_faces_uri.__doc__)
faces_file_parser.add_argument('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-uri', help=detect_labels_uri.__doc__)
labels_file_parser.add_argument('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-uri', help=detect_landmarks_uri.__doc__)
landmark_file_parser.add_argument('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-uri', help=detect_text_uri.__doc__)
text_file_parser.add_argument('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-uri', help=detect_logos_uri.__doc__)
logos_file_parser.add_argument('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-uri',
help=detect_safe_search_uri.__doc__)
safe_search_file_parser.add_argument('uri')
properties_parser = subparsers.add_parser(
'properties', help=detect_properties.__doc__)
properties_parser.add_argument('path')
properties_file_parser = subparsers.add_parser(
'properties-uri',
help=detect_properties_uri.__doc__)
properties_file_parser.add_argument('uri')
# 1.1 Vision features
web_parser = subparsers.add_parser(
'web', help=detect_web.__doc__)
web_parser.add_argument('path')
web_uri_parser = subparsers.add_parser(
'web-uri',
help=detect_web_uri.__doc__)
web_uri_parser.add_argument('uri')
crop_hints_parser = subparsers.add_parser(
'crophints', help=detect_crop_hints.__doc__)
crop_hints_parser.add_argument('path')
crop_hints_uri_parser = subparsers.add_parser(
'crophints-uri', help=detect_crop_hints_uri.__doc__)
crop_hints_uri_parser.add_argument('uri')
document_parser = subparsers.add_parser(
'document', help=detect_document.__doc__)
document_parser.add_argument('path')
document_uri_parser = subparsers.add_parser(
'document-uri', help=detect_document_uri.__doc__)
document_uri_parser.add_argument('uri')
args = parser.parse_args()
if ('uri' in args.command):
run_uri(args)
else:
run_local(args)
#scoreMatch = (entity.score)
#descri = (entity.description)