Skip to content

Commit 710b09a

Browse files
authored
Update samples to GA, cleanup tests, delete old samples [(#1704)](GoogleCloudPlatform/python-docs-samples#1704)
1 parent 1876256 commit 710b09a

File tree

6 files changed

+127
-164
lines changed

6 files changed

+127
-164
lines changed

samples/snippets/detect/README.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ To run this sample:
8181
$ python detect.py
8282
8383
usage: detect.py [-h]
84-
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri,ocr-uri}
84+
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri,ocr-uri,object-localization,object-localization-uri}
8585
...
8686
8787
This application demonstrates how to perform basic operations with the
@@ -94,13 +94,15 @@ To run this sample:
9494
python detect.py web-uri http://wheresgus.com/dog.JPG
9595
python detect.py web-geo ./resources/city.jpg
9696
python detect.py faces-uri gs://your-bucket/file.jpg
97-
python detect_pdf.py ocr-uri gs://python-docs-samples-tests/HodgeConj.pdf gs://BUCKET_NAME/PREFIX/
97+
python detect.py ocr-uri gs://python-docs-samples-tests/HodgeConj.pdf gs://BUCKET_NAME/PREFIX/
98+
python detect.py object-localization ./resources/puppies.jpg
99+
python detect.py object-localization-uri gs://...
98100
99101
For more information, the documentation at
100102
https://cloud.google.com/vision/docs.
101103
102104
positional arguments:
103-
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri,ocr-uri}
105+
{faces,faces-uri,labels,labels-uri,landmarks,landmarks-uri,text,text-uri,logos,logos-uri,safe-search,safe-search-uri,properties,properties-uri,web,web-uri,web-geo,web-geo-uri,crophints,crophints-uri,document,document-uri,ocr-uri,object-localization,object-localization-uri}
104106
faces Detects faces in an image.
105107
faces-uri Detects faces in the file located in Google Cloud
106108
Storage or the web.
@@ -126,17 +128,21 @@ To run this sample:
126128
web-uri Detects web annotations in the file located in Google
127129
Cloud Storage.
128130
web-geo Detects web annotations given an image, using the
129-
geotag metadata in the iamge to detect web entities.
131+
geotag metadata in the image to detect web entities.
130132
web-geo-uri Detects web annotations given an image in the file
131133
located in Google Cloud Storage., using the geotag
132-
metadata in the iamge to detect web entities.
134+
metadata in the image to detect web entities.
133135
crophints Detects crop hints in an image.
134136
crophints-uri Detects crop hints in the file located in Google Cloud
135137
Storage.
136138
document Detects document features in an image.
137139
document-uri Detects document features in the file located in
138140
Google Cloud Storage.
139141
ocr-uri OCR with PDF/TIFF as source files on GCS
142+
object-localization
143+
OCR with PDF/TIFF as source files on GCS
144+
object-localization-uri
145+
OCR with PDF/TIFF as source files on GCS
140146
141147
optional arguments:
142148
-h, --help show this help message and exit
@@ -166,8 +172,8 @@ To run this sample:
166172
167173
Example Usage:
168174
python beta_snippets.py -h
169-
python beta_snippets.py object-localizer INPUT_IMAGE
170-
python beta_snippets.py object-localizer-uri gs://...
175+
python beta_snippets.py object-localization INPUT_IMAGE
176+
python beta_snippets.py object-localization-uri gs://...
171177
python beta_snippets.py handwritten-ocr INPUT_IMAGE
172178
python beta_snippets.py handwritten-ocr-uri gs://...
173179

samples/snippets/detect/detect.py

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
python detect.py web-uri http://wheresgus.com/dog.JPG
2525
python detect.py web-geo ./resources/city.jpg
2626
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 \
2828
gs://BUCKET_NAME/PREFIX/
29+
python detect.py object-localization ./resources/puppies.jpg
30+
python detect.py object-localization-uri gs://...
2931
3032
For more information, the documentation at
3133
https://cloud.google.com/vision/docs.
@@ -35,14 +37,11 @@
3537
import io
3638
import re
3739

38-
from google.cloud import storage
39-
from google.cloud import vision
40-
from google.protobuf import json_format
41-
4240

4341
# [START vision_face_detection]
4442
def detect_faces(path):
4543
"""Detects faces in an image."""
44+
from google.cloud import vision
4645
client = vision.ImageAnnotatorClient()
4746

4847
# [START vision_python_migration_face_detection]
@@ -77,6 +76,7 @@ def detect_faces(path):
7776
# [START vision_face_detection_gcs]
7877
def detect_faces_uri(uri):
7978
"""Detects faces in the file located in Google Cloud Storage or the web."""
79+
from google.cloud import vision
8080
client = vision.ImageAnnotatorClient()
8181
# [START vision_python_migration_image_uri]
8282
image = vision.types.Image()
@@ -106,6 +106,7 @@ def detect_faces_uri(uri):
106106
# [START vision_label_detection]
107107
def detect_labels(path):
108108
"""Detects labels in the file."""
109+
from google.cloud import vision
109110
client = vision.ImageAnnotatorClient()
110111

111112
# [START vision_python_migration_label_detection]
@@ -128,6 +129,7 @@ def detect_labels(path):
128129
def detect_labels_uri(uri):
129130
"""Detects labels in the file located in Google Cloud Storage or on the
130131
Web."""
132+
from google.cloud import vision
131133
client = vision.ImageAnnotatorClient()
132134
image = vision.types.Image()
133135
image.source.image_uri = uri
@@ -144,6 +146,7 @@ def detect_labels_uri(uri):
144146
# [START vision_landmark_detection]
145147
def detect_landmarks(path):
146148
"""Detects landmarks in the file."""
149+
from google.cloud import vision
147150
client = vision.ImageAnnotatorClient()
148151

149152
# [START vision_python_migration_landmark_detection]
@@ -170,6 +173,7 @@ def detect_landmarks(path):
170173
def detect_landmarks_uri(uri):
171174
"""Detects landmarks in the file located in Google Cloud Storage or on the
172175
Web."""
176+
from google.cloud import vision
173177
client = vision.ImageAnnotatorClient()
174178
image = vision.types.Image()
175179
image.source.image_uri = uri
@@ -186,6 +190,7 @@ def detect_landmarks_uri(uri):
186190
# [START vision_logo_detection]
187191
def detect_logos(path):
188192
"""Detects logos in the file."""
193+
from google.cloud import vision
189194
client = vision.ImageAnnotatorClient()
190195

191196
# [START vision_python_migration_logo_detection]
@@ -208,6 +213,7 @@ def detect_logos(path):
208213
def detect_logos_uri(uri):
209214
"""Detects logos in the file located in Google Cloud Storage or on the Web.
210215
"""
216+
from google.cloud import vision
211217
client = vision.ImageAnnotatorClient()
212218
image = vision.types.Image()
213219
image.source.image_uri = uri
@@ -224,6 +230,7 @@ def detect_logos_uri(uri):
224230
# [START vision_safe_search_detection]
225231
def detect_safe_search(path):
226232
"""Detects unsafe features in the file."""
233+
from google.cloud import vision
227234
client = vision.ImageAnnotatorClient()
228235

229236
# [START vision_python_migration_safe_search_detection]
@@ -253,6 +260,7 @@ def detect_safe_search(path):
253260
def detect_safe_search_uri(uri):
254261
"""Detects unsafe features in the file located in Google Cloud Storage or
255262
on the Web."""
263+
from google.cloud import vision
256264
client = vision.ImageAnnotatorClient()
257265
image = vision.types.Image()
258266
image.source.image_uri = uri
@@ -276,6 +284,7 @@ def detect_safe_search_uri(uri):
276284
# [START vision_text_detection]
277285
def detect_text(path):
278286
"""Detects text in the file."""
287+
from google.cloud import vision
279288
client = vision.ImageAnnotatorClient()
280289

281290
# [START vision_python_migration_text_detection]
@@ -303,6 +312,7 @@ def detect_text(path):
303312
def detect_text_uri(uri):
304313
"""Detects text in the file located in Google Cloud Storage or on the Web.
305314
"""
315+
from google.cloud import vision
306316
client = vision.ImageAnnotatorClient()
307317
image = vision.types.Image()
308318
image.source.image_uri = uri
@@ -324,6 +334,7 @@ def detect_text_uri(uri):
324334
# [START vision_image_property_detection]
325335
def detect_properties(path):
326336
"""Detects image properties in the file."""
337+
from google.cloud import vision
327338
client = vision.ImageAnnotatorClient()
328339

329340
# [START vision_python_migration_image_properties]
@@ -350,6 +361,7 @@ def detect_properties(path):
350361
def detect_properties_uri(uri):
351362
"""Detects image properties in the file located in Google Cloud Storage or
352363
on the Web."""
364+
from google.cloud import vision
353365
client = vision.ImageAnnotatorClient()
354366
image = vision.types.Image()
355367
image.source.image_uri = uri
@@ -370,6 +382,7 @@ def detect_properties_uri(uri):
370382
# [START vision_web_detection]
371383
def detect_web(path):
372384
"""Detects web annotations given an image."""
385+
from google.cloud import vision
373386
client = vision.ImageAnnotatorClient()
374387

375388
# [START vision_python_migration_web_detection]
@@ -427,6 +440,7 @@ def detect_web(path):
427440
# [START vision_web_detection_gcs]
428441
def detect_web_uri(uri):
429442
"""Detects web annotations in the file located in Google Cloud Storage."""
443+
from google.cloud import vision
430444
client = vision.ImageAnnotatorClient()
431445
image = vision.types.Image()
432446
image.source.image_uri = uri
@@ -480,6 +494,7 @@ def detect_web_uri(uri):
480494
def web_entities_include_geo_results(path):
481495
"""Detects web annotations given an image, using the geotag metadata
482496
in the image to detect web entities."""
497+
from google.cloud import vision
483498
client = vision.ImageAnnotatorClient()
484499

485500
with io.open(path, 'rb') as image_file:
@@ -505,6 +520,7 @@ def web_entities_include_geo_results_uri(uri):
505520
"""Detects web annotations given an image in the file located in
506521
Google Cloud Storage., using the geotag metadata in the image to
507522
detect web entities."""
523+
from google.cloud import vision
508524
client = vision.ImageAnnotatorClient()
509525

510526
image = vision.types.Image()
@@ -526,6 +542,7 @@ def web_entities_include_geo_results_uri(uri):
526542
# [START vision_crop_hint_detection]
527543
def detect_crop_hints(path):
528544
"""Detects crop hints in an image."""
545+
from google.cloud import vision
529546
client = vision.ImageAnnotatorClient()
530547

531548
# [START vision_python_migration_crop_hints]
@@ -554,6 +571,7 @@ def detect_crop_hints(path):
554571
# [START vision_crop_hint_detection_gcs]
555572
def detect_crop_hints_uri(uri):
556573
"""Detects crop hints in the file located in Google Cloud Storage."""
574+
from google.cloud import vision
557575
client = vision.ImageAnnotatorClient()
558576
image = vision.types.Image()
559577
image.source.image_uri = uri
@@ -578,6 +596,7 @@ def detect_crop_hints_uri(uri):
578596
# [START vision_fulltext_detection]
579597
def detect_document(path):
580598
"""Detects document features in an image."""
599+
from google.cloud import vision
581600
client = vision.ImageAnnotatorClient()
582601

583602
# [START vision_python_migration_document_text_detection]
@@ -614,6 +633,7 @@ def detect_document(path):
614633
def detect_document_uri(uri):
615634
"""Detects document features in the file located in Google Cloud
616635
Storage."""
636+
from google.cloud import vision
617637
client = vision.ImageAnnotatorClient()
618638
image = vision.types.Image()
619639
image.source.image_uri = uri
@@ -644,6 +664,9 @@ def detect_document_uri(uri):
644664
# [START vision_text_detection_pdf_gcs]
645665
def async_detect_document(gcs_source_uri, gcs_destination_uri):
646666
"""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
647670
# Supported mime_types are: 'application/pdf' and 'image/tiff'
648671
mime_type = 'application/pdf'
649672

@@ -711,6 +734,57 @@ def async_detect_document(gcs_source_uri, gcs_destination_uri):
711734
# [END vision_text_detection_pdf_gcs]
712735

713736

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+
714788
def run_local(args):
715789
if args.command == 'faces':
716790
detect_faces(args.path)
@@ -734,6 +808,8 @@ def run_local(args):
734808
detect_document(args.path)
735809
elif args.command == 'web-geo':
736810
web_entities_include_geo_results(args.path)
811+
elif args.command == 'object-localization':
812+
localize_objects(args.path)
737813

738814

739815
def run_uri(args):
@@ -761,6 +837,8 @@ def run_uri(args):
761837
web_entities_include_geo_results_uri(args.uri)
762838
elif args.command == 'ocr-uri':
763839
async_detect_document(args.uri, args.destination_uri)
840+
elif args.command == 'object-localization-uri':
841+
localize_objects_uri(args.uri)
764842

765843

766844
if __name__ == '__main__':
@@ -867,6 +945,14 @@ def run_uri(args):
867945
ocr_uri_parser.add_argument('uri')
868946
ocr_uri_parser.add_argument('destination_uri')
869947

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+
870956
args = parser.parse_args()
871957

872958
if 'uri' in args.command:

0 commit comments

Comments
 (0)