Skip to content

Commit

Permalink
Merge pull request googleapis#2750 from daspecster/fix-vision-face-no…
Browse files Browse the repository at this point in the history
…-results

Fix vision error when no results returned.
  • Loading branch information
daspecster authored Nov 17, 2016
2 parents 445e261 + 9366fb4 commit af888fe
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
16 changes: 9 additions & 7 deletions vision/google/cloud/vision/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,22 @@ def detect_text(self, limit=10):

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

detected_objects = []
feature_key = _REVERSE_TYPES[feature_type]
annotations = results.get(feature_key, ())
if not annotations:
return []

detected_objects = []
if feature_type == _FACE_DETECTION:
detected_objects.extend(
Face.from_api_repr(face) for face in results[feature_key])
Face.from_api_repr(face) for face in annotations)
elif feature_type == _IMAGE_PROPERTIES:
detected_objects.append(
ImagePropertiesAnnotation.from_api_repr(results[feature_key]))
ImagePropertiesAnnotation.from_api_repr(annotations))
elif feature_type == _SAFE_SEARCH_DETECTION:
result = results[feature_key]
detected_objects.append(SafeSearchAnnotation.from_api_repr(result))
detected_objects.append(
SafeSearchAnnotation.from_api_repr(annotations))
else:
for result in results[feature_key]:
for result in annotations:
detected_objects.append(EntityAnnotation.from_api_repr(result))
return detected_objects
70 changes: 70 additions & 0 deletions vision/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ def test_face_detection_from_content(self):
image_request['image']['content'])
self.assertEqual(5, image_request['features'][0]['maxResults'])

def test_face_detection_from_content_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = _Credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

image = client.image(content=IMAGE_CONTENT)
faces = image.detect_faces(limit=5)
self.assertEqual(faces, [])
self.assertEqual(len(faces), 0)
image_request = client._connection._requested[0]['data']['requests'][0]

self.assertEqual(B64_IMAGE_CONTENT,
image_request['image']['content'])
self.assertEqual(5, image_request['features'][0]['maxResults'])

def test_label_detection_from_source(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests._fixtures import (
Expand All @@ -138,6 +156,19 @@ def test_label_detection_from_source(self):
self.assertEqual('/m/0k4j', labels[0].mid)
self.assertEqual('/m/07yv9', labels[1].mid)

def test_label_detection_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = _Credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

image = client.image(content=IMAGE_CONTENT)
labels = image.detect_labels()
self.assertEqual(labels, [])
self.assertEqual(len(labels), 0)

def test_landmark_detection_from_source(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests._fixtures import (
Expand Down Expand Up @@ -178,6 +209,19 @@ def test_landmark_detection_from_content(self):
image_request['image']['content'])
self.assertEqual(5, image_request['features'][0]['maxResults'])

def test_landmark_detection_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = _Credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

image = client.image(content=IMAGE_CONTENT)
landmarks = image.detect_landmarks()
self.assertEqual(landmarks, [])
self.assertEqual(len(landmarks), 0)

def test_logo_detection_from_source(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests._fixtures import LOGO_DETECTION_RESPONSE
Expand Down Expand Up @@ -254,6 +298,19 @@ def test_safe_search_detection_from_source(self):
self.assertEqual('POSSIBLE', safe_search.medical)
self.assertEqual('VERY_UNLIKELY', safe_search.violence)

def test_safe_search_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = _Credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

image = client.image(content=IMAGE_CONTENT)
safe_search = image.detect_safe_search()
self.assertEqual(safe_search, [])
self.assertEqual(len(safe_search), 0)

def test_image_properties_detection_from_source(self):
from google.cloud.vision.color import ImagePropertiesAnnotation
from unit_tests._fixtures import IMAGE_PROPERTIES_RESPONSE
Expand All @@ -277,6 +334,19 @@ def test_image_properties_detection_from_source(self):
self.assertEqual(65, image_properties.colors[0].color.blue)
self.assertEqual(0.0, image_properties.colors[0].color.alpha)

def test_image_properties_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = _Credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

image = client.image(content=IMAGE_CONTENT)
image_properties = image.detect_properties()
self.assertEqual(image_properties, [])
self.assertEqual(len(image_properties), 0)


class TestVisionRequest(unittest.TestCase):
@staticmethod
Expand Down

0 comments on commit af888fe

Please sign in to comment.