Skip to content

Commit 890b77c

Browse files
authored
Merge pull request #2750 from daspecster/fix-vision-face-no-results
Fix vision error when no results returned.
2 parents 231b9ad + c234648 commit 890b77c

2 files changed

Lines changed: 79 additions & 7 deletions

File tree

packages/google-cloud-vision/google/cloud/vision/image.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,22 @@ def detect_text(self, limit=10):
217217

218218
def _entity_from_response_type(feature_type, results):
219219
"""Convert a JSON result to an entity type based on the feature."""
220-
221-
detected_objects = []
222220
feature_key = _REVERSE_TYPES[feature_type]
221+
annotations = results.get(feature_key, ())
222+
if not annotations:
223+
return []
223224

225+
detected_objects = []
224226
if feature_type == _FACE_DETECTION:
225227
detected_objects.extend(
226-
Face.from_api_repr(face) for face in results[feature_key])
228+
Face.from_api_repr(face) for face in annotations)
227229
elif feature_type == _IMAGE_PROPERTIES:
228230
detected_objects.append(
229-
ImagePropertiesAnnotation.from_api_repr(results[feature_key]))
231+
ImagePropertiesAnnotation.from_api_repr(annotations))
230232
elif feature_type == _SAFE_SEARCH_DETECTION:
231-
result = results[feature_key]
232-
detected_objects.append(SafeSearchAnnotation.from_api_repr(result))
233+
detected_objects.append(
234+
SafeSearchAnnotation.from_api_repr(annotations))
233235
else:
234-
for result in results[feature_key]:
236+
for result in annotations:
235237
detected_objects.append(EntityAnnotation.from_api_repr(result))
236238
return detected_objects

packages/google-cloud-vision/unit_tests/test_client.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ def test_face_detection_from_content(self):
116116
image_request['image']['content'])
117117
self.assertEqual(5, image_request['features'][0]['maxResults'])
118118

119+
def test_face_detection_from_content_no_results(self):
120+
RETURNED = {
121+
'responses': [{}]
122+
}
123+
credentials = _Credentials()
124+
client = self._make_one(project=PROJECT, credentials=credentials)
125+
client._connection = _Connection(RETURNED)
126+
127+
image = client.image(content=IMAGE_CONTENT)
128+
faces = image.detect_faces(limit=5)
129+
self.assertEqual(faces, [])
130+
self.assertEqual(len(faces), 0)
131+
image_request = client._connection._requested[0]['data']['requests'][0]
132+
133+
self.assertEqual(B64_IMAGE_CONTENT,
134+
image_request['image']['content'])
135+
self.assertEqual(5, image_request['features'][0]['maxResults'])
136+
119137
def test_label_detection_from_source(self):
120138
from google.cloud.vision.entity import EntityAnnotation
121139
from unit_tests._fixtures import (
@@ -138,6 +156,19 @@ def test_label_detection_from_source(self):
138156
self.assertEqual('/m/0k4j', labels[0].mid)
139157
self.assertEqual('/m/07yv9', labels[1].mid)
140158

159+
def test_label_detection_no_results(self):
160+
RETURNED = {
161+
'responses': [{}]
162+
}
163+
credentials = _Credentials()
164+
client = self._make_one(project=PROJECT, credentials=credentials)
165+
client._connection = _Connection(RETURNED)
166+
167+
image = client.image(content=IMAGE_CONTENT)
168+
labels = image.detect_labels()
169+
self.assertEqual(labels, [])
170+
self.assertEqual(len(labels), 0)
171+
141172
def test_landmark_detection_from_source(self):
142173
from google.cloud.vision.entity import EntityAnnotation
143174
from unit_tests._fixtures import (
@@ -178,6 +209,19 @@ def test_landmark_detection_from_content(self):
178209
image_request['image']['content'])
179210
self.assertEqual(5, image_request['features'][0]['maxResults'])
180211

212+
def test_landmark_detection_no_results(self):
213+
RETURNED = {
214+
'responses': [{}]
215+
}
216+
credentials = _Credentials()
217+
client = self._make_one(project=PROJECT, credentials=credentials)
218+
client._connection = _Connection(RETURNED)
219+
220+
image = client.image(content=IMAGE_CONTENT)
221+
landmarks = image.detect_landmarks()
222+
self.assertEqual(landmarks, [])
223+
self.assertEqual(len(landmarks), 0)
224+
181225
def test_logo_detection_from_source(self):
182226
from google.cloud.vision.entity import EntityAnnotation
183227
from unit_tests._fixtures import LOGO_DETECTION_RESPONSE
@@ -254,6 +298,19 @@ def test_safe_search_detection_from_source(self):
254298
self.assertEqual('POSSIBLE', safe_search.medical)
255299
self.assertEqual('VERY_UNLIKELY', safe_search.violence)
256300

301+
def test_safe_search_no_results(self):
302+
RETURNED = {
303+
'responses': [{}]
304+
}
305+
credentials = _Credentials()
306+
client = self._make_one(project=PROJECT, credentials=credentials)
307+
client._connection = _Connection(RETURNED)
308+
309+
image = client.image(content=IMAGE_CONTENT)
310+
safe_search = image.detect_safe_search()
311+
self.assertEqual(safe_search, [])
312+
self.assertEqual(len(safe_search), 0)
313+
257314
def test_image_properties_detection_from_source(self):
258315
from google.cloud.vision.color import ImagePropertiesAnnotation
259316
from unit_tests._fixtures import IMAGE_PROPERTIES_RESPONSE
@@ -277,6 +334,19 @@ def test_image_properties_detection_from_source(self):
277334
self.assertEqual(65, image_properties.colors[0].color.blue)
278335
self.assertEqual(0.0, image_properties.colors[0].color.alpha)
279336

337+
def test_image_properties_no_results(self):
338+
RETURNED = {
339+
'responses': [{}]
340+
}
341+
credentials = _Credentials()
342+
client = self._make_one(project=PROJECT, credentials=credentials)
343+
client._connection = _Connection(RETURNED)
344+
345+
image = client.image(content=IMAGE_CONTENT)
346+
image_properties = image.detect_properties()
347+
self.assertEqual(image_properties, [])
348+
self.assertEqual(len(image_properties), 0)
349+
280350

281351
class TestVisionRequest(unittest.TestCase):
282352
@staticmethod

0 commit comments

Comments
 (0)