Skip to content

Commit 0cf596f

Browse files
nnegreyleahecole
andcommitted
Add vision ocr set endpoint samples (GoogleCloudPlatform#2569)
* Add vision ocr set endpoint samples * Remove port number as it is optional in Python * Use unique output names * lint * Add support for python2 print statements * use uuid instead of datetime * remove all tests that use https as they perform duplicate work Co-authored-by: Leah E. Cole <[email protected]>
1 parent a8d3a1b commit 0cf596f

File tree

6 files changed

+79
-87
lines changed

6 files changed

+79
-87
lines changed

vision/cloud-client/detect/beta_snippets.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,16 @@ def detect_batch_annotate_files(path):
214214
for image_response in response.responses[0].responses:
215215
for page in image_response.full_text_annotation.pages:
216216
for block in page.blocks:
217-
print('\nBlock confidence: {}\n'.format(block.confidence))
217+
print(u'\nBlock confidence: {}\n'.format(block.confidence))
218218
for par in block.paragraphs:
219-
print('\tParagraph confidence: {}'.format(par.confidence))
219+
print(u'\tParagraph confidence: {}'.format(par.confidence))
220220
for word in par.words:
221221
symbol_texts = [symbol.text for symbol in word.symbols]
222222
word_text = ''.join(symbol_texts)
223-
print('\t\tWord text: {} (confidence: {})'.format(
223+
print(u'\t\tWord text: {} (confidence: {})'.format(
224224
word_text, word.confidence))
225225
for symbol in word.symbols:
226-
print('\t\t\tSymbol: {} (confidence: {})'.format(
226+
print(u'\t\t\tSymbol: {} (confidence: {})'.format(
227227
symbol.text, symbol.confidence))
228228
# [END vision_batch_annotate_files_beta]
229229

@@ -262,16 +262,16 @@ def detect_batch_annotate_files_uri(gcs_uri):
262262
for image_response in response.responses[0].responses:
263263
for page in image_response.full_text_annotation.pages:
264264
for block in page.blocks:
265-
print('\nBlock confidence: {}\n'.format(block.confidence))
265+
print(u'\nBlock confidence: {}\n'.format(block.confidence))
266266
for par in block.paragraphs:
267-
print('\tParagraph confidence: {}'.format(par.confidence))
267+
print(u'\tParagraph confidence: {}'.format(par.confidence))
268268
for word in par.words:
269269
symbol_texts = [symbol.text for symbol in word.symbols]
270270
word_text = ''.join(symbol_texts)
271-
print('\t\tWord text: {} (confidence: {})'.format(
271+
print(u'\t\tWord text: {} (confidence: {})'.format(
272272
word_text, word.confidence))
273273
for symbol in word.symbols:
274-
print('\t\t\tSymbol: {} (confidence: {})'.format(
274+
print(u'\t\t\tSymbol: {} (confidence: {})'.format(
275275
symbol.text, symbol.confidence))
276276
# [END vision_batch_annotate_files_gcs_beta]
277277

vision/cloud-client/detect/beta_snippets_test.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
1415
import os
16+
import uuid
1517

1618
import beta_snippets
1719

1820
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
1921
GCS_ROOT = 'gs://cloud-samples-data/vision/'
2022

2123
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
22-
OUTPUT_PREFIX = 'OCR_PDF_TEST_OUTPUT'
24+
OUTPUT_PREFIX = 'TEST_OUTPUT_{}'.format(uuid.uuid4())
2325
GCS_DESTINATION_URI = 'gs://{}/{}/'.format(BUCKET, OUTPUT_PREFIX)
2426

2527

vision/cloud-client/detect/detect_test.py

+2-77
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
1617

1718
from google.cloud import storage
1819

@@ -21,7 +22,7 @@
2122
ASSET_BUCKET = "cloud-samples-data"
2223

2324
BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
24-
OUTPUT_PREFIX = 'OCR_PDF_TEST_OUTPUT'
25+
OUTPUT_PREFIX = 'TEST_OUTPUT_{}'.format(uuid.uuid4())
2526
GCS_SOURCE_URI = 'gs://{}/HodgeConj.pdf'.format(BUCKET)
2627
GCS_DESTINATION_URI = 'gs://{}/{}/'.format(BUCKET, OUTPUT_PREFIX)
2728

@@ -42,14 +43,6 @@ def test_labels_uri(capsys):
4243
assert 'Labels' in out
4344

4445

45-
def test_labels_http(capsys):
46-
uri = 'https://storage-download.googleapis.com/{}' \
47-
'/vision/label/wakeupcat.jpg'
48-
detect.detect_labels_uri(uri.format(ASSET_BUCKET))
49-
out, _ = capsys.readouterr()
50-
assert 'Labels' in out
51-
52-
5346
def test_landmarks(capsys):
5447
file_name = os.path.join(
5548
os.path.dirname(__file__),
@@ -66,13 +59,6 @@ def test_landmarks_uri(capsys):
6659
assert 'palace' in out.lower()
6760

6861

69-
def test_landmarks_http(capsys):
70-
uri = 'https://storage-download.googleapis.com/{}/vision/landmark/pofa.jpg'
71-
detect.detect_landmarks_uri(uri.format(ASSET_BUCKET))
72-
out, _ = capsys.readouterr()
73-
assert 'palace' in out.lower()
74-
75-
7662
def test_faces(capsys):
7763
file_name = os.path.join(
7864
os.path.dirname(__file__),
@@ -89,14 +75,6 @@ def test_faces_uri(capsys):
8975
assert 'POSSIBLE' in out
9076

9177

92-
def test_faces_http(capsys):
93-
uri = ('https://storage-download.googleapis.com/{}/vision/' +
94-
'face/face_no_surprise.jpg')
95-
detect.detect_faces_uri(uri.format(ASSET_BUCKET))
96-
out, _ = capsys.readouterr()
97-
assert 'POSSIBLE' in out
98-
99-
10078
def test_logos(capsys):
10179
file_name = os.path.join(
10280
os.path.dirname(__file__),
@@ -113,14 +91,6 @@ def test_logos_uri(capsys):
11391
assert 'google' in out.lower()
11492

11593

116-
def test_logos_http(capsys):
117-
uri = 'https://storage-download.googleapis.com/{}' \
118-
'/vision/logo/logo_google.png'
119-
detect.detect_logos_uri(uri.format(ASSET_BUCKET))
120-
out, _ = capsys.readouterr()
121-
assert 'google' in out.lower()
122-
123-
12494
def test_safe_search(capsys):
12595
file_name = os.path.join(
12696
os.path.dirname(__file__),
@@ -139,15 +109,6 @@ def test_safe_search_uri(capsys):
139109
assert 'racy: ' in out
140110

141111

142-
def test_safe_search_http(capsys):
143-
uri = 'https://storage-download.googleapis.com/{}' \
144-
'/vision/label/wakeupcat.jpg'
145-
detect.detect_safe_search_uri(uri.format(ASSET_BUCKET))
146-
out, _ = capsys.readouterr()
147-
assert 'VERY_LIKELY' in out
148-
assert 'racy: ' in out
149-
150-
151112
def test_detect_text(capsys):
152113
file_name = os.path.join(
153114
os.path.dirname(__file__),
@@ -164,13 +125,6 @@ def test_detect_text_uri(capsys):
164125
assert '37%' in out
165126

166127

167-
def test_detect_text_http(capsys):
168-
uri = 'https://storage-download.googleapis.com/{}/vision/text/screen.jpg'
169-
detect.detect_text_uri(uri.format(ASSET_BUCKET))
170-
out, _ = capsys.readouterr()
171-
assert '37%' in out
172-
173-
174128
def test_detect_properties(capsys):
175129
file_name = os.path.join(
176130
os.path.dirname(__file__),
@@ -187,13 +141,6 @@ def test_detect_properties_uri(capsys):
187141
assert 'frac' in out
188142

189143

190-
def test_detect_properties_http(capsys):
191-
uri = 'https://storage-download.googleapis.com/{}/vision/landmark/pofa.jpg'
192-
detect.detect_properties_uri(uri.format(ASSET_BUCKET))
193-
out, _ = capsys.readouterr()
194-
assert 'frac' in out
195-
196-
197144
# Vision 1.1 tests
198145
def test_detect_web(capsys):
199146
file_name = os.path.join(
@@ -211,13 +158,6 @@ def test_detect_web_uri(capsys):
211158
assert 'best guess label: palace of fine arts' in out.lower()
212159

213160

214-
def test_detect_web_http(capsys):
215-
uri = 'https://storage-download.googleapis.com/{}/vision/landmark/pofa.jpg'
216-
detect.detect_web_uri(uri.format(ASSET_BUCKET))
217-
out, _ = capsys.readouterr()
218-
assert 'best guess label: palace of fine arts' in out.lower()
219-
220-
221161
def test_detect_web_with_geo(capsys):
222162
file_name = os.path.join(
223163
os.path.dirname(__file__),
@@ -254,13 +194,6 @@ def test_detect_document_uri(capsys):
254194
assert 'class' in out
255195

256196

257-
def test_detect_document_http(capsys):
258-
uri = 'https://storage-download.googleapis.com/{}/vision/text/screen.jpg'
259-
detect.detect_document_uri(uri.format(ASSET_BUCKET))
260-
out, _ = capsys.readouterr()
261-
assert 'class' in out
262-
263-
264197
def test_detect_crop_hints(capsys):
265198
file_name = os.path.join(
266199
os.path.dirname(__file__),
@@ -277,14 +210,6 @@ def test_detect_crop_hints_uri(capsys):
277210
assert 'bounds: ' in out
278211

279212

280-
def test_detect_crop_hints_http(capsys):
281-
uri = 'https://storage-download.googleapis.com/{}' \
282-
'/vision/label/wakeupcat.jpg'
283-
detect.detect_crop_hints_uri(uri.format(ASSET_BUCKET))
284-
out, _ = capsys.readouterr()
285-
assert 'bounds: ' in out
286-
287-
288213
def test_async_detect_document(capsys):
289214
storage_client = storage.Client()
290215
bucket = storage_client.get_bucket(BUCKET)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-vision==0.39.0
1+
google-cloud-vision==0.41.0
22
google-cloud-storage==1.19.1
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def set_endpoint():
17+
"""Change your endpoint"""
18+
# [START vision_set_endpoint]
19+
from google.cloud import vision
20+
21+
client_options = {'api_endpoint': 'eu-vision.googleapis.com'}
22+
23+
client = vision.ImageAnnotatorClient(client_options=client_options)
24+
# [END vision_set_endpoint]
25+
image_source = vision.types.ImageSource(
26+
image_uri='gs://cloud-samples-data/vision/text/screen.jpg')
27+
image = vision.types.Image(source=image_source)
28+
29+
response = client.text_detection(image=image)
30+
31+
print('Texts:')
32+
for text in response.text_annotations:
33+
print('{}'.format(text.description))
34+
35+
vertices = ['({},{})'.format(vertex.x, vertex.y)
36+
for vertex in text.bounding_poly.vertices]
37+
38+
print('bounds: {}\n'.format(','.join(vertices)))
39+
40+
41+
if __name__ == '__main__':
42+
set_endpoint()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import set_endpoint
16+
17+
18+
def test_set_endpoint(capsys):
19+
set_endpoint.set_endpoint()
20+
21+
out, _ = capsys.readouterr()
22+
assert 'System' in out
23+
assert 'bounds:' in out

0 commit comments

Comments
 (0)