1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ # pylint: disable=too-many-arguments
1516"""Annotations management for Vision API responses."""
1617
1718import six
1819
1920from google .cloud .vision .color import ImagePropertiesAnnotation
21+ from google .cloud .vision .crop_hint import CropHint
2022from google .cloud .vision .entity import EntityAnnotation
2123from google .cloud .vision .face import Face
2224from google .cloud .vision .safe_search import SafeSearchAnnotation
25+ from google .cloud .vision .text import TextAnnotation
26+ from google .cloud .vision .web import WebDetection
2327
2428
29+ _CROP_HINTS_ANNOTATION = 'cropHintsAnnotation'
2530_FACE_ANNOTATIONS = 'faceAnnotations'
31+ _FULL_TEXT_ANNOTATION = 'fullTextAnnotation'
2632_IMAGE_PROPERTIES_ANNOTATION = 'imagePropertiesAnnotation'
2733_SAFE_SEARCH_ANNOTATION = 'safeSearchAnnotation'
34+ _WEB_ANNOTATION = 'webDetection'
2835
2936_KEY_MAP = {
37+ _CROP_HINTS_ANNOTATION : 'crop_hints' ,
3038 _FACE_ANNOTATIONS : 'faces' ,
39+ _FULL_TEXT_ANNOTATION : 'full_texts' ,
3140 _IMAGE_PROPERTIES_ANNOTATION : 'properties' ,
3241 'labelAnnotations' : 'labels' ,
3342 'landmarkAnnotations' : 'landmarks' ,
3443 'logoAnnotations' : 'logos' ,
3544 _SAFE_SEARCH_ANNOTATION : 'safe_searches' ,
36- 'textAnnotations' : 'texts'
45+ 'textAnnotations' : 'texts' ,
46+ _WEB_ANNOTATION : 'web' ,
3747}
3848
3949
4050class Annotations (object ):
4151 """Helper class to bundle annotation responses.
4252
53+ :type crop_hints: list
54+ :param crop_hints: List of
55+ :class:`~google.cloud.vision.crop_hint.CropHintsAnnotation`.
56+
4357 :type faces: list
4458 :param faces: List of :class:`~google.cloud.vision.face.Face`.
4559
60+ :type full_texts: list
61+ :param full_texts: List of
62+ :class:`~google.cloud.vision.text.TextAnnotation`.
63+
4664 :type properties: list
4765 :param properties:
4866 List of :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`.
@@ -66,16 +84,23 @@ class Annotations(object):
6684 :type texts: list
6785 :param texts: List of
6886 :class:`~google.cloud.vision.entity.EntityAnnotation`.
87+
88+ :type web: list
89+ :param web: List of :class:`~google.cloud.vision.web.WebDetection`.
6990 """
70- def __init__ (self , faces = (), properties = (), labels = (), landmarks = (),
71- logos = (), safe_searches = (), texts = ()):
91+ def __init__ (self , crop_hints = (), faces = (), full_texts = (), properties = (),
92+ labels = (), landmarks = (), logos = (), safe_searches = (),
93+ texts = (), web = ()):
94+ self .crop_hints = crop_hints
7295 self .faces = faces
96+ self .full_texts = full_texts
7397 self .properties = properties
7498 self .labels = labels
7599 self .landmarks = landmarks
76100 self .logos = logos
77101 self .safe_searches = safe_searches
78102 self .texts = texts
103+ self .web = web
79104
80105 @classmethod
81106 def from_api_repr (cls , response ):
@@ -121,7 +146,9 @@ def _process_image_annotations(image):
121146 :returns: Dictionary populated with entities from response.
122147 """
123148 return {
149+ 'crop_hints' : _make_crop_hints_from_pb (image .crop_hints_annotation ),
124150 'faces' : _make_faces_from_pb (image .face_annotations ),
151+ 'full_texts' : _make_full_text_from_pb (image .full_text_annotation ),
125152 'labels' : _make_entity_from_pb (image .label_annotations ),
126153 'landmarks' : _make_entity_from_pb (image .landmark_annotations ),
127154 'logos' : _make_entity_from_pb (image .logo_annotations ),
@@ -130,9 +157,24 @@ def _process_image_annotations(image):
130157 'safe_searches' : _make_safe_search_from_pb (
131158 image .safe_search_annotation ),
132159 'texts' : _make_entity_from_pb (image .text_annotations ),
160+ 'web' : _make_web_detection_from_pb (image .web_detection )
133161 }
134162
135163
164+ def _make_crop_hints_from_pb (crop_hints ):
165+ """Create list of ``CropHint`` objects from a protobuf response.
166+
167+ :type crop_hints: list
168+ :param crop_hints: List of
169+ :class:`google.cloud.grpc.vision.v1.\
170+ image_annotator_pb2.CropHintsAnnotation`
171+
172+ :rtype: list
173+ :returns: List of ``CropHint`` objects.
174+ """
175+ return [CropHint .from_pb (hint ) for hint in crop_hints .crop_hints ]
176+
177+
136178def _make_entity_from_pb (annotations ):
137179 """Create an entity from a protobuf response.
138180
@@ -159,6 +201,19 @@ def _make_faces_from_pb(faces):
159201 return [Face .from_pb (face ) for face in faces ]
160202
161203
204+ def _make_full_text_from_pb (full_text ):
205+ """Create text annotation object from protobuf response.
206+
207+ :type full_text: :class:`~google.cloud.proto.vision.v1.\
208+ text_annotation_pb2.TextAnnotation`
209+ :param full_text: Protobuf instance of ``TextAnnotation``.
210+
211+ :rtype: :class:`~google.cloud.vision.text.TextAnnotation`
212+ :returns: Instance of ``TextAnnotation``.
213+ """
214+ return TextAnnotation .from_pb (full_text )
215+
216+
162217def _make_image_properties_from_pb (image_properties ):
163218 """Create ``ImageProperties`` object from a protobuf response.
164219
@@ -186,6 +241,19 @@ def _make_safe_search_from_pb(safe_search):
186241 return SafeSearchAnnotation .from_pb (safe_search )
187242
188243
244+ def _make_web_detection_from_pb (annotation ):
245+ """Create ``WebDetection`` object from a protobuf response.
246+
247+ :type annotation: :class:`~google.cloud.proto.vision.v1.web_detection_pb2\
248+ .WebDetection`
249+ :param annotation: Protobuf instance of ``WebDetection``.
250+
251+ :rtype: :class: `~google.cloud.vision.web.WebDetection`
252+ :returns: Instance of ``WebDetection``.
253+ """
254+ return WebDetection .from_pb (annotation )
255+
256+
189257def _entity_from_response_type (feature_type , results ):
190258 """Convert a JSON result to an entity type based on the feature.
191259
@@ -207,6 +275,14 @@ def _entity_from_response_type(feature_type, results):
207275 return ImagePropertiesAnnotation .from_api_repr (results )
208276 elif feature_type == _SAFE_SEARCH_ANNOTATION :
209277 return SafeSearchAnnotation .from_api_repr (results )
278+ elif feature_type == _WEB_ANNOTATION :
279+ return WebDetection .from_api_repr (results )
280+ elif feature_type == _CROP_HINTS_ANNOTATION :
281+ crop_hints = results .get ('cropHints' , [])
282+ detected_objects .extend (
283+ CropHint .from_api_repr (result ) for result in crop_hints )
284+ elif feature_type == _FULL_TEXT_ANNOTATION :
285+ return TextAnnotation .from_api_repr (results )
210286 else :
211287 for result in results :
212288 detected_objects .append (EntityAnnotation .from_api_repr (result ))
0 commit comments