@@ -26,20 +26,37 @@ def __init__(self, colors):
26
26
self ._colors = colors
27
27
28
28
@classmethod
29
- def from_api_repr (cls , response ):
29
+ def from_api_repr (cls , image_properties ):
30
30
"""Factory: construct ``ImagePropertiesAnnotation`` from a response.
31
31
32
- :type response: dict
33
- :param response: Dictionary response from Vision API with image
34
- properties data.
32
+ :type image_properties: dict
33
+ :param image_properties: Dictionary response from Vision API with image
34
+ properties data.
35
+
36
+ :rtype: list of
37
+ :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`.
38
+ :returns: List of ``ImagePropertiesAnnotation``.
39
+ """
40
+ colors = image_properties .get ('dominantColors' , {}).get ('colors' , ())
41
+ return cls ([ColorInformation .from_api_repr (color )
42
+ for color in colors ])
43
+
44
+ @classmethod
45
+ def from_pb (cls , image_properties ):
46
+ """Factory: construct ``ImagePropertiesAnnotation`` from a response.
47
+
48
+ :type image_properties: :class:`~google.cloud.grpc.vision.v1.\
49
+ image_annotator_pb2.ImageProperties`
50
+ :param image_properties: Protobuf response from Vision API with image
51
+ properties data.
35
52
36
- :rtype: :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`.
37
- :returns: Populated instance of ``ImagePropertiesAnnotation``.
53
+ :rtype: list of
54
+ :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`
55
+ :returns: List of ``ImagePropertiesAnnotation``.
38
56
"""
39
- raw_colors = response .get ('dominantColors' , {}).get ('colors' , ())
40
- colors = [ColorInformation .from_api_repr (color )
41
- for color in raw_colors ]
42
- return cls (colors )
57
+ colors = getattr (image_properties .dominant_colors , 'colors' , ())
58
+ if len (colors ) > 0 :
59
+ return cls ([ColorInformation .from_pb (color ) for color in colors ])
43
60
44
61
@property
45
62
def colors (self ):
@@ -54,17 +71,17 @@ def colors(self):
54
71
class Color (object ):
55
72
"""Representation of RGBA color information.
56
73
57
- :type red: int
74
+ :type red: float
58
75
:param red: The amount of red in the color as a value in the interval
59
- [0, 255].
76
+ [0.0 , 255.0 ].
60
77
61
- :type green: int
78
+ :type green: float
62
79
:param green: The amount of green in the color as a value in the interval
63
- [0, 255].
80
+ [0.0 , 255.0 ].
64
81
65
- :type blue: int
82
+ :type blue: float
66
83
:param blue: The amount of blue in the color as a value in the interval
67
- [0, 255].
84
+ [0.0 , 255.0 ].
68
85
69
86
:type alpha: float
70
87
:param alpha: The fraction of this color that should be applied to the
@@ -86,13 +103,25 @@ def from_api_repr(cls, response):
86
103
:rtype: :class:`~google.cloud.vision.color.Color`
87
104
:returns: Instance of :class:`~google.cloud.vision.color.Color`.
88
105
"""
89
- red = response .get ('red' , 0 )
90
- green = response .get ('green' , 0 )
91
- blue = response .get ('blue' , 0 )
106
+ red = float ( response .get ('red' , 0.0 ) )
107
+ green = float ( response .get ('green' , 0.0 ) )
108
+ blue = float ( response .get ('blue' , 0.0 ) )
92
109
alpha = response .get ('alpha' , 0.0 )
93
110
94
111
return cls (red , green , blue , alpha )
95
112
113
+ @classmethod
114
+ def from_pb (cls , color ):
115
+ """Factory: construct a ``Color`` from a protobuf response.
116
+
117
+ :type color: :module: `google.type.color_pb2`
118
+ :param color: ``Color`` from API Response.
119
+
120
+ :rtype: :class:`~google.cloud.vision.color.Color`
121
+ :returns: Instance of :class:`~google.cloud.vision.color.Color`.
122
+ """
123
+ return cls (color .red , color .green , color .blue , color .alpha .value )
124
+
96
125
@property
97
126
def red (self ):
98
127
"""Red component of the color.
@@ -149,19 +178,34 @@ def __init__(self, color, score, pixel_fraction):
149
178
self ._pixel_fraction = pixel_fraction
150
179
151
180
@classmethod
152
- def from_api_repr (cls , response ):
153
- """Factory: construct ``ColorInformation`` for a color found .
181
+ def from_api_repr (cls , color_information ):
182
+ """Factory: construct ``ColorInformation`` for a color.
154
183
155
- :type response : dict
156
- :param response : Color data with extra meta information.
184
+ :type color_information : dict
185
+ :param color_information : Color data with extra meta information.
157
186
158
187
:rtype: :class:`~google.cloud.vision.color.ColorInformation`
159
188
:returns: Instance of ``ColorInformation``.
160
189
"""
161
- color = Color .from_api_repr (response .get ('color' ))
162
- score = response .get ('score' )
163
- pixel_fraction = response .get ('pixelFraction' )
190
+ color = Color .from_api_repr (color_information .get ('color' , {}))
191
+ score = color_information .get ('score' )
192
+ pixel_fraction = color_information .get ('pixelFraction' )
193
+ return cls (color , score , pixel_fraction )
164
194
195
+ @classmethod
196
+ def from_pb (cls , color_information ):
197
+ """Factory: construct ``ColorInformation`` for a color.
198
+
199
+ :type color_information: :class:`~google.cloud.grpc.vision.v1.\
200
+ image_annotator_pb2.ColorInfo`
201
+ :param color_information: Color data with extra meta information.
202
+
203
+ :rtype: :class:`~google.cloud.vision.color.ColorInformation`
204
+ :returns: Instance of ``ColorInformation``.
205
+ """
206
+ color = Color .from_pb (color_information .color )
207
+ score = color_information .score
208
+ pixel_fraction = color_information .pixel_fraction
165
209
return cls (color , score , pixel_fraction )
166
210
167
211
@property
0 commit comments