Skip to content

Commit 9224cd9

Browse files
authored
Merge pull request googleapis#2754 from daspecster/vision-docs-cleanup
Cleaning up docs and fixing typos
2 parents 60fed0a + 678d502 commit 9224cd9

File tree

1 file changed

+89
-99
lines changed

1 file changed

+89
-99
lines changed

docs/vision-usage.rst

Lines changed: 89 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -8,98 +8,37 @@ Authentication and Configuration
88
see :doc:`google-cloud-auth`.
99

1010
- In addition to any authentication configuration, you should also set the
11-
:envvar:`GOOGLE_CLOUD_PROJECT` environment variable for the project you'd like
12-
to interact with. If the GOOGLE_CLOUD_PROJECT environment variable is not present,
13-
the project ID from JSON file credentials is used.
11+
:envvar:`GOOGLE_CLOUD_PROJECT` environment variable for the project you'd
12+
like to interact with. If the GOOGLE_CLOUD_PROJECT environment variable is
13+
not present, the project ID from JSON file credentials is used.
1414

1515
If you are using Google App Engine or Google Compute Engine
1616
this will be detected automatically.
1717

1818
- After configuring your environment, create a
19-
:class:`Client <google.cloud.vision.client.Client>`
19+
:class:`~google.cloud.vision.client.Client`.
2020

2121
.. code-block:: python
2222
2323
>>> from google.cloud import vision
2424
>>> client = vision.Client()
2525
26-
or pass in ``credentials`` and ``project`` explicitly
26+
or pass in ``credentials`` and ``project`` explicitly.
2727

2828
.. code-block:: python
2929
3030
>>> from google.cloud import vision
3131
>>> client = vision.Client(project='my-project', credentials=creds)
3232
33-
Annotating an Image
34-
-------------------
35-
36-
Annotate a single image
37-
~~~~~~~~~~~~~~~~~~~~~~~
38-
39-
.. code-block:: python
40-
41-
>>> import io
42-
>>> from google.cloud import vision
43-
>>> client = vision.Client()
44-
>>> with io.open('./image.png', 'rb') as image_file:
45-
... image = client.image(content=image_file.read())
46-
>>> faces = image.detect_faces(limit=10)
47-
>>> faces[0].landmarks.left_eye.position.x_coordinate
48-
... 1004.8003
49-
50-
Annotate multiple images
51-
~~~~~~~~~~~~~~~~~~~~~~~~
52-
53-
.. code-block:: python
54-
55-
>>> import io
56-
>>> from google.cloud import vision
57-
>>> client = vision.Client()
58-
>>> with io.open('./image.png', 'rb') as image_file:
59-
... image_one = client.image(content=image_file.read())
60-
>>> image_two = client.image(source_uri='gs://my-storage-bucket/image.jpg')
61-
>>> with client.batch():
62-
... labels = image_one.detect_labels()
63-
... faces = image_two.detect_faces(limit=10)
64-
65-
No results returned
66-
~~~~~~~~~~~~~~~~~~~
67-
68-
Failing annotations return no results for the feature type requested.
69-
70-
.. code-block:: python
71-
72-
>>> from google.cloud import vision
73-
>>> client = vision.Client()
74-
>>> image = client.image(source_uri='gs://my-storage-bucket/image.jpg')
75-
>>> logos = image.detect_logos(limit=10)
76-
>>> logos
77-
[]
78-
79-
80-
Manual Detection
81-
~~~~~~~~~~~~~~~~
82-
83-
You can call the detection method manually.
84-
85-
.. code-block:: python
86-
87-
>>> from google.cloud import vision
88-
>>> from google.cloud.vision.image import Feature
89-
>>> from google.cloud.vision.image import FeatureTypes
90-
>>> client = vision.Client()
91-
>>> image = client.image(source_uri='gs://my-test-bucket/image.jpg')
92-
>>> features = [Feature(FeatureTypes.FACE_DETECTION, 5),
93-
... Feature(FeatureTypes.LOGO_DETECTION, 3)]
94-
>>> annotations = image.detect(features)
9533
9634
Face Detection
9735
~~~~~~~~~~~~~~
9836

99-
Detecting a face or faces in an image.
100-
For a list of the possible facial landmarks
101-
see: https://cloud.google.com/vision/reference/rest/v1/images/annotate#type_1
37+
:meth:`~google.cloud.vision.image.Image.detect_faces` will search for faces in
38+
an image and return the coordinates in the image of each `landmark type`_ that
39+
was detected.
10240

41+
.. _landmark type: https://cloud.google.com/vision/reference/rest/v1/images/annotate#type_1
10342

10443
.. code-block:: python
10544
@@ -119,13 +58,13 @@ see: https://cloud.google.com/vision/reference/rest/v1/images/annotate#type_1
11958
0.02545464
12059
12160
122-
12361
Label Detection
12462
~~~~~~~~~~~~~~~
12563

126-
Image labels are a way to help categorize the contents of an image.
127-
If you have an image with a car, person and a dog it, label detection will
128-
attempt to identify those objects.
64+
:meth:`~google.cloud.vision.image.Image.detect_labels` will attempt to label
65+
objects in an image. If there is a car, person and a dog in the image, label
66+
detection will attempt to identify those objects and score the level of
67+
certainty from ``0.0 to 1.0``.
12968

13069
.. code-block:: python
13170
@@ -142,15 +81,16 @@ attempt to identify those objects.
14281
Landmark Detection
14382
~~~~~~~~~~~~~~~~~~
14483

145-
The API will attemtp to detect landmarks such as Mount Rushmore and
146-
the Sydney Opera House. The API will also provide their known geographical
147-
locations if available.
84+
:meth:`~google.cloud.vision.image.Image.detect_landmarks` will attempt to
85+
detect landmarks such as "Mount Rushmore" and the "Sydney Opera House". The API
86+
will also provide their known geographical locations if available.
14887

14988
.. code-block:: python
15089
15190
>>> from google.cloud import vision
15291
>>> client = vision.Client()
153-
>>> image = client.image('./image.jpg')
92+
>>> with open('./image.jpg', 'rb') as image_file:
93+
... image = client.image(content=image_file.read())
15494
>>> landmarks = image.detect_landmarks()
15595
>>> landmarks[0].description
15696
'Sydney Opera House'
@@ -163,52 +103,78 @@ locations if available.
163103
>>> landmarks[0].bounding_poly.vertices[0].y_coordinate
164104
162
165105
106+
166107
Logo Detection
167108
~~~~~~~~~~~~~~
168109

169-
Google Vision can also attempt to detect company and brand logos in images.
110+
With :meth:`~google.cloud.vision.image.Image.detect_logos`, you can identify
111+
brand logos in an image. Their shape and location in the image can be found by
112+
iterating through the detected logo's ``vertices``.
170113

171114
.. code-block:: python
172115
173116
>>> from google.cloud import vision
174117
>>> client = vision.Client()
175-
>>> image = client.image('./image.jpg')
176-
>>> logos = image.detect_logos(limit=1)
177-
>>> results.logos[0].description
118+
>>> with open('./image.jpg', 'rb') as image_file:
119+
... image = client.image(content=image_file.read())
120+
>>> logos = image.detect_logos(limit=3)
121+
>>> print(len(logos))
122+
3
123+
>>> first_logo = logos[0]
124+
>>> first_logo.description
178125
'Google'
179-
>>> logos[0].score
126+
>>> first_logo.score
180127
0.9795432
181-
>>> logos[0].bounding_poly.vertices[0].x_coordinate
128+
>>> print(len(first_logo.bounding_poly.vertices))
129+
4
130+
>>> first_logo.bounding_poly.vertices[0].x_coordinate
182131
78
183-
>>> logos[0].bounding_poly.vertices[0].y_coordinate
132+
>>> first_logo.bounding_poly.vertices[0].y_coordinate
184133
62
185134
135+
186136
Safe Search Detection
187137
~~~~~~~~~~~~~~~~~~~~~
188138

189-
Detecting safe search properties of an image.
139+
:meth:`~google.cloud.vision.image.Image.detect_safe_search` will try to
140+
categorize the entire contents of the image under four categories.
141+
142+
- adult: Represents the likelihood that the image contains adult content.
143+
- spoof: The likelihood that an obvious modification was made to the image's
144+
canonical version to make it appear funny or offensive.
145+
- medical: Likelihood this is a medical image.
146+
- violence: Violence likelihood.
190147

191148
.. code-block:: python
192149
193150
>>> from google.cloud import vision
194151
>>> client = vision.Client()
195-
>>> image = client.image('./image.jpg')
196-
>>> safe_search = image.detect_safe_search()
197-
>>> safe_search[0].adult
152+
>>> with open('./image.jpg', 'rb') as image_file:
153+
... image = client.image(content=image_file.read())
154+
>>> safe_search_results = image.detect_safe_search()
155+
>>> safe_search = safe_search_results[0]
156+
>>> safe_search.adult
198157
'VERY_UNLIKELY'
199-
>>> safe_search[0].medical
200-
'UNLIKELY'
158+
>>> safe_search.spoof
159+
'POSSIBLE'
160+
>>> safe_search.medical
161+
'VERY_LIKELY'
162+
>>> safe_search.violence
163+
'LIKELY'
164+
201165
202166
Text Detection
203167
~~~~~~~~~~~~~~
204168

205-
Detecting text with ORC from an image.
169+
:meth:`~google.cloud.vision.image.Image.detect_text` performs OCR to find text
170+
in an image.
206171

207172
.. code-block:: python
208173
209174
>>> from google.cloud import vision
210175
>>> client = vision.Client()
211-
>>> image = client.image('./image.jpg')
176+
>>> with open('./image.jpg', 'rb') as image_file:
177+
... image = client.image(content=image_file.read())
212178
>>> texts = image.detect_text()
213179
>>> texts[0].locale
214180
'en'
@@ -217,23 +183,47 @@ Detecting text with ORC from an image.
217183
>>> texts[1].description
218184
'some other text in the image'
219185
186+
220187
Image Properties
221188
~~~~~~~~~~~~~~~~
222189

223-
Detecting image color properties.
190+
:meth:`~google.cloud.vision.image.Image.detect_properties` will process the
191+
image and determine the dominant colors in the image.
224192

225193
.. code-block:: python
226194
227195
>>> from google.cloud import vision
228196
>>> client = vision.Client()
229-
>>> image = client.image('./image.jpg')
197+
>>> with open('./image.jpg', 'rb') as image_file:
198+
... image = client.image(content=image_file.read())
230199
>>> results = image.detect_properties()
231200
>>> colors = results[0]
232-
>>> colors[0].red
201+
>>> first_color = colors[0]
202+
>>> first_color.red
233203
244
234-
>>> colors[0].blue
204+
>>> first_color.blue
235205
134
236-
>>> colors[0].score
206+
>>> first_color.score
237207
0.65519291
238-
>>> colors[0].pixel_fraction
208+
>>> first_color.pixel_fraction
239209
0.758658
210+
211+
212+
No results found
213+
~~~~~~~~~~~~~~~~
214+
215+
If no results for the detection performed can be extracted from the image, then
216+
an empty list is returned. This behavior is similiar with all detection types.
217+
218+
219+
Example with :meth:`~google.cloud.vision.image.Image.detect_logos`:
220+
221+
.. code-block:: python
222+
223+
>>> from google.cloud import vision
224+
>>> client = vision.Client()
225+
>>> with open('./image.jpg', 'rb') as image_file:
226+
... image = client.image(content=image_file.read())
227+
>>> logos = image.detect_logos(limit=3)
228+
>>> logos
229+
[]

0 commit comments

Comments
 (0)