Skip to content

Commit 732c099

Browse files
committed
Add GAPIC support for face detection.
1 parent 791f5b8 commit 732c099

File tree

11 files changed

+322
-58
lines changed

11 files changed

+322
-58
lines changed

system_tests/vision.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def _assert_coordinate(self, coordinate):
6363
if coordinate is None:
6464
return
6565
self.assertIsInstance(coordinate, (int, float))
66-
self.assertNotEqual(coordinate, 0.0)
6766

6867
def _assert_likelihood(self, likelihood):
6968
from google.cloud.vision.likelihood import Likelihood
@@ -137,6 +136,7 @@ def test_detect_logos_gcs(self):
137136

138137
class TestVisionClientFace(BaseVisionTestCase):
139138
def setUp(self):
139+
Config.CLIENT = vision.Client(use_gax=True)
140140
self.to_delete_by_case = []
141141

142142
def tearDown(self):
@@ -150,7 +150,7 @@ def _assert_landmarks(self, landmarks):
150150

151151
for landmark in LandmarkTypes:
152152
if landmark is not LandmarkTypes.UNKNOWN_LANDMARK:
153-
feature = getattr(landmarks, landmark.value.lower())
153+
feature = getattr(landmarks, landmark.name.lower())
154154
self.assertIsInstance(feature, Landmark)
155155
self.assertIsInstance(feature.position, Position)
156156
self._assert_coordinate(feature.position.x_coordinate)
@@ -297,6 +297,7 @@ class TestVisionClientLandmark(BaseVisionTestCase):
297297
DESCRIPTIONS = ('Mount Rushmore',)
298298

299299
def setUp(self):
300+
Config.CLIENT = vision.Client(use_gax=True)
300301
self.to_delete_by_case = []
301302

302303
def tearDown(self):
@@ -356,6 +357,7 @@ def test_detect_landmark_filename(self):
356357

357358
class TestVisionClientSafeSearch(BaseVisionTestCase):
358359
def setUp(self):
360+
Config.CLIENT = vision.Client(use_gax=False)
359361
self.to_delete_by_case = []
360362

361363
def tearDown(self):
@@ -478,6 +480,7 @@ def test_detect_text_filename(self):
478480

479481
class TestVisionClientImageProperties(BaseVisionTestCase):
480482
def setUp(self):
483+
Config.CLIENT = vision.Client(use_gax=False)
481484
self.to_delete_by_case = []
482485

483486
def tearDown(self):

vision/google/cloud/vision/_gax.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from google.cloud.gapic.vision.v1 import image_annotator_client
1818
from google.cloud.grpc.vision.v1 import image_annotator_pb2
1919

20-
from google.cloud._helpers import _to_bytes
21-
2220
from google.cloud.vision.annotations import Annotations
2321

2422

@@ -85,7 +83,7 @@ def _to_gapic_image(image):
8583
:class:`~google.cloud.vision.image.Image`.
8684
"""
8785
if image.content is not None:
88-
return image_annotator_pb2.Image(content=_to_bytes(image.content))
86+
return image_annotator_pb2.Image(content=image.content)
8987
if image.source is not None:
9088
return image_annotator_pb2.Image(
9189
source=image_annotator_pb2.ImageSource(

vision/google/cloud/vision/annotations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def _process_image_annotations(image):
119119
:returns: Dictionary populated with entities from response.
120120
"""
121121
return {
122+
'faces': _make_faces_from_pb(image.face_annotations),
122123
'labels': _make_entity_from_pb(image.label_annotations),
123124
'landmarks': _make_entity_from_pb(image.landmark_annotations),
124125
'logos': _make_entity_from_pb(image.logo_annotations),
@@ -139,6 +140,22 @@ def _make_entity_from_pb(annotations):
139140
return [EntityAnnotation.from_pb(annotation) for annotation in annotations]
140141

141142

143+
def _make_faces_from_pb(annotations):
144+
"""Create face objects from a gRPC response.
145+
146+
:type annotations:
147+
:class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.FaceAnnotation`
148+
:param annotations: gRPC instance of ``FaceAnnotation``.
149+
150+
:rtype: list
151+
:returns: List of ``Face``.
152+
"""
153+
faces = []
154+
for annotation in annotations:
155+
faces.append(Face.from_pb(annotation))
156+
return faces
157+
158+
142159
def _entity_from_response_type(feature_type, results):
143160
"""Convert a JSON result to an entity type based on the feature.
144161

0 commit comments

Comments
 (0)