From efff403f769a71fa06d35063303f2cb8606c0bb2 Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Wed, 30 Nov 2016 13:32:46 -0500 Subject: [PATCH] Move emotions to Face class. --- docs/vision-usage.rst | 11 ++--- vision/google/cloud/vision/face.py | 70 +++++++++++++++++++++++------- vision/unit_tests/test_face.py | 14 +++--- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/docs/vision-usage.rst b/docs/vision-usage.rst index 904b8539f3e1..c127e02ad095 100644 --- a/docs/vision-usage.rst +++ b/docs/vision-usage.rst @@ -75,15 +75,16 @@ was detected. >>> client = vision.Client() >>> image = client.image(source_uri='gs://my-test-bucket/image.jpg') >>> faces = image.detect_faces(limit=10) - >>> faces[0].landmarks.left_eye.landmark_type + >>> first_face = faces[0] + >>> first_face.landmarks.left_eye.landmark_type 'LEFT_EYE' - >>> faces[0].landmarks.left_eye.position.x_coordinate + >>> first_face.landmarks.left_eye.position.x_coordinate 1301.2404 - >>> faces[0].detection_confidence + >>> first_face.detection_confidence 0.9863683 - >>> faces[0].joy_likelihood + >>> first_face.joy 0.54453093 - >>> faces[0].anger_likelihood + >>> first_face.anger 0.02545464 diff --git a/vision/google/cloud/vision/face.py b/vision/google/cloud/vision/face.py index 4e4da85dc06f..dc1c6470225a 100644 --- a/vision/google/cloud/vision/face.py +++ b/vision/google/cloud/vision/face.py @@ -101,44 +101,44 @@ def from_api_repr(cls, response): anger_likelihood) @property - def joy_likelihood(self): - """Likelihood of joy in detected face. + def anger(self): + """Likelihood of anger in detected face. :rtype: str :returns: String derived from :class:`~google.cloud.vision.face.Likelihood`. """ - return self._joy_likelihood + return self._anger_likelihood @property - def sorrow_likelihood(self): - """Likelihood of sorrow in detected face. + def joy(self): + """Likelihood of joy in detected face. :rtype: str :returns: String derived from :class:`~google.cloud.vision.face.Likelihood`. """ - return self._sorrow_likelihood + return self._joy_likelihood @property - def surprise_likelihood(self): - """Likelihood of surprise in detected face. + def sorrow(self): + """Likelihood of sorrow in detected face. :rtype: str :returns: String derived from :class:`~google.cloud.vision.face.Likelihood`. """ - return self._surprise_likelihood + return self._sorrow_likelihood @property - def anger_likelihood(self): - """Likelihood of anger in detected face. + def surprise(self): + """Likelihood of surprise in detected face. :rtype: str :returns: String derived from :class:`~google.cloud.vision.face.Likelihood`. """ - return self._anger_likelihood + return self._surprise_likelihood class Face(object): @@ -182,6 +182,16 @@ def from_api_repr(cls, response): headwear_likelihood, image_properties, landmarks, landmarking_confidence) + @property + def anger(self): + """Accessor to likelihood that the detected face is angry. + + :rtype: str + :returns: String derived from + :class:`~google.cloud.vision.face.Likelihood`. + """ + return self.emotions.anger + @property def angles(self): """Accessor to the pan, tilt and roll angles of a Face. @@ -230,7 +240,7 @@ def fd_bounds(self): return self._fd_bounds @property - def headwear_likelihood(self): + def headwear(self): """Headwear likelihood. :rtype: :class:`~google.cloud.vision.face.Likelihood` @@ -248,6 +258,16 @@ def image_properties(self): """ return self._image_properties + @property + def joy(self): + """Likelihood of joy in detected face. + + :rtype: str + :returns: String derived from + :class:`~google.cloud.vision.face.Likelihood`. + """ + return self.emotions.joy + @property def landmarks(self): """Accessor to the facial landmarks detected in a face. @@ -267,6 +287,26 @@ def landmarking_confidence(self): """ return self._landmarking_confidence + @property + def sorrow(self): + """Likelihood of sorrow in detected face. + + :rtype: str + :returns: String derived from + :class:`~google.cloud.vision.face.Likelihood`. + """ + return self.emotions.sorrow + + @property + def surprise(self): + """Likelihood of surprise in detected face. + + :rtype: str + :returns: String derived from + :class:`~google.cloud.vision.face.Likelihood`. + """ + return self.emotions.surprise + class FaceImageProperties(object): """A representation of the image properties from face detection.""" @@ -289,7 +329,7 @@ def from_api_repr(cls, response): return cls(blurred_likelihood, underexposed_likelihood) @property - def blurred_likelihood(self): + def blurred(self): """Likelihood of the image being blurred. :rtype: str @@ -299,7 +339,7 @@ def blurred_likelihood(self): return self._blurred_likelihood @property - def underexposed_likelihood(self): + def underexposed(self): """Likelihood that the image used for detection was underexposed. :rtype: str diff --git a/vision/unit_tests/test_face.py b/vision/unit_tests/test_face.py index e37da8c42452..0c2a568268d6 100644 --- a/vision/unit_tests/test_face.py +++ b/vision/unit_tests/test_face.py @@ -44,13 +44,13 @@ def test_face_landmarks(self): def test_facial_emotions(self): from google.cloud.vision.face import Likelihood self.assertEqual(Likelihood.VERY_LIKELY, - self.face.emotions.joy_likelihood) + self.face.joy) self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.emotions.sorrow_likelihood) + self.face.sorrow) self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.emotions.surprise_likelihood) + self.face.surprise) self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.emotions.anger_likelihood) + self.face.anger) def test_faciale_angles(self): self.assertEqual(-0.43419784, self.face.angles.roll) @@ -60,11 +60,11 @@ def test_faciale_angles(self): def test_face_headware_and_blur_and_underexposed(self): from google.cloud.vision.face import Likelihood self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.image_properties.blurred_likelihood) + self.face.image_properties.blurred) self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.headwear_likelihood) + self.face.headwear) self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.image_properties.underexposed_likelihood) + self.face.image_properties.underexposed) def test_face_bounds(self): self.assertEqual(4, len(self.face.bounds.vertices))