Skip to content

Commit c8234f3

Browse files
committed
Add Vision API Face detection.
1 parent df1a27f commit c8234f3

12 files changed

Lines changed: 749 additions & 40 deletions

File tree

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
vision-client
152152
vision-image
153153
vision-feature
154+
vision-face
154155

155156
.. toctree::
156157
:maxdepth: 0

docs/vision-face.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Vision Face
2+
============
3+
4+
Face
5+
~~~~~
6+
7+
.. automodule:: gcloud.vision.face
8+
:members:
9+
:undoc-members:
10+
:show-inheritance:

docs/vision-image.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,19 @@ Image
88
:members:
99
:undoc-members:
1010
:show-inheritance:
11+
12+
Geometry
13+
~~~~~~~~
14+
15+
.. automodule:: gcloud.vision.geometry
16+
:members:
17+
:undoc-members:
18+
:show-inheritance:
19+
20+
Likelihood
21+
~~~~~~~~~~
22+
23+
.. automodule:: gcloud.vision.likelihood
24+
:members:
25+
:undoc-members:
26+
:show-inheritance:

docs/vision-usage.rst

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,30 @@ Annotate a single image
3838

3939
.. code-block:: python
4040
41+
>>> import io
4142
>>> from google.cloud import vision
4243
>>> client = vision.Client()
43-
>>> image = client.image('./image.png')
44+
>>> with io.open('./image.png', 'rb') as image_file:
45+
>>> image = client.image(image_file.read())
4446
>>> faces = image.detect_faces(limit=10)
47+
>>> faces[0].landmarks.left_eye.position.x
48+
... 1004.8003
4549
4650
Annotate multiple images
4751
~~~~~~~~~~~~~~~~~~~~~~~~
4852

4953
.. code-block:: python
5054
51-
>>> first_image = client.image('./image.jpg')
55+
>>> import io
56+
>>> from gcloud import vision
57+
>>> client = vision.Client()
58+
>>> with io.open('./image.png', 'rb') as image_file:
59+
>>> first_image = client.image(image_file.read())
5260
>>> second_image = client.image('gs://my-storage-bucket/image2.jpg')
5361
>>> with client.batch():
5462
... labels = first_image.detect_labels()
5563
... faces = second_image.detect_faces(limit=10)
5664
57-
or
58-
59-
.. code-block:: python
60-
61-
>>> images = []
62-
>>> images.append(client.image('./image.jpg'))
63-
>>> images.append(client.image('gs://my-storage-bucket/image2.jpg'))
64-
>>> faces = client.detect_faces_multi(images, limit=10)
65-
6665
No results returned
6766
~~~~~~~~~~~~~~~~~~~
6867

@@ -86,9 +85,13 @@ You can call the detection method manually.
8685
.. code-block:: python
8786
8887
>>> from google.cloud import vision
88+
>>> from gcloud.vision.image import Feature
89+
>>> from gcloud.vision.image import FeatureTypes
8990
>>> client = vision.Client()
9091
>>> image = client.image('gs://my-test-bucket/image.jpg')
91-
>>> faces = image.detect(type=vision.FACE_DETECTION, limit=10)
92+
>>> features = [Feature(FeatureTypes.FACE_DETECTION, 5),
93+
Feature(FeatureTypes.LOGO_DETECTION, 3)]
94+
>>> annotations = image.detect(features)
9295
9396
Face Detection
9497
~~~~~~~~~~~~~~
@@ -102,11 +105,11 @@ see: https://cloud.google.com/vision/reference/rest/v1/images/annotate#type_1
102105
103106
>>> from google.cloud import vision
104107
>>> client = vision.Client()
105-
>>> image = client.image('./image.jpg')
108+
>>> image = client.image('gs://my-test-bucket/image.jpg')
106109
>>> faces = image.detect_faces(limit=10)
107-
>>> faces[0].landmarks[0].type
110+
>>> faces[0].landmarks.left_eye.landmark_type
108111
'LEFT_EYE'
109-
>>> faces[0].landmarks[0].position.x
112+
>>> faces[0].landmarks.left_eye.position.x
110113
1301.2404
111114
>>> faces[0].detection_confidence
112115
0.9863683

google/cloud/vision/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
"""Google Cloud Vision API package."""
16+
from gcloud.vision.client import Client

google/cloud/vision/client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,22 @@ def annotate(self, image, features):
9898
:rtype: dict
9999
:returns: List of annotations.
100100
"""
101-
img = Image(image, self)
102-
request = VisionRequest(img, features)
101+
request = VisionRequest(image, features)
103102

104103
data = {'requests': [request.as_dict()]}
105104
response = self.connection.api_request(method='POST',
106105
path='/images:annotate',
107106
data=data)
108107

109108
return response['responses'][0]
109+
110+
def image(self, image_source):
111+
"""Get instance of Image using current client.
112+
113+
:type image_source: str or bytes
114+
:param image_source: Byte stream of an image or a GCS URI.
115+
116+
:rtype: :class:`gcloud.vision.image.Image`
117+
:returns: Image instance with the current client attached.
118+
"""
119+
return Image(client=self, image_source=image_source)

0 commit comments

Comments
 (0)