|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 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 | +"""Image properties class representation derived from Vision API response.""" |
| 16 | + |
| 17 | + |
| 18 | +class ImagePropertiesAnnotation(object): |
| 19 | + """Representation of image properties |
| 20 | +
|
| 21 | + :type colors: list |
| 22 | + :param colors: List of |
| 23 | + :class:`~google.cloud.vision.color.ColorInformation`. |
| 24 | + """ |
| 25 | + def __init__(self, colors): |
| 26 | + self._colors = colors |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def from_api_repr(cls, response): |
| 30 | + """Factory: construct ``ImagePropertiesAnnotation`` from a response. |
| 31 | +
|
| 32 | + :type response: dict |
| 33 | + :param response: Dictionary response from Vision API with image |
| 34 | + properties data. |
| 35 | +
|
| 36 | + :rtype: :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`. |
| 37 | + :returns: Populated instance of ``ImagePropertiesAnnotation``. |
| 38 | + """ |
| 39 | + colors = [ColorInformation.from_api_repr(color) for color in |
| 40 | + response['dominantColors']['colors']] |
| 41 | + return cls(colors) |
| 42 | + |
| 43 | + @property |
| 44 | + def colors(self): |
| 45 | + """Colors in an image. |
| 46 | +
|
| 47 | + :rtype: list of :class:`~google.cloud.vision.color.ColorInformation` |
| 48 | + :returns: Populated list of ``ColorInformation``. |
| 49 | + """ |
| 50 | + return self._colors |
| 51 | + |
| 52 | + |
| 53 | +class Color(object): |
| 54 | + """Representation of RGBA color information. |
| 55 | +
|
| 56 | + :type red: int |
| 57 | + :param red: The amount of red in the color as a value in the interval |
| 58 | + [0, 1]. |
| 59 | +
|
| 60 | + :type green: int |
| 61 | + :param green: The amount of green in the color as a value in the interval |
| 62 | + [0, 1]. |
| 63 | +
|
| 64 | + :type blue: int |
| 65 | + :param blue: The amount of blue in the color as a value in the interval |
| 66 | + [0, 1]. |
| 67 | +
|
| 68 | + :type alpha: float |
| 69 | + :param alpha: The fraction of this color that should be applied to the |
| 70 | + pixel. |
| 71 | + """ |
| 72 | + def __init__(self, red, green, blue, alpha): |
| 73 | + self._red = red |
| 74 | + self._green = green |
| 75 | + self._blue = blue |
| 76 | + self._alpha = alpha |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def from_api_repr(cls, response): |
| 80 | + """Factory: construct a ``Color`` from a Vision API response. |
| 81 | +
|
| 82 | + :type response: dict |
| 83 | + :param response: Color from API Response. |
| 84 | +
|
| 85 | + :rtype: :class:`~google.cloud.vision.color.Color` |
| 86 | + :returns: Instance of :class:`~google.cloud.vision.color.Color`. |
| 87 | + """ |
| 88 | + red = response['red'] |
| 89 | + green = response['green'] |
| 90 | + blue = response['blue'] |
| 91 | + alpha = response.get('alpha') |
| 92 | + |
| 93 | + return cls(red, green, blue, alpha) |
| 94 | + |
| 95 | + @property |
| 96 | + def red(self): |
| 97 | + """Red color. |
| 98 | +
|
| 99 | + :rtype: int |
| 100 | + :returns: Red RGB value. |
| 101 | + """ |
| 102 | + return self._red |
| 103 | + |
| 104 | + @property |
| 105 | + def green(self): |
| 106 | + """Green color. |
| 107 | +
|
| 108 | + :rtype: int |
| 109 | + :returns: Green RGB value. |
| 110 | + """ |
| 111 | + return self._green |
| 112 | + |
| 113 | + @property |
| 114 | + def blue(self): |
| 115 | + """Blue color. |
| 116 | +
|
| 117 | + :rtype: int |
| 118 | + :returns: Blue RGB value. |
| 119 | + """ |
| 120 | + return self._blue |
| 121 | + |
| 122 | + @property |
| 123 | + def alpha(self): |
| 124 | + """Alpha level. |
| 125 | +
|
| 126 | + :rtype: float |
| 127 | + :returns: Alpha transparency level. |
| 128 | + """ |
| 129 | + return self._alpha |
| 130 | + |
| 131 | + |
| 132 | +class ColorInformation(object): |
| 133 | + """Representation of color information from API response. |
| 134 | +
|
| 135 | + :type color: :class:`~google.cloud.vision.color.Color` |
| 136 | + :param color: RGB components of the color. |
| 137 | +
|
| 138 | + :type score: float |
| 139 | + :param score: Image-specific score for this color. Value in range [0, 1]. |
| 140 | +
|
| 141 | + :type pixel_fraction: float |
| 142 | + :param pixel_fraction: Stores the fraction of pixels the color occupies in |
| 143 | + the image. Value in range [0, 1]. |
| 144 | + """ |
| 145 | + def __init__(self, color, score, pixel_fraction): |
| 146 | + self._color = color |
| 147 | + self._score = score |
| 148 | + self._pixel_fraction = pixel_fraction |
| 149 | + |
| 150 | + @classmethod |
| 151 | + def from_api_repr(cls, response): |
| 152 | + """Factory: construct ``ColorInformation`` for a color found. |
| 153 | +
|
| 154 | + :type response: dict |
| 155 | + :param response: Color data with extra meta information. |
| 156 | +
|
| 157 | + :rtype: :class:`~google.cloud.vision.color.ColorInformation` |
| 158 | + :returns: Instance of ``ColorInformation``. |
| 159 | + """ |
| 160 | + color = Color.from_api_repr(response['color']) |
| 161 | + score = response['score'] |
| 162 | + pixel_fraction = response['pixelFraction'] |
| 163 | + |
| 164 | + return cls(color, score, pixel_fraction) |
| 165 | + |
| 166 | + @property |
| 167 | + def color(self): |
| 168 | + """RGB components of the color. |
| 169 | +
|
| 170 | + :rtype: :class:`~google.vision.color.Color` |
| 171 | + :returns: Instance of ``Color``. |
| 172 | + """ |
| 173 | + return self._color |
| 174 | + |
| 175 | + @property |
| 176 | + def score(self): |
| 177 | + """Image-specific score for this color. Value in range [0, 1]. |
| 178 | +
|
| 179 | + :rtype: float |
| 180 | + :returns: Image score for this color. |
| 181 | + """ |
| 182 | + return self._score |
| 183 | + |
| 184 | + @property |
| 185 | + def pixel_fraction(self): |
| 186 | + """Stores the fraction of pixels the color occupies in the image. |
| 187 | +
|
| 188 | + :rtype: float |
| 189 | + :returns: Pixel fraction value in range [0, 1]. |
| 190 | + """ |
| 191 | + return self._pixel_fraction |
0 commit comments