diff --git a/vision/cloud-client/web/README.rst b/vision/cloud-client/web/README.rst new file mode 100644 index 000000000000..a841972175cd --- /dev/null +++ b/vision/cloud-client/web/README.rst @@ -0,0 +1,118 @@ +.. This file is automatically generated. Do not edit this file directly. + +Google Cloud Vision API Python Samples +=============================================================================== + +This directory contains samples for Google Cloud Vision API. `Google Cloud Vision API`_ allows developers to easily integrate vision detection features within applications, including image labeling, face and landmark detection, optical character recognition (OCR), and tagging of explicit content + + + + +.. _Google Cloud Vision API: https://cloud.google.com/vision/docs + +Setup +------------------------------------------------------------------------------- + + +Authentication +++++++++++++++ + +Authentication is typically done through `Application Default Credentials`_, +which means you do not have to change the code to authenticate as long as +your environment has credentials. You have a few options for setting up +authentication: + +#. When running locally, use the `Google Cloud SDK`_ + + .. code-block:: bash + + gcloud beta auth application-default login + + +#. When running on App Engine or Compute Engine, credentials are already + set-up. However, you may need to configure your Compute Engine instance + with `additional scopes`_. + +#. You can create a `Service Account key file`_. This file can be used to + authenticate to Google Cloud Platform services from any environment. To use + the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to + the path to the key file, for example: + + .. code-block:: bash + + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json + +.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow +.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using +.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount + +Install Dependencies +++++++++++++++++++++ + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +Web ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python web_detect.py + + usage: web_detect.py [-h] image_url + + Demonstrates web detection using the Google Cloud Vision API. + + Example usage: + python web_detect.py https://goo.gl/X4qcB6 + python web_detect.py ../detect/resources/landmark.jpg + python web_detect.py gs://your-bucket/image.png + + positional arguments: + image_url The image to detect, can be web URI, Google Cloud Storage, or + path to local file. + + optional arguments: + -h, --help show this help message and exit + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/vision/cloud-client/web/README.rst.in b/vision/cloud-client/web/README.rst.in new file mode 100644 index 000000000000..abcdec01dc90 --- /dev/null +++ b/vision/cloud-client/web/README.rst.in @@ -0,0 +1,22 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Vision API + short_name: Cloud Vision API + url: https://cloud.google.com/vision/docs + description: > + `Google Cloud Vision API`_ allows developers to easily integrate vision + detection features within applications, including image labeling, face and + landmark detection, optical character recognition (OCR), and tagging of + explicit content + +setup: +- auth +- install_deps + +samples: +- name: Web + file: web_detect.py + show_help: True + +cloud_client_library: true diff --git a/vision/cloud-client/web/requirements.txt b/vision/cloud-client/web/requirements.txt new file mode 100644 index 000000000000..468e8753cffe --- /dev/null +++ b/vision/cloud-client/web/requirements.txt @@ -0,0 +1 @@ +google-cloud-vision==0.23.3 diff --git a/vision/cloud-client/web/web_detect.py b/vision/cloud-client/web/web_detect.py new file mode 100644 index 000000000000..622aae61a9ed --- /dev/null +++ b/vision/cloud-client/web/web_detect.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python + +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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. + +"""Demonstrates web detection using the Google Cloud Vision API. + +Example usage: + python web_detect.py https://goo.gl/X4qcB6 + python web_detect.py ../detect/resources/landmark.jpg + python web_detect.py gs://your-bucket/image.png +""" +# [START full_tutorial] +# [START imports] +import argparse +import io + +from google.cloud import vision +# [END imports] + + +def annotate(path): + """Returns web annotations given the path to an image.""" + # [START get_annotations] + image = None + vision_client = vision.Client() + + if path.startswith('http') or path.startswith('gs:'): + image = vision_client.image(source_uri=path) + + else: + with io.open(path, 'rb') as image_file: + content = image_file.read() + + image = vision_client.image(content=content) + + return image.detect_web() + # [END get_annotations] + + +def report(annotations): + """Prints detected features in the provided web annotations.""" + # [START print_annotations] + if annotations.pages_with_matching_images: + print('\n{} Pages with matching images retrieved') + + for page in annotations.pages_with_matching_images: + print('Score : {}'.format(page.score)) + print('Url : {}'.format(page.url)) + + if annotations.full_matching_images: + print ('\n{} Full Matches found: '.format( + len(annotations.full_matching_images))) + + for image in annotations.full_matching_images: + print('Score: {}'.format(image.score)) + print('Url : {}'.format(image.url)) + + if annotations.partial_matching_images: + print ('\n{} Partial Matches found: '.format( + len(annotations.partial_matching_images))) + + for image in annotations.partial_matching_images: + print('Score: {}'.format(image.score)) + print('Url : {}'.format(image.url)) + + if annotations.web_entities: + print ('\n{} Web entities found: '.format( + len(annotations.web_entities))) + + for entity in annotations.web_entities: + print('Score : {}'.format(entity.score)) + print('Description: {}'.format(entity.description)) + # [END print_annotations] + + +if __name__ == '__main__': + # [START run_web] + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + path_help = str('The image to detect, can be web URI, ' + 'Google Cloud Storage, or path to local file.') + parser.add_argument('image_url', help=path_help) + args = parser.parse_args() + + report(annotate(args.image_url)) + # [END run_web] +# [END full_tutorial] diff --git a/vision/cloud-client/web/web_detect_test.py b/vision/cloud-client/web/web_detect_test.py new file mode 100644 index 000000000000..d5cd08b68fe4 --- /dev/null +++ b/vision/cloud-client/web/web_detect_test.py @@ -0,0 +1,36 @@ +# Copyright 2017 Google Inc. All Rights Reserved. +# +# 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 web_detect + + +def test_detect_file(cloud_config, capsys): + file_name = ('../detect/resources/landmark.jpg') + web_detect.report(web_detect.annotate(file_name)) + out, _ = capsys.readouterr() + assert 'Description: Palace of Fine Arts Theatre' in out + + +def test_detect_web_gsuri(cloud_config, capsys): + file_name = ('gs://{}/vision/landmark.jpg'.format( + cloud_config.storage_bucket)) + web_detect.report(web_detect.annotate(file_name)) + out, _ = capsys.readouterr() + assert 'Description: Palace of Fine Arts Theatre' in out + + +def test_detect_web_http(cloud_config, capsys): + web_detect.report(web_detect.annotate('https://goo.gl/X4qcB6')) + out, _ = capsys.readouterr() + assert 'https://cloud.google.com/vision/' in out