Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions vision/google/cloud/vision/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
from google.cloud.vision.safe import SafeSearchAnnotation


FACE_ANNOTATIONS = 'faceAnnotations'
IMAGE_PROPERTIES_ANNOTATION = 'imagePropertiesAnnotation'
SAFE_SEARCH_ANNOTATION = 'safeSearchAnnotation'

This comment was marked as spam.

This comment was marked as spam.


_KEY_MAP = {
FACE_ANNOTATIONS: 'faces',
IMAGE_PROPERTIES_ANNOTATION: 'properties',
'labelAnnotations': 'labels',
'landmarkAnnotations': 'landmarks',
'logoAnnotations': 'logos',
SAFE_SEARCH_ANNOTATION: 'safe_searches',
'textAnnotations': 'texts'
}


class Annotations(object):
"""Helper class to bundle annotation responses.

Expand Down Expand Up @@ -51,15 +66,15 @@ class Annotations(object):
:param texts: List of
:class:`~google.cloud.vision.entity.EntityAnnotation`.
"""
def __init__(self, faces=None, properties=None, labels=None,
landmarks=None, logos=None, safe_searches=None, texts=None):
self.faces = faces or ()
self.properties = properties or ()
self.labels = labels or ()
self.landmarks = landmarks or ()
self.logos = logos or ()
self.safe_searches = safe_searches or ()
self.texts = texts or ()
def __init__(self, faces=(), properties=(), labels=(), landmarks=(),
logos=(), safe_searches=(), texts=()):
self.faces = faces
self.properties = properties
self.labels = labels
self.landmarks = landmarks
self.logos = logos
self.safe_searches = safe_searches
self.texts = texts

@classmethod
def from_api_repr(cls, response):
Expand All @@ -72,18 +87,8 @@ def from_api_repr(cls, response):
:returns: An instance of ``Annotations`` with detection types loaded.
"""
annotations = {}
key_map = {
'faceAnnotations': 'faces',
'imagePropertiesAnnotation': 'properties',
'labelAnnotations': 'labels',
'landmarkAnnotations': 'landmarks',
'logoAnnotations': 'logos',
'safeSearchAnnotation': 'safe_searches',
'textAnnotations': 'texts'
}

for feature_type, annotation in response.items():
curr_feature = annotations.setdefault(key_map[feature_type], [])
curr_feature = annotations.setdefault(_KEY_MAP[feature_type], [])
curr_feature.extend(
_entity_from_response_type(feature_type, annotation))

This comment was marked as spam.

return cls(**annotations)
Expand All @@ -100,13 +105,13 @@ def _entity_from_response_type(feature_type, results):
:class:`~google.cloud.vision.safe.SafeSearchAnnotation`.
"""
detected_objects = []
if feature_type == 'faceAnnotations':
if feature_type == FACE_ANNOTATIONS:
detected_objects.extend(
Face.from_api_repr(face) for face in results)
elif feature_type == 'imagePropertiesAnnotation':
elif feature_type == IMAGE_PROPERTIES_ANNOTATION:
detected_objects.append(
ImagePropertiesAnnotation.from_api_repr(results))
elif feature_type == 'safeSearchAnnotation':
elif feature_type == SAFE_SEARCH_ANNOTATION:
detected_objects.append(SafeSearchAnnotation.from_api_repr(results))
else:
for result in results:
Expand Down