Skip to content

Commit 50d3dd1

Browse files
authored
Merge pull request googleapis#2761 from daspecster/vision-handle-empty-values
Expect vision response data to have missing keys.
2 parents 3456c8b + ddc30eb commit 50d3dd1

2 files changed

Lines changed: 57 additions & 9 deletions

File tree

vision/google/cloud/vision/color.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def from_api_repr(cls, response):
3636
:rtype: :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`.
3737
:returns: Populated instance of ``ImagePropertiesAnnotation``.
3838
"""
39-
colors = [ColorInformation.from_api_repr(color) for color in
40-
response['dominantColors']['colors']]
39+
raw_colors = response.get('dominantColors', {}).get('colors', ())
40+
colors = [ColorInformation.from_api_repr(color)
41+
for color in raw_colors]
4142
return cls(colors)
4243

4344
@property
@@ -85,10 +86,10 @@ def from_api_repr(cls, response):
8586
:rtype: :class:`~google.cloud.vision.color.Color`
8687
:returns: Instance of :class:`~google.cloud.vision.color.Color`.
8788
"""
88-
red = response['red']
89-
green = response['green']
90-
blue = response['blue']
91-
alpha = response.get('alpha')
89+
red = response.get('red', 0)
90+
green = response.get('green', 0)
91+
blue = response.get('blue', 0)
92+
alpha = response.get('alpha', 0.0)
9293

9394
return cls(red, green, blue, alpha)
9495

@@ -157,9 +158,9 @@ def from_api_repr(cls, response):
157158
:rtype: :class:`~google.cloud.vision.color.ColorInformation`
158159
:returns: Instance of ``ColorInformation``.
159160
"""
160-
color = Color.from_api_repr(response['color'])
161-
score = response['score']
162-
pixel_fraction = response['pixelFraction']
161+
color = Color.from_api_repr(response.get('color'))
162+
score = response.get('score')
163+
pixel_fraction = response.get('pixelFraction')
163164

164165
return cls(color, score, pixel_fraction)
165166

vision/unit_tests/test_color.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
18+
class TestColor(unittest.TestCase):
19+
@staticmethod
20+
def _get_target_class():
21+
from google.cloud.vision.color import Color
22+
return Color
23+
24+
def test_rgb_color_data(self):
25+
colors = {
26+
'red': 255,
27+
'green': 255,
28+
'blue': 255,
29+
'alpha': 0.5,
30+
}
31+
color_class = self._get_target_class()
32+
colors = color_class.from_api_repr(colors)
33+
34+
self.assertEqual(colors.red, 255)
35+
self.assertEqual(colors.green, 255)
36+
self.assertEqual(colors.blue, 255)
37+
self.assertEqual(colors.alpha, 0.5)
38+
39+
def test_missing_rgb_values(self):
40+
colors = {}
41+
color_class = self._get_target_class()
42+
colors = color_class.from_api_repr(colors)
43+
44+
self.assertEqual(colors.red, 0)
45+
self.assertEqual(colors.green, 0)
46+
self.assertEqual(colors.blue, 0)
47+
self.assertEqual(colors.alpha, 0.0)

0 commit comments

Comments
 (0)