1919
2020from google .cloud ._helpers import _to_bytes
2121from google .cloud ._helpers import _bytes_to_unicode
22- from google .cloud .vision .entity import EntityAnnotation
23- from google .cloud .vision .face import Face
22+ from google .cloud .vision .annotations import Annotations
2423from google .cloud .vision .feature import Feature
2524from google .cloud .vision .feature import FeatureTypes
26- from google .cloud .vision .color import ImagePropertiesAnnotation
27- from google .cloud .vision .safe import SafeSearchAnnotation
28-
29-
30- _FACE_DETECTION = 'FACE_DETECTION'
31- _IMAGE_PROPERTIES = 'IMAGE_PROPERTIES'
32- _LABEL_DETECTION = 'LABEL_DETECTION'
33- _LANDMARK_DETECTION = 'LANDMARK_DETECTION'
34- _LOGO_DETECTION = 'LOGO_DETECTION'
35- _SAFE_SEARCH_DETECTION = 'SAFE_SEARCH_DETECTION'
36- _TEXT_DETECTION = 'TEXT_DETECTION'
37-
38- _REVERSE_TYPES = {
39- _FACE_DETECTION : 'faceAnnotations' ,
40- _IMAGE_PROPERTIES : 'imagePropertiesAnnotation' ,
41- _LABEL_DETECTION : 'labelAnnotations' ,
42- _LANDMARK_DETECTION : 'landmarkAnnotations' ,
43- _LOGO_DETECTION : 'logoAnnotations' ,
44- _SAFE_SEARCH_DETECTION : 'safeSearchAnnotation' ,
45- _TEXT_DETECTION : 'textAnnotations' ,
46- }
4725
4826
4927class Image (object ):
@@ -105,7 +83,7 @@ def source(self):
10583 return self ._source
10684
10785 def _detect_annotation (self , features ):
108- """Generic method for detecting a single annotation .
86+ """Generic method for detecting annotations .
10987
11088 :type features: list
11189 :param features: List of :class:`~google.cloud.vision.feature.Feature`
@@ -118,12 +96,21 @@ def _detect_annotation(self, features):
11896 :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`,
11997 :class:`~google.cloud.vision.sage.SafeSearchAnnotation`,
12098 """
121- detected_objects = []
12299 results = self .client .annotate (self , features )
123- for feature in features :
124- detected_objects .extend (
125- _entity_from_response_type (feature .feature_type , results ))
126- return detected_objects
100+ return Annotations .from_api_repr (results )
101+
102+ def detect (self , features ):
103+ """Detect multiple feature types.
104+
105+ :type features: list of :class:`~google.cloud.vision.feature.Feature`
106+ :param features: List of the ``Feature`` indication the type of
107+ annotation to perform.
108+
109+ :rtype: list
110+ :returns: List of
111+ :class:`~google.cloud.vision.entity.EntityAnnotation`.
112+ """
113+ return self ._detect_annotation (features )
127114
128115 def detect_faces (self , limit = 10 ):
129116 """Detect faces in image.
@@ -135,7 +122,8 @@ def detect_faces(self, limit=10):
135122 :returns: List of :class:`~google.cloud.vision.face.Face`.
136123 """
137124 features = [Feature (FeatureTypes .FACE_DETECTION , limit )]
138- return self ._detect_annotation (features )
125+ annotations = self ._detect_annotation (features )
126+ return annotations .faces
139127
140128 def detect_labels (self , limit = 10 ):
141129 """Detect labels that describe objects in an image.
@@ -147,7 +135,8 @@ def detect_labels(self, limit=10):
147135 :returns: List of :class:`~google.cloud.vision.entity.EntityAnnotation`
148136 """
149137 features = [Feature (FeatureTypes .LABEL_DETECTION , limit )]
150- return self ._detect_annotation (features )
138+ annotations = self ._detect_annotation (features )
139+ return annotations .labels
151140
152141 def detect_landmarks (self , limit = 10 ):
153142 """Detect landmarks in an image.
@@ -160,7 +149,8 @@ def detect_landmarks(self, limit=10):
160149 :class:`~google.cloud.vision.entity.EntityAnnotation`.
161150 """
162151 features = [Feature (FeatureTypes .LANDMARK_DETECTION , limit )]
163- return self ._detect_annotation (features )
152+ annotations = self ._detect_annotation (features )
153+ return annotations .landmarks
164154
165155 def detect_logos (self , limit = 10 ):
166156 """Detect logos in an image.
@@ -173,7 +163,8 @@ def detect_logos(self, limit=10):
173163 :class:`~google.cloud.vision.entity.EntityAnnotation`.
174164 """
175165 features = [Feature (FeatureTypes .LOGO_DETECTION , limit )]
176- return self ._detect_annotation (features )
166+ annotations = self ._detect_annotation (features )
167+ return annotations .logos
177168
178169 def detect_properties (self , limit = 10 ):
179170 """Detect the color properties of an image.
@@ -186,7 +177,8 @@ def detect_properties(self, limit=10):
186177 :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`.
187178 """
188179 features = [Feature (FeatureTypes .IMAGE_PROPERTIES , limit )]
189- return self ._detect_annotation (features )
180+ annotations = self ._detect_annotation (features )
181+ return annotations .properties
190182
191183 def detect_safe_search (self , limit = 10 ):
192184 """Retreive safe search properties from an image.
@@ -199,7 +191,8 @@ def detect_safe_search(self, limit=10):
199191 :class:`~google.cloud.vision.sage.SafeSearchAnnotation`.
200192 """
201193 features = [Feature (FeatureTypes .SAFE_SEARCH_DETECTION , limit )]
202- return self ._detect_annotation (features )
194+ annotations = self ._detect_annotation (features )
195+ return annotations .safe_searches
203196
204197 def detect_text (self , limit = 10 ):
205198 """Detect text in an image.
@@ -212,27 +205,5 @@ def detect_text(self, limit=10):
212205 :class:`~google.cloud.vision.entity.EntityAnnotation`.
213206 """
214207 features = [Feature (FeatureTypes .TEXT_DETECTION , limit )]
215- return self ._detect_annotation (features )
216-
217-
218- def _entity_from_response_type (feature_type , results ):
219- """Convert a JSON result to an entity type based on the feature."""
220- feature_key = _REVERSE_TYPES [feature_type ]
221- annotations = results .get (feature_key , ())
222- if not annotations :
223- return []
224-
225- detected_objects = []
226- if feature_type == _FACE_DETECTION :
227- detected_objects .extend (
228- Face .from_api_repr (face ) for face in annotations )
229- elif feature_type == _IMAGE_PROPERTIES :
230- detected_objects .append (
231- ImagePropertiesAnnotation .from_api_repr (annotations ))
232- elif feature_type == _SAFE_SEARCH_DETECTION :
233- detected_objects .append (
234- SafeSearchAnnotation .from_api_repr (annotations ))
235- else :
236- for result in annotations :
237- detected_objects .append (EntityAnnotation .from_api_repr (result ))
238- return detected_objects
208+ annotations = self ._detect_annotation (features )
209+ return annotations .texts
0 commit comments