24
24
python detect.py web-uri http://wheresgus.com/dog.JPG
25
25
python detect.py web-geo ./resources/city.jpg
26
26
python detect.py faces-uri gs://your-bucket/file.jpg
27
- python detect_pdf .py ocr-uri gs://python-docs-samples-tests/HodgeConj.pdf \
27
+ python detect .py ocr-uri gs://python-docs-samples-tests/HodgeConj.pdf \
28
28
gs://BUCKET_NAME/PREFIX/
29
+ python detect.py object-localization ./resources/puppies.jpg
30
+ python detect.py object-localization-uri gs://...
29
31
30
32
For more information, the documentation at
31
33
https://cloud.google.com/vision/docs.
35
37
import io
36
38
import re
37
39
38
- from google .cloud import storage
39
- from google .cloud import vision
40
- from google .protobuf import json_format
41
-
42
40
43
41
# [START vision_face_detection]
44
42
def detect_faces (path ):
45
43
"""Detects faces in an image."""
44
+ from google .cloud import vision
46
45
client = vision .ImageAnnotatorClient ()
47
46
48
47
# [START vision_python_migration_face_detection]
@@ -77,6 +76,7 @@ def detect_faces(path):
77
76
# [START vision_face_detection_gcs]
78
77
def detect_faces_uri (uri ):
79
78
"""Detects faces in the file located in Google Cloud Storage or the web."""
79
+ from google .cloud import vision
80
80
client = vision .ImageAnnotatorClient ()
81
81
# [START vision_python_migration_image_uri]
82
82
image = vision .types .Image ()
@@ -106,6 +106,7 @@ def detect_faces_uri(uri):
106
106
# [START vision_label_detection]
107
107
def detect_labels (path ):
108
108
"""Detects labels in the file."""
109
+ from google .cloud import vision
109
110
client = vision .ImageAnnotatorClient ()
110
111
111
112
# [START vision_python_migration_label_detection]
@@ -128,6 +129,7 @@ def detect_labels(path):
128
129
def detect_labels_uri (uri ):
129
130
"""Detects labels in the file located in Google Cloud Storage or on the
130
131
Web."""
132
+ from google .cloud import vision
131
133
client = vision .ImageAnnotatorClient ()
132
134
image = vision .types .Image ()
133
135
image .source .image_uri = uri
@@ -144,6 +146,7 @@ def detect_labels_uri(uri):
144
146
# [START vision_landmark_detection]
145
147
def detect_landmarks (path ):
146
148
"""Detects landmarks in the file."""
149
+ from google .cloud import vision
147
150
client = vision .ImageAnnotatorClient ()
148
151
149
152
# [START vision_python_migration_landmark_detection]
@@ -170,6 +173,7 @@ def detect_landmarks(path):
170
173
def detect_landmarks_uri (uri ):
171
174
"""Detects landmarks in the file located in Google Cloud Storage or on the
172
175
Web."""
176
+ from google .cloud import vision
173
177
client = vision .ImageAnnotatorClient ()
174
178
image = vision .types .Image ()
175
179
image .source .image_uri = uri
@@ -186,6 +190,7 @@ def detect_landmarks_uri(uri):
186
190
# [START vision_logo_detection]
187
191
def detect_logos (path ):
188
192
"""Detects logos in the file."""
193
+ from google .cloud import vision
189
194
client = vision .ImageAnnotatorClient ()
190
195
191
196
# [START vision_python_migration_logo_detection]
@@ -208,6 +213,7 @@ def detect_logos(path):
208
213
def detect_logos_uri (uri ):
209
214
"""Detects logos in the file located in Google Cloud Storage or on the Web.
210
215
"""
216
+ from google .cloud import vision
211
217
client = vision .ImageAnnotatorClient ()
212
218
image = vision .types .Image ()
213
219
image .source .image_uri = uri
@@ -224,6 +230,7 @@ def detect_logos_uri(uri):
224
230
# [START vision_safe_search_detection]
225
231
def detect_safe_search (path ):
226
232
"""Detects unsafe features in the file."""
233
+ from google .cloud import vision
227
234
client = vision .ImageAnnotatorClient ()
228
235
229
236
# [START vision_python_migration_safe_search_detection]
@@ -253,6 +260,7 @@ def detect_safe_search(path):
253
260
def detect_safe_search_uri (uri ):
254
261
"""Detects unsafe features in the file located in Google Cloud Storage or
255
262
on the Web."""
263
+ from google .cloud import vision
256
264
client = vision .ImageAnnotatorClient ()
257
265
image = vision .types .Image ()
258
266
image .source .image_uri = uri
@@ -276,6 +284,7 @@ def detect_safe_search_uri(uri):
276
284
# [START vision_text_detection]
277
285
def detect_text (path ):
278
286
"""Detects text in the file."""
287
+ from google .cloud import vision
279
288
client = vision .ImageAnnotatorClient ()
280
289
281
290
# [START vision_python_migration_text_detection]
@@ -303,6 +312,7 @@ def detect_text(path):
303
312
def detect_text_uri (uri ):
304
313
"""Detects text in the file located in Google Cloud Storage or on the Web.
305
314
"""
315
+ from google .cloud import vision
306
316
client = vision .ImageAnnotatorClient ()
307
317
image = vision .types .Image ()
308
318
image .source .image_uri = uri
@@ -324,6 +334,7 @@ def detect_text_uri(uri):
324
334
# [START vision_image_property_detection]
325
335
def detect_properties (path ):
326
336
"""Detects image properties in the file."""
337
+ from google .cloud import vision
327
338
client = vision .ImageAnnotatorClient ()
328
339
329
340
# [START vision_python_migration_image_properties]
@@ -350,6 +361,7 @@ def detect_properties(path):
350
361
def detect_properties_uri (uri ):
351
362
"""Detects image properties in the file located in Google Cloud Storage or
352
363
on the Web."""
364
+ from google .cloud import vision
353
365
client = vision .ImageAnnotatorClient ()
354
366
image = vision .types .Image ()
355
367
image .source .image_uri = uri
@@ -370,6 +382,7 @@ def detect_properties_uri(uri):
370
382
# [START vision_web_detection]
371
383
def detect_web (path ):
372
384
"""Detects web annotations given an image."""
385
+ from google .cloud import vision
373
386
client = vision .ImageAnnotatorClient ()
374
387
375
388
# [START vision_python_migration_web_detection]
@@ -427,6 +440,7 @@ def detect_web(path):
427
440
# [START vision_web_detection_gcs]
428
441
def detect_web_uri (uri ):
429
442
"""Detects web annotations in the file located in Google Cloud Storage."""
443
+ from google .cloud import vision
430
444
client = vision .ImageAnnotatorClient ()
431
445
image = vision .types .Image ()
432
446
image .source .image_uri = uri
@@ -480,6 +494,7 @@ def detect_web_uri(uri):
480
494
def web_entities_include_geo_results (path ):
481
495
"""Detects web annotations given an image, using the geotag metadata
482
496
in the image to detect web entities."""
497
+ from google .cloud import vision
483
498
client = vision .ImageAnnotatorClient ()
484
499
485
500
with io .open (path , 'rb' ) as image_file :
@@ -505,6 +520,7 @@ def web_entities_include_geo_results_uri(uri):
505
520
"""Detects web annotations given an image in the file located in
506
521
Google Cloud Storage., using the geotag metadata in the image to
507
522
detect web entities."""
523
+ from google .cloud import vision
508
524
client = vision .ImageAnnotatorClient ()
509
525
510
526
image = vision .types .Image ()
@@ -526,6 +542,7 @@ def web_entities_include_geo_results_uri(uri):
526
542
# [START vision_crop_hint_detection]
527
543
def detect_crop_hints (path ):
528
544
"""Detects crop hints in an image."""
545
+ from google .cloud import vision
529
546
client = vision .ImageAnnotatorClient ()
530
547
531
548
# [START vision_python_migration_crop_hints]
@@ -554,6 +571,7 @@ def detect_crop_hints(path):
554
571
# [START vision_crop_hint_detection_gcs]
555
572
def detect_crop_hints_uri (uri ):
556
573
"""Detects crop hints in the file located in Google Cloud Storage."""
574
+ from google .cloud import vision
557
575
client = vision .ImageAnnotatorClient ()
558
576
image = vision .types .Image ()
559
577
image .source .image_uri = uri
@@ -578,6 +596,7 @@ def detect_crop_hints_uri(uri):
578
596
# [START vision_fulltext_detection]
579
597
def detect_document (path ):
580
598
"""Detects document features in an image."""
599
+ from google .cloud import vision
581
600
client = vision .ImageAnnotatorClient ()
582
601
583
602
# [START vision_python_migration_document_text_detection]
@@ -614,6 +633,7 @@ def detect_document(path):
614
633
def detect_document_uri (uri ):
615
634
"""Detects document features in the file located in Google Cloud
616
635
Storage."""
636
+ from google .cloud import vision
617
637
client = vision .ImageAnnotatorClient ()
618
638
image = vision .types .Image ()
619
639
image .source .image_uri = uri
@@ -644,6 +664,9 @@ def detect_document_uri(uri):
644
664
# [START vision_text_detection_pdf_gcs]
645
665
def async_detect_document (gcs_source_uri , gcs_destination_uri ):
646
666
"""OCR with PDF/TIFF as source files on GCS"""
667
+ from google .cloud import vision
668
+ from google .cloud import storage
669
+ from google .protobuf import json_format
647
670
# Supported mime_types are: 'application/pdf' and 'image/tiff'
648
671
mime_type = 'application/pdf'
649
672
@@ -711,6 +734,57 @@ def async_detect_document(gcs_source_uri, gcs_destination_uri):
711
734
# [END vision_text_detection_pdf_gcs]
712
735
713
736
737
+ # [START vision_localize_objects]
738
+ def localize_objects (path ):
739
+ """Localize objects in the local image.
740
+
741
+ Args:
742
+ path: The path to the local file.
743
+ """
744
+ from google .cloud import vision
745
+ client = vision .ImageAnnotatorClient ()
746
+
747
+ with open (path , 'rb' ) as image_file :
748
+ content = image_file .read ()
749
+ image = vision .types .Image (content = content )
750
+
751
+ objects = client .object_localization (
752
+ image = image ).localized_object_annotations
753
+
754
+ print ('Number of objects found: {}' .format (len (objects )))
755
+ for object_ in objects :
756
+ print ('\n {} (confidence: {})' .format (object_ .name , object_ .score ))
757
+ print ('Normalized bounding polygon vertices: ' )
758
+ for vertex in object_ .bounding_poly .normalized_vertices :
759
+ print (' - ({}, {})' .format (vertex .x , vertex .y ))
760
+ # [END vision_localize_objects]
761
+
762
+
763
+ # [START vision_localize_objects_gcs]
764
+ def localize_objects_uri (uri ):
765
+ """Localize objects in the image on Google Cloud Storage
766
+
767
+ Args:
768
+ uri: The path to the file in Google Cloud Storage (gs://...)
769
+ """
770
+ from google .cloud import vision
771
+ client = vision .ImageAnnotatorClient ()
772
+
773
+ image = vision .types .Image ()
774
+ image .source .image_uri = uri
775
+
776
+ objects = client .object_localization (
777
+ image = image ).localized_object_annotations
778
+
779
+ print ('Number of objects found: {}' .format (len (objects )))
780
+ for object_ in objects :
781
+ print ('\n {} (confidence: {})' .format (object_ .name , object_ .score ))
782
+ print ('Normalized bounding polygon vertices: ' )
783
+ for vertex in object_ .bounding_poly .normalized_vertices :
784
+ print (' - ({}, {})' .format (vertex .x , vertex .y ))
785
+ # [END vision_localize_objects_gcs]
786
+
787
+
714
788
def run_local (args ):
715
789
if args .command == 'faces' :
716
790
detect_faces (args .path )
@@ -734,6 +808,8 @@ def run_local(args):
734
808
detect_document (args .path )
735
809
elif args .command == 'web-geo' :
736
810
web_entities_include_geo_results (args .path )
811
+ elif args .command == 'object-localization' :
812
+ localize_objects (args .path )
737
813
738
814
739
815
def run_uri (args ):
@@ -761,6 +837,8 @@ def run_uri(args):
761
837
web_entities_include_geo_results_uri (args .uri )
762
838
elif args .command == 'ocr-uri' :
763
839
async_detect_document (args .uri , args .destination_uri )
840
+ elif args .command == 'object-localization-uri' :
841
+ localize_objects_uri (args .uri )
764
842
765
843
766
844
if __name__ == '__main__' :
@@ -867,6 +945,14 @@ def run_uri(args):
867
945
ocr_uri_parser .add_argument ('uri' )
868
946
ocr_uri_parser .add_argument ('destination_uri' )
869
947
948
+ object_localization_parser = subparsers .add_parser (
949
+ 'object-localization' , help = async_detect_document .__doc__ )
950
+ object_localization_parser .add_argument ('path' )
951
+
952
+ object_localization_uri_parser = subparsers .add_parser (
953
+ 'object-localization-uri' , help = async_detect_document .__doc__ )
954
+ object_localization_uri_parser .add_argument ('uri' )
955
+
870
956
args = parser .parse_args ()
871
957
872
958
if 'uri' in args .command :
0 commit comments