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