Skip to content

Commit 7520c5c

Browse files
committed
Change image init. Update doc class references.
1 parent d32042c commit 7520c5c

8 files changed

Lines changed: 65 additions & 61 deletions

File tree

docs/vision-usage.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Annotate a single image
4242
>>> from google.cloud import vision
4343
>>> client = vision.Client()
4444
>>> with io.open('./image.png', 'rb') as image_file:
45-
... image = client.image(image_file.read())
45+
... image = client.image(content=image_file.read())
4646
>>> faces = image.detect_faces(limit=10)
4747
>>> faces[0].landmarks.left_eye.position.x_coordinate
4848
... 1004.8003
@@ -52,15 +52,15 @@ Annotate multiple images
5252

5353
.. code-block:: python
5454
55-
>>> import io
56-
>>> from gcloud import vision
57-
>>> client = vision.Client()
55+
>>> import io
56+
>>> from gcloud import vision
57+
>>> client = vision.Client()
5858
>>> with io.open('./image.png', 'rb') as image_file:
59-
... first_image = client.image(image_file.read())
60-
>>> second_image = client.image('gs://my-storage-bucket/image2.jpg')
61-
>>> with client.batch():
62-
... labels = first_image.detect_labels()
63-
... faces = second_image.detect_faces(limit=10)
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)
6464
6565
No results returned
6666
~~~~~~~~~~~~~~~~~~~
@@ -71,7 +71,7 @@ Failing annotations return no results for the feature type requested.
7171
7272
>>> from google.cloud import vision
7373
>>> client = vision.Client()
74-
>>> image = client.image('./image.jpg')
74+
>>> image = client.image(source_uri='gs://my-storage-bucket/image.jpg')
7575
>>> logos = image.detect_logos(limit=10)
7676
>>> logos
7777
[]
@@ -88,7 +88,7 @@ You can call the detection method manually.
8888
>>> from google.cloud.vision.image import Feature
8989
>>> from google.cloud.vision.image import FeatureTypes
9090
>>> client = vision.Client()
91-
>>> image = client.image('gs://my-test-bucket/image.jpg')
91+
>>> image = client.image(source_uri='gs://my-test-bucket/image.jpg')
9292
>>> features = [Feature(FeatureTypes.FACE_DETECTION, 5),
9393
... Feature(FeatureTypes.LOGO_DETECTION, 3)]
9494
>>> annotations = image.detect(features)
@@ -105,7 +105,7 @@ see: https://cloud.google.com/vision/reference/rest/v1/images/annotate#type_1
105105
106106
>>> from google.cloud import vision
107107
>>> client = vision.Client()
108-
>>> image = client.image('gs://my-test-bucket/image.jpg')
108+
>>> image = client.image(source_uri='gs://my-test-bucket/image.jpg')
109109
>>> faces = image.detect_faces(limit=10)
110110
>>> faces[0].landmarks.left_eye.landmark_type
111111
'LEFT_EYE'
@@ -131,7 +131,7 @@ attempt to identify those objects.
131131
132132
>>> from google.cloud import vision
133133
>>> client = vision.Client()
134-
>>> image = client.image('./image.jpg')
134+
>>> image = client.image(source_uri='gs://my-storage-bucket/image.jpg')
135135
>>> labels = image.detect_labels(limit=3)
136136
>>> labels[0].description
137137
'automobile'

google/cloud/vision/client.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class VisionRequest(object):
2525
"""Request container with image and features information to annotate.
2626
27-
:type features: list of :class:`gcoud.vision.feature.Feature`.
27+
:type features: list of :class:`~gcoud.vision.feature.Feature`.
2828
:param features: The features that dictate which annotations to run.
2929
3030
:type image: bytes
@@ -89,7 +89,7 @@ def annotate(self, image, features):
8989
:param image: A string which can be a URL, a Google Cloud Storage path,
9090
or a byte stream of the image.
9191
92-
:type features: list of :class:`google.cloud.vision.feature.Feature`
92+
:type features: list of :class:`~google.cloud.vision.feature.Feature`
9393
:param features: The type of detection that the Vision API should
9494
use to determine image attributes. Pricing is
9595
based on the number of Feature Types.
@@ -107,13 +107,16 @@ def annotate(self, image, features):
107107

108108
return response['responses'][0]
109109

110-
def image(self, image_source):
110+
def image(self, content=None, source_uri=None):
111111
"""Get instance of Image using current client.
112112
113-
:type image_source: str or bytes
114-
:param image_source: Byte stream of an image or a GCS URI.
113+
:type content: bytes
114+
:param content: Byte stream of an image.
115115
116-
:rtype: :class:`google.cloud.vision.image.Image`
116+
:type source_uri: str
117+
:param source_uri: Google Cloud Storage URI of image.
118+
119+
:rtype: :class:`~google.cloud.vision.image.Image`
117120
:returns: Image instance with the current client attached.
118121
"""
119-
return Image(client=self, image_source=image_source)
122+
return Image(client=self, content=content, source_uri=source_uri)

google/cloud/vision/face.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, roll, pan, tilt):
3131
def from_api_repr(cls, response):
3232
"""Factory: construct the angles from an Vision API response.
3333
34-
:rtype: :class:`google.cloud.vision.face.Angles`
34+
:rtype: :class:`~google.cloud.vision.face.Angles`
3535
:returns: An `Angles` instance with data parsed from `response`.
3636
"""
3737
roll = response['rollAngle']
@@ -88,7 +88,7 @@ def from_api_repr(cls, response):
8888
:type response: dict
8989
:param response: Response dictionary representing a face.
9090
91-
:rtype: :class:`google.cloud.vision.face.Emotions`
91+
:rtype: :class:`~google.cloud.vision.face.Emotions`
9292
:returns: Populated instance of `Emotions`.
9393
"""
9494
joy_likelihood = getattr(Likelihood, response['joyLikelihood'])
@@ -106,7 +106,7 @@ def joy_likelihood(self):
106106
107107
:rtype: str
108108
:returns: String derived from
109-
:class:`google.cloud.vision.face.Likelihood`.
109+
:class:`~google.cloud.vision.face.Likelihood`.
110110
"""
111111
return self._joy_likelihood
112112

@@ -116,7 +116,7 @@ def sorrow_likelihood(self):
116116
117117
:rtype: str
118118
:returns: String derived from
119-
:class:`google.cloud.vision.face.Likelihood`.
119+
:class:`~google.cloud.vision.face.Likelihood`.
120120
"""
121121
return self._sorrow_likelihood
122122

@@ -126,7 +126,7 @@ def surprise_likelihood(self):
126126
127127
:rtype: str
128128
:returns: String derived from
129-
:class:`google.cloud.vision.face.Likelihood`.
129+
:class:`~google.cloud.vision.face.Likelihood`.
130130
"""
131131
return self._surprise_likelihood
132132

@@ -136,7 +136,7 @@ def anger_likelihood(self):
136136
137137
:rtype: str
138138
:returns: String derived from
139-
:class:`google.cloud.vision.face.Likelihood`.
139+
:class:`~google.cloud.vision.face.Likelihood`.
140140
"""
141141
return self._anger_likelihood
142142

@@ -164,7 +164,7 @@ def from_api_repr(cls, response):
164164
:type response: dict
165165
:param response: Face annotation dict returned from the Vision API.
166166
167-
:rtype: :class:`google.cloud.vision.face.Face`
167+
:rtype: :class:`~google.cloud.vision.face.Face`
168168
:returns: A instance of `Face` with data parsed from `response`.
169169
"""
170170
angles = Angles.from_api_repr(response)
@@ -186,7 +186,7 @@ def from_api_repr(cls, response):
186186
def angles(self):
187187
"""Accessor to the pan, tilt and roll angles of a Face.
188188
189-
:rtype: :class:`google.cloud.vision.face.Angles`
189+
:rtype: :class:`~google.cloud.vision.face.Angles`
190190
:returns: Pan, tilt and roll angles of the detected face.
191191
"""
192192

@@ -196,7 +196,7 @@ def angles(self):
196196
def bounds(self):
197197
"""Accessor to the bounding poly information of the detected face.
198198
199-
:rtype: :class:`google.cloud.vision.face.Bounds`
199+
:rtype: :class:`~google.cloud.vision.face.Bounds`
200200
:returns: An instance of ``Bounds`` which has a list of vertices.
201201
"""
202202
return self._bounds
@@ -214,7 +214,7 @@ def detection_confidence(self):
214214
def emotions(self):
215215
"""Accessor to the possible emotions expressed in the detected face.
216216
217-
:rtype: :class:`google.cloud.vision.face.Emotions`
217+
:rtype: :class:`~google.cloud.vision.face.Emotions`
218218
:returns: An instance of ``Emotions`` with joy, sorrow, anger, surprise
219219
likelihood.
220220
"""
@@ -224,7 +224,7 @@ def emotions(self):
224224
def fd_bounds(self):
225225
"""Accessor to the skin area bounding poly of the detected face.
226226
227-
:rtype: :class:`google.cloud.vision.image.FDBounds`
227+
:rtype: :class:`~google.cloud.vision.image.FDBounds`
228228
:returns: An instance of ``FDBounds`` which has a list of vertices.
229229
"""
230230
return self._fd_bounds
@@ -233,17 +233,17 @@ def fd_bounds(self):
233233
def headwear_likelihood(self):
234234
"""Headwear likelihood.
235235
236-
:rtype: :class:`google.cloud.vision.face.Likelihood`
236+
:rtype: :class:`~google.cloud.vision.face.Likelihood`
237237
:returns: String representing the likelihood based on
238-
:class:`google.cloud.vision.face.Likelihood`
238+
:class:`~google.cloud.vision.face.Likelihood`
239239
"""
240240
return self._headwear_likelihood
241241

242242
@property
243243
def image_properties(self):
244244
"""Image properties from imaged used in face detection.
245245
246-
:rtype: :class:`google.cloud.vision.face.FaceImageProperties`
246+
:rtype: :class:`~google.cloud.vision.face.FaceImageProperties`
247247
:returns: ``FaceImageProperties`` object with image properties.
248248
"""
249249
return self._image_properties
@@ -252,7 +252,7 @@ def image_properties(self):
252252
def landmarks(self):
253253
"""Accessor to the facial landmarks detected in a face.
254254
255-
:rtype: :class:`google.cloud.vision.face.Landmarks`
255+
:rtype: :class:`~google.cloud.vision.face.Landmarks`
256256
:returns: ``Landmarks`` object with facial landmarks as properies.
257257
"""
258258
return self._landmarks
@@ -278,7 +278,7 @@ def __init__(self, blurred_likelihood, underexposed_likelihood):
278278
def from_api_repr(cls, response):
279279
"""Factory: construct image properties from image.
280280
281-
:rtype: :class:`google.cloud.vision.face.FaceImageProperties`
281+
:rtype: :class:`~google.cloud.vision.face.FaceImageProperties`
282282
:returns: Instance populated with image property data.
283283
"""
284284
blurred_likelihood = getattr(Likelihood,
@@ -294,7 +294,7 @@ def blurred_likelihood(self):
294294
295295
:rtype: str
296296
:returns: String representation derived from
297-
:class:`google.cloud.vision.face.Position`.
297+
:class:`~google.cloud.vision.face.Position`.
298298
"""
299299
return self._blurred_likelihood
300300

@@ -304,7 +304,7 @@ def underexposed_likelihood(self):
304304
305305
:rtype: str
306306
:returns: String representation derived from
307-
:class:`google.cloud.vision.face.Position`.
307+
:class:`~google.cloud.vision.face.Position`.
308308
"""
309309
return self._underexposed_likelihood
310310

@@ -369,7 +369,7 @@ def from_api_repr(cls, response_landmark):
369369
:type response_landmark: dict
370370
:param response_landmark: Landmark representation from Vision API.
371371
372-
:rtype: :class:`google.cloud.vision.face.Landmark`
372+
:rtype: :class:`~google.cloud.vision.face.Landmark`
373373
:returns: Populated instance of `Landmark`.
374374
"""
375375
position = Position.from_api_repr(response_landmark['position'])
@@ -380,7 +380,7 @@ def from_api_repr(cls, response_landmark):
380380
def position(self):
381381
"""Landmark position on face.
382382
383-
:rtype: :class:`google.cloud.vision.face.Position`
383+
:rtype: :class:`~google.cloud.vision.face.Position`
384384
:returns: Instance of `Position` with landmark coordinates.
385385
"""
386386
return self._position

google/cloud/vision/feature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Feature(object):
3434
3535
:type feature_type: str
3636
:param feature_type: String representation of
37-
:class:`google.cloud.vision.feature.FeatureType`.
37+
:class:`~google.cloud.vision.feature.FeatureType`.
3838
3939
:type max_results: int
4040
:param max_results: Number of results to return for the specified

google/cloud/vision/geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def from_api_repr(cls, response_vertices):
2727
:type response_vertices: dict
2828
:param response_vertices: List of vertices.
2929
30-
:rtype: :class:`google.cloud.vision.geometry.BoundsBase`
30+
:rtype: :class:`~google.cloud.vision.geometry.BoundsBase`
3131
:returns: Instance of BoundsBase with populated verticies.
3232
"""
3333
vertices = []
@@ -61,7 +61,7 @@ def __init__(self, x_coordinate, y_coordinate, z_coordinate):
6161
def from_api_repr(cls, response_position):
6262
"""Factory: construct 3D position from API response.
6363
64-
:rtype: :class:`google.cloud.vision.geometry.Position`
64+
:rtype: :class:`~google.cloud.vision.geometry.Position`
6565
:returns: `Position` constructed with 3D points from API response.
6666
"""
6767
x_coordinate = response_position['x']

google/cloud/vision/image.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from base64 import b64encode
1919

2020
from google.cloud._helpers import _to_bytes
21-
from google.cloud._helpers import _bytes_to_unicode
2221
from google.cloud.vision.face import Face
2322
from google.cloud.vision.feature import Feature
2423
from google.cloud.vision.feature import FeatureTypes
@@ -27,23 +26,25 @@
2726
class Image(object):
2827
"""Image representation containing information to be annotate.
2928
30-
:type image_source: str
31-
:param image_source: A string which can a Google Cloud Storage URI, or
32-
a byte stream of the image.
29+
:type content: bytes
30+
:param content: Byte stream of an image.
3331
34-
:type client: :class:`Client`
32+
:type source_uri: str
33+
:param source_uri: Google Cloud Storage URI of image.
34+
35+
:type client: :class:`~google.cloud.vision.client.Client`
3536
:param client: Instance of Vision client.
3637
"""
3738

38-
def __init__(self, image_source, client):
39+
def __init__(self, client, content=None, source_uri=None):
3940
self.client = client
4041
self._content = None
4142
self._source = None
4243

43-
if _bytes_to_unicode(image_source).startswith('gs://'):
44-
self._source = image_source
44+
if source_uri:
45+
self._source = source_uri
4546
else:
46-
self._content = b64encode(_to_bytes(image_source))
47+
self._content = b64encode(_to_bytes(content))
4748

4849
def as_dict(self):
4950
"""Generate dictionary structure for request.
@@ -87,7 +88,7 @@ def detect_faces(self, limit=10):
8788
:param limit: The number of faces to try and detect.
8889
8990
:rtype: list
90-
:returns: List of :class:`google.cloud.vision.face.Face`.
91+
:returns: List of :class:`~google.cloud.vision.face.Face`.
9192
"""
9293
faces = []
9394
face_detection_feature = Feature(FeatureTypes.FACE_DETECTION, limit)

unit_tests/vision/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_face_annotation(self):
6464

6565
features = [Feature(feature_type=FeatureTypes.FACE_DETECTION,
6666
max_results=3)]
67-
image = client.image(_IMAGE_CONTENT)
67+
image = client.image(content=_IMAGE_CONTENT)
6868
response = client.annotate(image, features)
6969

7070
self.assertEqual(REQUEST,
@@ -77,7 +77,7 @@ def test_image_with_client(self):
7777
credentials = _Credentials()
7878
client = self._makeOne(project=self.PROJECT,
7979
credentials=credentials)
80-
image = client.image(_IMAGE_SOURCE)
80+
image = client.image(source_uri=_IMAGE_SOURCE)
8181
self.assertTrue(isinstance(image, Image))
8282

8383
def test_face_detection_from_source(self):
@@ -88,7 +88,7 @@ def test_face_detection_from_source(self):
8888
client = self._makeOne(project=self.PROJECT, credentials=credentials)
8989
client.connection = _Connection(RETURNED)
9090

91-
image = client.image(_IMAGE_SOURCE)
91+
image = client.image(source_uri=_IMAGE_SOURCE)
9292
faces = image.detect_faces(limit=3)
9393
self.assertEqual(5, len(faces))
9494
self.assertTrue(isinstance(faces[0], Face))
@@ -105,7 +105,7 @@ def test_face_detection_from_content(self):
105105
client = self._makeOne(project=self.PROJECT, credentials=credentials)
106106
client.connection = _Connection(RETURNED)
107107

108-
image = client.image(_IMAGE_CONTENT)
108+
image = client.image(content=_IMAGE_CONTENT)
109109
faces = image.detect_faces(limit=5)
110110
self.assertEqual(5, len(faces))
111111
self.assertTrue(isinstance(faces[0], Face))

0 commit comments

Comments
 (0)