Skip to content

Commit 7aa42be

Browse files
committed
Fix vision error when no results returned.
1 parent b92f0cf commit 7aa42be

2 files changed

Lines changed: 78 additions & 7 deletions

File tree

vision/google/cloud/vision/image.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,18 @@ def _entity_from_response_type(feature_type, results):
220220

221221
detected_objects = []
222222
feature_key = _REVERSE_TYPES[feature_type]
223+
annotations = results.get(feature_key, [])
223224

224225
if feature_type == _FACE_DETECTION:
225226
detected_objects.extend(
226-
Face.from_api_repr(face) for face in results[feature_key])
227-
elif feature_type == _IMAGE_PROPERTIES:
227+
Face.from_api_repr(face) for face in annotations)
228+
elif feature_type == _IMAGE_PROPERTIES and annotations:
228229
detected_objects.append(
229-
ImagePropertiesAnnotation.from_api_repr(results[feature_key]))
230-
elif feature_type == _SAFE_SEARCH_DETECTION:
231-
result = results[feature_key]
232-
detected_objects.append(SafeSearchAnnotation.from_api_repr(result))
230+
ImagePropertiesAnnotation.from_api_repr(annotations))
231+
elif feature_type == _SAFE_SEARCH_DETECTION and annotations:
232+
detected_objects.append(
233+
SafeSearchAnnotation.from_api_repr(annotations))
233234
else:
234-
for result in results[feature_key]:
235+
for result in annotations:
235236
detected_objects.append(EntityAnnotation.from_api_repr(result))
236237
return detected_objects

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)