Skip to content

Add vision ocr set endpoint samples #2569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions vision/cloud-client/detect/beta_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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]

Expand Down
4 changes: 3 additions & 1 deletion vision/cloud-client/detect/beta_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
# 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

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
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)


Expand Down
79 changes: 2 additions & 77 deletions vision/cloud-client/detect/detect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import uuid

from google.cloud import storage

Expand All @@ -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)

Expand All @@ -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__),
Expand All @@ -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__),
Expand All @@ -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__),
Expand All @@ -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__),
Expand All @@ -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__),
Expand All @@ -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__),
Expand All @@ -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(
Expand All @@ -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__),
Expand Down Expand Up @@ -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__),
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion vision/cloud-client/detect/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-vision==0.39.0
google-cloud-vision==0.41.0
google-cloud-storage==1.19.1
42 changes: 42 additions & 0 deletions vision/cloud-client/detect/set_endpoint.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions vision/cloud-client/detect/set_endpoint_test.py
Original file line number Diff line number Diff line change
@@ -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