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 1 commit
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
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:443'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing the endpoint URL as a function argument?

def set_endpoint(endpoint):
  ...
  # endpoint = 'eu-vision.googleapis.com:443'

  client_options = {'api_endpoint': endpoint}

That way it's more obvious what users needs to change, and is also more consistent with other samples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only supported endpoint

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there plans to support any others? What's the difference from not supplying this? I think I still prefer to have it as a variable, or do you think it makes more sense to hardcode it?


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