Skip to content

Commit

Permalink
Add GAPIC support for face detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Jan 12, 2017
1 parent 9258950 commit 69f64e7
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 58 deletions.
7 changes: 5 additions & 2 deletions system_tests/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def _assert_coordinate(self, coordinate):
if coordinate is None:
return
self.assertIsInstance(coordinate, (int, float))
self.assertNotEqual(coordinate, 0.0)

def _assert_likelihood(self, likelihood):
from google.cloud.vision.likelihood import Likelihood
Expand Down Expand Up @@ -133,6 +132,7 @@ def test_detect_logos_gcs(self):

class TestVisionClientFace(BaseVisionTestCase):
def setUp(self):
Config.CLIENT = vision.Client(use_gax=True)
self.to_delete_by_case = []

def tearDown(self):
Expand All @@ -146,7 +146,7 @@ def _assert_landmarks(self, landmarks):

for landmark in LandmarkTypes:
if landmark is not LandmarkTypes.UNKNOWN_LANDMARK:
feature = getattr(landmarks, landmark.value.lower())
feature = getattr(landmarks, landmark.name.lower())
self.assertIsInstance(feature, Landmark)
self.assertIsInstance(feature.position, Position)
self._assert_coordinate(feature.position.x_coordinate)
Expand Down Expand Up @@ -296,6 +296,7 @@ class TestVisionClientLandmark(BaseVisionTestCase):
DESCRIPTIONS = ('Mount Rushmore',)

def setUp(self):
Config.CLIENT = vision.Client(use_gax=True)
self.to_delete_by_case = []

def tearDown(self):
Expand Down Expand Up @@ -352,6 +353,7 @@ def test_detect_landmark_filename(self):

class TestVisionClientSafeSearch(BaseVisionTestCase):
def setUp(self):
Config.CLIENT = vision.Client(use_gax=False)
self.to_delete_by_case = []

def tearDown(self):
Expand Down Expand Up @@ -471,6 +473,7 @@ def test_detect_text_filename(self):

class TestVisionClientImageProperties(BaseVisionTestCase):
def setUp(self):
Config.CLIENT = vision.Client(use_gax=False)
self.to_delete_by_case = []

def tearDown(self):
Expand Down
4 changes: 1 addition & 3 deletions vision/google/cloud/vision/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from google.cloud.gapic.vision.v1 import image_annotator_client
from google.cloud.grpc.vision.v1 import image_annotator_pb2

from google.cloud._helpers import _to_bytes

from google.cloud.vision.annotations import Annotations


Expand Down Expand Up @@ -82,7 +80,7 @@ def _to_gapic_image(image):
:class:`~google.cloud.vision.image.Image`.
"""
if image.content is not None:
return image_annotator_pb2.Image(content=_to_bytes(image.content))
return image_annotator_pb2.Image(content=image.content)
if image.source is not None:
return image_annotator_pb2.Image(
source=image_annotator_pb2.ImageSource(
Expand Down
16 changes: 16 additions & 0 deletions vision/google/cloud/vision/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ def _make_entity_from_pb(annotations):
return [EntityAnnotation.from_pb(annotation) for annotation in annotations]


def _make_faces_from_pb(annotations):
"""Create face objects from a gRPC response.
:type annotations:
:class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.FaceAnnotation`
:param annotations: gRPC instance of ``FaceAnnotation``.
:rtype: list
:returns: List of ``Face``.
"""
faces = []
for annotation in annotations:
faces.append(Face.from_pb(annotation))
return faces


def _entity_from_response_type(feature_type, results):
"""Convert a JSON result to an entity type based on the feature.
Expand Down
Loading

0 comments on commit 69f64e7

Please sign in to comment.