Skip to content

Commit cfe5b88

Browse files
authored
Merge pull request googleapis#2826 from daspecster/vision-switch-to-enums
Switch to enums and clean up names.
2 parents 5f68b4a + fea8217 commit cfe5b88

7 files changed

Lines changed: 35 additions & 29 deletions

File tree

docs/vision-usage.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ was detected.
116116
>>> faces = image.detect_faces(limit=10)
117117
>>> first_face = faces[0]
118118
>>> first_face.landmarks.left_eye.landmark_type
119-
'LEFT_EYE'
119+
<LandmarkTypes.LEFT_EYE: 'LEFT_EYE'>
120120
>>> first_face.landmarks.left_eye.position.x_coordinate
121121
1301.2404
122122
>>> first_face.detection_confidence
123123
0.9863683
124124
>>> first_face.joy
125-
0.54453093
125+
<Likelihood.VERY_LIKELY: 'VERY_LIKELY'>
126126
>>> first_face.anger
127-
0.02545464
127+
<Likelihood.VERY_UNLIKELY: 'VERY_UNLIKELY'>
128128
129129
130130
***************
@@ -227,13 +227,13 @@ categorize the entire contents of the image under four categories.
227227
>>> safe_search_results = image.detect_safe_search()
228228
>>> safe_search = safe_search_results[0]
229229
>>> safe_search.adult
230-
'VERY_UNLIKELY'
230+
<Likelihood.VERY_UNLIKELY: 'VERY_UNLIKELY'>
231231
>>> safe_search.spoof
232-
'POSSIBLE'
232+
<Likelihood.POSSIBLE: 'POSSIBLE'>
233233
>>> safe_search.medical
234-
'VERY_LIKELY'
234+
<Likelihood.VERY_LIKELY: 'VERY_LIKELY'>
235235
>>> safe_search.violence
236-
'LIKELY'
236+
<Likelihood.LIKELY: 'LIKELY'>
237237
238238
239239
**************

vision/google/cloud/vision/face.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"""Face class representing the Vision API's face detection response."""
1616

1717

18+
from enum import Enum
19+
1820
from google.cloud.vision.geometry import BoundsBase
1921
from google.cloud.vision.likelihood import Likelihood
2022
from google.cloud.vision.geometry import Position
@@ -91,11 +93,10 @@ def from_api_repr(cls, response):
9193
:rtype: :class:`~google.cloud.vision.face.Emotions`
9294
:returns: Populated instance of `Emotions`.
9395
"""
94-
joy_likelihood = getattr(Likelihood, response['joyLikelihood'])
95-
sorrow_likelihood = getattr(Likelihood, response['sorrowLikelihood'])
96-
surprise_likelihood = getattr(Likelihood,
97-
response['surpriseLikelihood'])
98-
anger_likelihood = getattr(Likelihood, response['angerLikelihood'])
96+
joy_likelihood = Likelihood[response['joyLikelihood']]
97+
sorrow_likelihood = Likelihood[response['sorrowLikelihood']]
98+
surprise_likelihood = Likelihood[response['surpriseLikelihood']]
99+
anger_likelihood = Likelihood[response['angerLikelihood']]
99100

100101
return cls(joy_likelihood, sorrow_likelihood, surprise_likelihood,
101102
anger_likelihood)
@@ -172,8 +173,7 @@ def from_api_repr(cls, response):
172173
detection_confidence = response['detectionConfidence']
173174
emotions = Emotions.from_api_repr(response)
174175
fd_bounds = FDBounds.from_api_repr(response['fdBoundingPoly'])
175-
headwear_likelihood = getattr(Likelihood,
176-
response['headwearLikelihood'])
176+
headwear_likelihood = Likelihood[response['headwearLikelihood']]
177177
image_properties = FaceImageProperties.from_api_repr(response)
178178
landmarks = Landmarks(response['landmarks'])
179179
landmarking_confidence = response['landmarkingConfidence']
@@ -321,12 +321,10 @@ def from_api_repr(cls, response):
321321
:rtype: :class:`~google.cloud.vision.face.FaceImageProperties`
322322
:returns: Instance populated with image property data.
323323
"""
324-
blurred_likelihood = getattr(Likelihood,
325-
response['blurredLikelihood'])
326-
underexposed_likelihood = getattr(Likelihood,
327-
response['underExposedLikelihood'])
324+
blurred = Likelihood[response['blurredLikelihood']]
325+
underexposed = Likelihood[response['underExposedLikelihood']]
328326

329-
return cls(blurred_likelihood, underexposed_likelihood)
327+
return cls(blurred, underexposed)
330328

331329
@property
332330
def blurred(self):
@@ -349,7 +347,7 @@ def underexposed(self):
349347
return self._underexposed_likelihood
350348

351349

352-
class FaceLandmarkTypes(object):
350+
class LandmarkTypes(Enum):
353351
"""A representation of the face detection landmark types.
354352
355353
See:
@@ -413,7 +411,7 @@ def from_api_repr(cls, response_landmark):
413411
:returns: Populated instance of `Landmark`.
414412
"""
415413
position = Position.from_api_repr(response_landmark['position'])
416-
landmark_type = getattr(FaceLandmarkTypes, response_landmark['type'])
414+
landmark_type = LandmarkTypes[response_landmark['type']]
417415
return cls(position, landmark_type)
418416

419417
@property
@@ -440,4 +438,4 @@ class Landmarks(object):
440438
def __init__(self, landmarks):
441439
for landmark_response in landmarks:
442440
landmark = Landmark.from_api_repr(landmark_response)
443-
setattr(self, landmark.landmark_type.lower(), landmark)
441+
setattr(self, landmark.landmark_type.value.lower(), landmark)

vision/google/cloud/vision/likelihood.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"""Likelihood constants returned from Vision API."""
1616

1717

18-
class Likelihood(object):
18+
from enum import Enum
19+
20+
21+
class Likelihood(Enum):
1922
"""A representation of likelihood to give stable results across upgrades.
2023
2124
See:

vision/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151

5252
REQUIREMENTS = [
53+
'enum34',
5354
'google-cloud-core >= 0.21.0, < 0.22dev',
5455
]
5556

vision/tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ localdeps =
77
pip install --quiet --upgrade {toxinidir}/../core
88
deps =
99
{toxinidir}/../core
10+
enum34
1011
mock
1112
pytest
1213
covercmd =

vision/unit_tests/test_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ def test_text_detection_from_source(self):
359359
self.assertEqual(694, text[0].bounds.vertices[0].y_coordinate)
360360

361361
def test_safe_search_detection_from_source(self):
362+
from google.cloud.vision.likelihood import Likelihood
362363
from google.cloud.vision.safe import SafeSearchAnnotation
363364
from unit_tests._fixtures import SAFE_SEARCH_DETECTION_RESPONSE
364365

@@ -373,10 +374,10 @@ def test_safe_search_detection_from_source(self):
373374
image_request = client._connection._requested[0]['data']['requests'][0]
374375
self.assertEqual(IMAGE_SOURCE,
375376
image_request['image']['source']['gcs_image_uri'])
376-
self.assertEqual('VERY_UNLIKELY', safe_search.adult)
377-
self.assertEqual('UNLIKELY', safe_search.spoof)
378-
self.assertEqual('POSSIBLE', safe_search.medical)
379-
self.assertEqual('VERY_UNLIKELY', safe_search.violence)
377+
self.assertEqual(safe_search.adult, Likelihood.VERY_UNLIKELY)
378+
self.assertEqual(safe_search.spoof, Likelihood.UNLIKELY)
379+
self.assertEqual(safe_search.medical, Likelihood.POSSIBLE)
380+
self.assertEqual(safe_search.violence, Likelihood.VERY_UNLIKELY)
380381

381382
def test_safe_search_no_results(self):
382383
RETURNED = {

vision/unit_tests/test_face.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def setUp(self):
2929
self.FACE_ANNOTATIONS['faceAnnotations'][0])
3030

3131
def test_face_landmarks(self):
32+
from google.cloud.vision.face import LandmarkTypes
33+
3234
self.assertEqual(0.54453093, self.face.landmarking_confidence)
3335
self.assertEqual(0.9863683, self.face.detection_confidence)
3436
self.assertTrue(hasattr(self.face.landmarks, 'left_eye'))
@@ -38,8 +40,8 @@ def test_face_landmarks(self):
3840
self.face.landmarks.left_eye.position.y_coordinate)
3941
self.assertEqual(0.0016593217,
4042
self.face.landmarks.left_eye.position.z_coordinate)
41-
self.assertEqual('LEFT_EYE',
42-
self.face.landmarks.left_eye.landmark_type)
43+
self.assertEqual(self.face.landmarks.left_eye.landmark_type,
44+
LandmarkTypes.LEFT_EYE)
4345

4446
def test_facial_emotions(self):
4547
from google.cloud.vision.face import Likelihood

0 commit comments

Comments
 (0)