From 1dca994ff85db264146ba9f9df7be0d256febb66 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Fri, 10 Jan 2020 13:13:11 -0700 Subject: [PATCH] Add vision ocr set endpoint samples [(#2569)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/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 <6719667+leahecole@users.noreply.github.com> --- samples/snippets/detect/beta_snippets.py | 16 ++-- samples/snippets/detect/beta_snippets_test.py | 4 +- samples/snippets/detect/detect_test.py | 79 +------------------ samples/snippets/detect/requirements.txt | 2 +- samples/snippets/detect/set_endpoint.py | 42 ++++++++++ samples/snippets/detect/set_endpoint_test.py | 23 ++++++ 6 files changed, 79 insertions(+), 87 deletions(-) create mode 100644 samples/snippets/detect/set_endpoint.py create mode 100644 samples/snippets/detect/set_endpoint_test.py diff --git a/samples/snippets/detect/beta_snippets.py b/samples/snippets/detect/beta_snippets.py index 0b98d055..1a5ae27b 100644 --- a/samples/snippets/detect/beta_snippets.py +++ b/samples/snippets/detect/beta_snippets.py @@ -214,16 +214,16 @@ def detect_batch_annotate_files(path): for image_response in response.responses[0].responses: for page in image_response.full_text_annotation.pages: for block in page.blocks: - print('\nBlock confidence: {}\n'.format(block.confidence)) + print(u'\nBlock confidence: {}\n'.format(block.confidence)) for par in block.paragraphs: - print('\tParagraph confidence: {}'.format(par.confidence)) + print(u'\tParagraph confidence: {}'.format(par.confidence)) for word in par.words: symbol_texts = [symbol.text for symbol in word.symbols] word_text = ''.join(symbol_texts) - print('\t\tWord text: {} (confidence: {})'.format( + print(u'\t\tWord text: {} (confidence: {})'.format( word_text, word.confidence)) for symbol in word.symbols: - print('\t\t\tSymbol: {} (confidence: {})'.format( + print(u'\t\t\tSymbol: {} (confidence: {})'.format( symbol.text, symbol.confidence)) # [END vision_batch_annotate_files_beta] @@ -262,16 +262,16 @@ def detect_batch_annotate_files_uri(gcs_uri): for image_response in response.responses[0].responses: for page in image_response.full_text_annotation.pages: for block in page.blocks: - print('\nBlock confidence: {}\n'.format(block.confidence)) + print(u'\nBlock confidence: {}\n'.format(block.confidence)) for par in block.paragraphs: - print('\tParagraph confidence: {}'.format(par.confidence)) + print(u'\tParagraph confidence: {}'.format(par.confidence)) for word in par.words: symbol_texts = [symbol.text for symbol in word.symbols] word_text = ''.join(symbol_texts) - print('\t\tWord text: {} (confidence: {})'.format( + print(u'\t\tWord text: {} (confidence: {})'.format( word_text, word.confidence)) for symbol in word.symbols: - print('\t\t\tSymbol: {} (confidence: {})'.format( + print(u'\t\t\tSymbol: {} (confidence: {})'.format( symbol.text, symbol.confidence)) # [END vision_batch_annotate_files_gcs_beta] diff --git a/samples/snippets/detect/beta_snippets_test.py b/samples/snippets/detect/beta_snippets_test.py index 57f7c375..4a5a4663 100644 --- a/samples/snippets/detect/beta_snippets_test.py +++ b/samples/snippets/detect/beta_snippets_test.py @@ -11,7 +11,9 @@ # 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. + import os +import uuid import beta_snippets @@ -19,7 +21,7 @@ GCS_ROOT = 'gs://cloud-samples-data/vision/' BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] -OUTPUT_PREFIX = 'OCR_PDF_TEST_OUTPUT' +OUTPUT_PREFIX = 'TEST_OUTPUT_{}'.format(uuid.uuid4()) GCS_DESTINATION_URI = 'gs://{}/{}/'.format(BUCKET, OUTPUT_PREFIX) diff --git a/samples/snippets/detect/detect_test.py b/samples/snippets/detect/detect_test.py index 13c61d16..b7649d59 100644 --- a/samples/snippets/detect/detect_test.py +++ b/samples/snippets/detect/detect_test.py @@ -13,6 +13,7 @@ # limitations under the License. import os +import uuid from google.cloud import storage @@ -21,7 +22,7 @@ ASSET_BUCKET = "cloud-samples-data" BUCKET = os.environ['CLOUD_STORAGE_BUCKET'] -OUTPUT_PREFIX = 'OCR_PDF_TEST_OUTPUT' +OUTPUT_PREFIX = 'TEST_OUTPUT_{}'.format(uuid.uuid4()) GCS_SOURCE_URI = 'gs://{}/HodgeConj.pdf'.format(BUCKET) GCS_DESTINATION_URI = 'gs://{}/{}/'.format(BUCKET, OUTPUT_PREFIX) @@ -42,14 +43,6 @@ def test_labels_uri(capsys): assert 'Labels' in out -def test_labels_http(capsys): - uri = 'https://storage-download.googleapis.com/{}' \ - '/vision/label/wakeupcat.jpg' - detect.detect_labels_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'Labels' in out - - def test_landmarks(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -66,13 +59,6 @@ def test_landmarks_uri(capsys): assert 'palace' in out.lower() -def test_landmarks_http(capsys): - uri = 'https://storage-download.googleapis.com/{}/vision/landmark/pofa.jpg' - detect.detect_landmarks_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'palace' in out.lower() - - def test_faces(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -89,14 +75,6 @@ def test_faces_uri(capsys): assert 'POSSIBLE' in out -def test_faces_http(capsys): - uri = ('https://storage-download.googleapis.com/{}/vision/' + - 'face/face_no_surprise.jpg') - detect.detect_faces_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'POSSIBLE' in out - - def test_logos(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -113,14 +91,6 @@ def test_logos_uri(capsys): assert 'google' in out.lower() -def test_logos_http(capsys): - uri = 'https://storage-download.googleapis.com/{}' \ - '/vision/logo/logo_google.png' - detect.detect_logos_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'google' in out.lower() - - def test_safe_search(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -139,15 +109,6 @@ def test_safe_search_uri(capsys): assert 'racy: ' in out -def test_safe_search_http(capsys): - uri = 'https://storage-download.googleapis.com/{}' \ - '/vision/label/wakeupcat.jpg' - detect.detect_safe_search_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'VERY_LIKELY' in out - assert 'racy: ' in out - - def test_detect_text(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -164,13 +125,6 @@ def test_detect_text_uri(capsys): assert '37%' in out -def test_detect_text_http(capsys): - uri = 'https://storage-download.googleapis.com/{}/vision/text/screen.jpg' - detect.detect_text_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert '37%' in out - - def test_detect_properties(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -187,13 +141,6 @@ def test_detect_properties_uri(capsys): assert 'frac' in out -def test_detect_properties_http(capsys): - uri = 'https://storage-download.googleapis.com/{}/vision/landmark/pofa.jpg' - detect.detect_properties_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'frac' in out - - # Vision 1.1 tests def test_detect_web(capsys): file_name = os.path.join( @@ -211,13 +158,6 @@ def test_detect_web_uri(capsys): assert 'best guess label: palace of fine arts' in out.lower() -def test_detect_web_http(capsys): - uri = 'https://storage-download.googleapis.com/{}/vision/landmark/pofa.jpg' - detect.detect_web_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'best guess label: palace of fine arts' in out.lower() - - def test_detect_web_with_geo(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -254,13 +194,6 @@ def test_detect_document_uri(capsys): assert 'class' in out -def test_detect_document_http(capsys): - uri = 'https://storage-download.googleapis.com/{}/vision/text/screen.jpg' - detect.detect_document_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'class' in out - - def test_detect_crop_hints(capsys): file_name = os.path.join( os.path.dirname(__file__), @@ -277,14 +210,6 @@ def test_detect_crop_hints_uri(capsys): assert 'bounds: ' in out -def test_detect_crop_hints_http(capsys): - uri = 'https://storage-download.googleapis.com/{}' \ - '/vision/label/wakeupcat.jpg' - detect.detect_crop_hints_uri(uri.format(ASSET_BUCKET)) - out, _ = capsys.readouterr() - assert 'bounds: ' in out - - def test_async_detect_document(capsys): storage_client = storage.Client() bucket = storage_client.get_bucket(BUCKET) diff --git a/samples/snippets/detect/requirements.txt b/samples/snippets/detect/requirements.txt index c67a5c02..9b98aca7 100644 --- a/samples/snippets/detect/requirements.txt +++ b/samples/snippets/detect/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-vision==0.39.0 +google-cloud-vision==0.41.0 google-cloud-storage==1.19.1 diff --git a/samples/snippets/detect/set_endpoint.py b/samples/snippets/detect/set_endpoint.py new file mode 100644 index 00000000..d721ffc6 --- /dev/null +++ b/samples/snippets/detect/set_endpoint.py @@ -0,0 +1,42 @@ +# Copyright 2019 Google LLC +# +# 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. + + +def set_endpoint(): + """Change your endpoint""" + # [START vision_set_endpoint] + from google.cloud import vision + + client_options = {'api_endpoint': 'eu-vision.googleapis.com'} + + client = vision.ImageAnnotatorClient(client_options=client_options) + # [END vision_set_endpoint] + image_source = vision.types.ImageSource( + image_uri='gs://cloud-samples-data/vision/text/screen.jpg') + image = vision.types.Image(source=image_source) + + response = client.text_detection(image=image) + + print('Texts:') + for text in response.text_annotations: + print('{}'.format(text.description)) + + vertices = ['({},{})'.format(vertex.x, vertex.y) + for vertex in text.bounding_poly.vertices] + + print('bounds: {}\n'.format(','.join(vertices))) + + +if __name__ == '__main__': + set_endpoint() diff --git a/samples/snippets/detect/set_endpoint_test.py b/samples/snippets/detect/set_endpoint_test.py new file mode 100644 index 00000000..37bd590c --- /dev/null +++ b/samples/snippets/detect/set_endpoint_test.py @@ -0,0 +1,23 @@ +# Copyright 2019 Google LLC +# +# 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. + +import set_endpoint + + +def test_set_endpoint(capsys): + set_endpoint.set_endpoint() + + out, _ = capsys.readouterr() + assert 'System' in out + assert 'bounds:' in out