|
| 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 | +"""Entity class for holding information returned from annotating an image.""" |
| 16 | + |
| 17 | + |
| 18 | +from google.cloud.vision.geometry import Bounds |
| 19 | + |
| 20 | + |
| 21 | +class EntityAnnotation(object): |
| 22 | + """Representation of an entity returned from the Vision API. |
| 23 | +
|
| 24 | + :type bounds: dict |
| 25 | + :param bounds: Dictionary of bounary information of detected entity. |
| 26 | +
|
| 27 | + :type description: str |
| 28 | + :param description: Description of entity detected in an image. |
| 29 | +
|
| 30 | + :type mid: str |
| 31 | + :param mid: Opaque entity ID. |
| 32 | +
|
| 33 | + :type score: float |
| 34 | + :param score: Overall score of the result. Range [0, 1]. |
| 35 | + """ |
| 36 | + def __init__(self, bounds, description, mid, score): |
| 37 | + self._bounds = bounds |
| 38 | + self._description = description |
| 39 | + self._mid = mid |
| 40 | + self._score = score |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def from_api_repr(cls, response): |
| 44 | + """Factory: construct entity from Vision API response. |
| 45 | +
|
| 46 | + :type response: dict |
| 47 | + :param response: Dictionary response from Vision API with entity data. |
| 48 | +
|
| 49 | + :rtype: :class:`~google.cloud.vision.entiy.EntityAnnotation` |
| 50 | + :returns: Instance of ``EntityAnnotation``. |
| 51 | + """ |
| 52 | + bounds = Bounds.from_api_repr(response['boundingPoly']) |
| 53 | + description = response['description'] |
| 54 | + mid = response['mid'] |
| 55 | + score = response['score'] |
| 56 | + |
| 57 | + return cls(bounds, description, mid, score) |
| 58 | + |
| 59 | + @property |
| 60 | + def bounds(self): |
| 61 | + """Bounding polygon of detected image feature. |
| 62 | +
|
| 63 | + :rtype: :class:`~google.cloud.vision.geometry.Bounds` |
| 64 | + :returns: Instance of ``Bounds`` with populated vertices. |
| 65 | + """ |
| 66 | + return self._bounds |
| 67 | + |
| 68 | + @property |
| 69 | + def description(self): |
| 70 | + """Description of feature detected in image. |
| 71 | +
|
| 72 | + :rtype: str |
| 73 | + :returns: String description of feature detected in image. |
| 74 | + """ |
| 75 | + return self._description |
| 76 | + |
| 77 | + @property |
| 78 | + def mid(self): |
| 79 | + """MID of feature detected in image. |
| 80 | +
|
| 81 | + :rtype: str |
| 82 | + :returns: String MID of feature detected in image. |
| 83 | + """ |
| 84 | + return self._mid |
| 85 | + |
| 86 | + @property |
| 87 | + def score(self): |
| 88 | + """Overall score of the result. Range [0, 1]. |
| 89 | +
|
| 90 | + :rtype: float |
| 91 | + :returns: Overall score of the result. Range [0, 1]. |
| 92 | + """ |
| 93 | + return self._score |
0 commit comments