Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 97d99fd

Browse filesBrowse files
gguussJon Wayne Parrott
authored andcommitted
Adds web detection tutorial (GoogleCloudPlatform#874)
1 parent 20d27d2 commit 97d99fd
Copy full SHA for 97d99fd

File tree

Expand file treeCollapse file tree

5 files changed

+277
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+277
-0
lines changed

‎vision/cloud-client/web/README.rst

Copy file name to clipboard
+118Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Vision API Python Samples
4+
===============================================================================
5+
6+
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
7+
8+
9+
10+
11+
.. _Google Cloud Vision API: https://cloud.google.com/vision/docs
12+
13+
Setup
14+
-------------------------------------------------------------------------------
15+
16+
17+
Authentication
18+
++++++++++++++
19+
20+
Authentication is typically done through `Application Default Credentials`_,
21+
which means you do not have to change the code to authenticate as long as
22+
your environment has credentials. You have a few options for setting up
23+
authentication:
24+
25+
#. When running locally, use the `Google Cloud SDK`_
26+
27+
.. code-block:: bash
28+
29+
gcloud beta auth application-default login
30+
31+
32+
#. When running on App Engine or Compute Engine, credentials are already
33+
set-up. However, you may need to configure your Compute Engine instance
34+
with `additional scopes`_.
35+
36+
#. You can create a `Service Account key file`_. This file can be used to
37+
authenticate to Google Cloud Platform services from any environment. To use
38+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
39+
the path to the key file, for example:
40+
41+
.. code-block:: bash
42+
43+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
44+
45+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
46+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
47+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
48+
49+
Install Dependencies
50+
++++++++++++++++++++
51+
52+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
53+
54+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
55+
56+
.. code-block:: bash
57+
58+
$ virtualenv env
59+
$ source env/bin/activate
60+
61+
#. Install the dependencies needed to run the samples.
62+
63+
.. code-block:: bash
64+
65+
$ pip install -r requirements.txt
66+
67+
.. _pip: https://pip.pypa.io/
68+
.. _virtualenv: https://virtualenv.pypa.io/
69+
70+
Samples
71+
-------------------------------------------------------------------------------
72+
73+
Web
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python web_detect.py
83+
84+
usage: web_detect.py [-h] image_url
85+
86+
Demonstrates web detection using the Google Cloud Vision API.
87+
88+
Example usage:
89+
python web_detect.py https://goo.gl/X4qcB6
90+
python web_detect.py ../detect/resources/landmark.jpg
91+
python web_detect.py gs://your-bucket/image.png
92+
93+
positional arguments:
94+
image_url The image to detect, can be web URI, Google Cloud Storage, or
95+
path to local file.
96+
97+
optional arguments:
98+
-h, --help show this help message and exit
99+
100+
101+
102+
103+
The client library
104+
-------------------------------------------------------------------------------
105+
106+
This sample uses the `Google Cloud Client Library for Python`_.
107+
You can read the documentation for more details on API usage and use GitHub
108+
to `browse the source`_ and `report issues`_.
109+
110+
.. Google Cloud Client Library for Python:
111+
https://googlecloudplatform.github.io/google-cloud-python/
112+
.. browse the source:
113+
https://github.com/GoogleCloudPlatform/google-cloud-python
114+
.. report issues:
115+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
116+
117+
118+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

‎vision/cloud-client/web/README.rst.in

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Vision API
5+
short_name: Cloud Vision API
6+
url: https://cloud.google.com/vision/docs
7+
description: >
8+
`Google Cloud Vision API`_ allows developers to easily integrate vision
9+
detection features within applications, including image labeling, face and
10+
landmark detection, optical character recognition (OCR), and tagging of
11+
explicit content
12+
13+
setup:
14+
- auth
15+
- install_deps
16+
17+
samples:
18+
- name: Web
19+
file: web_detect.py
20+
show_help: True
21+
22+
cloud_client_library: true
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-vision==0.23.3

‎vision/cloud-client/web/web_detect.py

Copy file name to clipboard
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Demonstrates web detection using the Google Cloud Vision API.
18+
19+
Example usage:
20+
python web_detect.py https://goo.gl/X4qcB6
21+
python web_detect.py ../detect/resources/landmark.jpg
22+
python web_detect.py gs://your-bucket/image.png
23+
"""
24+
# [START full_tutorial]
25+
# [START imports]
26+
import argparse
27+
import io
28+
29+
from google.cloud import vision
30+
# [END imports]
31+
32+
33+
def annotate(path):
34+
"""Returns web annotations given the path to an image."""
35+
# [START get_annotations]
36+
image = None
37+
vision_client = vision.Client()
38+
39+
if path.startswith('http') or path.startswith('gs:'):
40+
image = vision_client.image(source_uri=path)
41+
42+
else:
43+
with io.open(path, 'rb') as image_file:
44+
content = image_file.read()
45+
46+
image = vision_client.image(content=content)
47+
48+
return image.detect_web()
49+
# [END get_annotations]
50+
51+
52+
def report(annotations):
53+
"""Prints detected features in the provided web annotations."""
54+
# [START print_annotations]
55+
if annotations.pages_with_matching_images:
56+
print('\n{} Pages with matching images retrieved')
57+
58+
for page in annotations.pages_with_matching_images:
59+
print('Score : {}'.format(page.score))
60+
print('Url : {}'.format(page.url))
61+
62+
if annotations.full_matching_images:
63+
print ('\n{} Full Matches found: '.format(
64+
len(annotations.full_matching_images)))
65+
66+
for image in annotations.full_matching_images:
67+
print('Score: {}'.format(image.score))
68+
print('Url : {}'.format(image.url))
69+
70+
if annotations.partial_matching_images:
71+
print ('\n{} Partial Matches found: '.format(
72+
len(annotations.partial_matching_images)))
73+
74+
for image in annotations.partial_matching_images:
75+
print('Score: {}'.format(image.score))
76+
print('Url : {}'.format(image.url))
77+
78+
if annotations.web_entities:
79+
print ('\n{} Web entities found: '.format(
80+
len(annotations.web_entities)))
81+
82+
for entity in annotations.web_entities:
83+
print('Score : {}'.format(entity.score))
84+
print('Description: {}'.format(entity.description))
85+
# [END print_annotations]
86+
87+
88+
if __name__ == '__main__':
89+
# [START run_web]
90+
parser = argparse.ArgumentParser(
91+
description=__doc__,
92+
formatter_class=argparse.RawDescriptionHelpFormatter)
93+
path_help = str('The image to detect, can be web URI, '
94+
'Google Cloud Storage, or path to local file.')
95+
parser.add_argument('image_url', help=path_help)
96+
args = parser.parse_args()
97+
98+
report(annotate(args.image_url))
99+
# [END run_web]
100+
# [END full_tutorial]
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
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 web_detect
16+
17+
18+
def test_detect_file(cloud_config, capsys):
19+
file_name = ('../detect/resources/landmark.jpg')
20+
web_detect.report(web_detect.annotate(file_name))
21+
out, _ = capsys.readouterr()
22+
assert 'Description: Palace of Fine Arts Theatre' in out
23+
24+
25+
def test_detect_web_gsuri(cloud_config, capsys):
26+
file_name = ('gs://{}/vision/landmark.jpg'.format(
27+
cloud_config.storage_bucket))
28+
web_detect.report(web_detect.annotate(file_name))
29+
out, _ = capsys.readouterr()
30+
assert 'Description: Palace of Fine Arts Theatre' in out
31+
32+
33+
def test_detect_web_http(cloud_config, capsys):
34+
web_detect.report(web_detect.annotate('https://goo.gl/X4qcB6'))
35+
out, _ = capsys.readouterr()
36+
assert 'https://cloud.google.com/vision/' in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.