|
| 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 | +"""Safe search class for information returned from annotating an image.""" |
| 16 | + |
| 17 | + |
| 18 | +from google.cloud.vision.likelihood import Likelihood |
| 19 | + |
| 20 | + |
| 21 | +class SafeSearchAnnotation(object): |
| 22 | + """Representation of a SafeSearchAnnotation. |
| 23 | +
|
| 24 | + :type adult_likelihood: :class:`~google.cloud.vision.likelihood.Likelihood` |
| 25 | + :param adult_likelihood: Likelihood that image contains adult material. |
| 26 | +
|
| 27 | + :type spoof_likelihood: :class:`~google.cloud.vision.likelihood.Likelihood` |
| 28 | + :param spoof_likelihood: Likelihood that image is a spoof. |
| 29 | +
|
| 30 | + :type medical_likelihood: |
| 31 | + :class:`~google.cloud.vision.likelihood.Likelihood` |
| 32 | + :param medical_likelihood: Likelihood that image contains medical material. |
| 33 | +
|
| 34 | + :type violence_likelihood: |
| 35 | + :class:`~google.cloud.vision.likelihood.Likelihood` |
| 36 | + :param violence_likelihood: Likelihood that image contains violence. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, adult_likelihood, spoof_likelihood, medical_likelihood, |
| 40 | + violence_likelihood): |
| 41 | + self._adult_likelihood = adult_likelihood |
| 42 | + self._spoof_likelihood = spoof_likelihood |
| 43 | + self._medical_likeliehood = medical_likelihood |
| 44 | + self._violence_likelihood = violence_likelihood |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def from_api_repr(cls, response): |
| 48 | + """Factory: construct SafeSearchAnnotation from Vision API response. |
| 49 | +
|
| 50 | + :type response: dict |
| 51 | + :param response: Dictionary response from Vision API with safe search |
| 52 | + data. |
| 53 | +
|
| 54 | + :rtype: :class:`~google.cloud.vision.safe.SafeSearchAnnotation` |
| 55 | + :returns: Instance of ``SafeSearchAnnotation``. |
| 56 | + """ |
| 57 | + adult_likelihood = getattr(Likelihood, response['adult']) |
| 58 | + spoof_likelihood = getattr(Likelihood, response['spoof']) |
| 59 | + medical_likelihood = getattr(Likelihood, response['medical']) |
| 60 | + violence_likelihood = getattr(Likelihood, response['violence']) |
| 61 | + |
| 62 | + return cls(adult_likelihood, spoof_likelihood, medical_likelihood, |
| 63 | + violence_likelihood) |
| 64 | + |
| 65 | + @property |
| 66 | + def adult(self): |
| 67 | + """Represents the adult contents likelihood for the image. |
| 68 | +
|
| 69 | + :rtype: :class:`~google.cloud.vision.likelihood.Likelihood` |
| 70 | + :returns: ``Likelihood`` of the image containing adult content. |
| 71 | + """ |
| 72 | + return self._adult_likelihood |
| 73 | + |
| 74 | + @property |
| 75 | + def spoof(self): |
| 76 | + """The likelihood that an obvious modification was made to the image. |
| 77 | +
|
| 78 | + :rtype: :class:`~google.cloud.vision.likelihood.Likelihood` |
| 79 | + :returns: The ``Likelihood`` that an obvious modification was made to |
| 80 | + the image's canonical version to make it appear funny or |
| 81 | + offensive. |
| 82 | + """ |
| 83 | + return self._spoof_likelihood |
| 84 | + |
| 85 | + @property |
| 86 | + def medical(self): |
| 87 | + """Likelihood this is a medical image. |
| 88 | +
|
| 89 | + :rtype: :class:`~google.cloud.vision.likelihood.Likelihood` |
| 90 | + :returns: The ``Likelihood`` that the image is medical in origin. |
| 91 | + """ |
| 92 | + return self._medical_likeliehood |
| 93 | + |
| 94 | + @property |
| 95 | + def violence(self): |
| 96 | + """Likeliehood that this image contains violence. |
| 97 | +
|
| 98 | + :rtype: :class:`~google.cloud.vision.likelihood.Likelihood` |
| 99 | + :returns: The ``Likelihood`` that the image contains violence. |
| 100 | + """ |
| 101 | + return self._violence_likelihood |
0 commit comments