forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrekognition_objects.py
More file actions
340 lines (277 loc) · 11.5 KB
/
rekognition_objects.py
File metadata and controls
340 lines (277 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Wraps several Amazon Rekognition elements in Python classes. Provides functions
to draw bounding boxes and polygons on an image and display it with the default
viewer.
"""
# snippet-start:[python.example_code.rekognition.helper.imports]
import io
import logging
from PIL import Image, ImageDraw
logger = logging.getLogger(__name__)
# snippet-end:[python.example_code.rekognition.helper.imports]
# snippet-start:[python.example_code.rekognition.helper.show_bounding_boxes]
def show_bounding_boxes(image_bytes, box_sets, colors):
"""
Draws bounding boxes on an image and shows it with the default image viewer.
:param image_bytes: The image to draw, as bytes.
:param box_sets: A list of lists of bounding boxes to draw on the image.
:param colors: A list of colors to use to draw the bounding boxes.
"""
image = Image.open(io.BytesIO(image_bytes))
draw = ImageDraw.Draw(image)
for boxes, color in zip(box_sets, colors):
for box in boxes:
left = image.width * box["Left"]
top = image.height * box["Top"]
right = (image.width * box["Width"]) + left
bottom = (image.height * box["Height"]) + top
draw.rectangle([left, top, right, bottom], outline=color, width=3)
image.show()
# snippet-end:[python.example_code.rekognition.helper.show_bounding_boxes]
# snippet-start:[python.example_code.rekognition.helper.show_polygons]
def show_polygons(image_bytes, polygons, color):
"""
Draws polygons on an image and shows it with the default image viewer.
:param image_bytes: The image to draw, as bytes.
:param polygons: The list of polygons to draw on the image.
:param color: The color to use to draw the polygons.
"""
image = Image.open(io.BytesIO(image_bytes))
draw = ImageDraw.Draw(image)
for polygon in polygons:
draw.polygon(
[
(image.width * point["X"], image.height * point["Y"])
for point in polygon
],
outline=color,
)
image.show()
# snippet-end:[python.example_code.rekognition.helper.show_polygons]
# snippet-start:[python.example_code.rekognition.helper.RekognitionFace]
class RekognitionFace:
"""Encapsulates an Amazon Rekognition face."""
def __init__(self, face, timestamp=None):
"""
Initializes the face object.
:param face: Face data, in the format returned by Amazon Rekognition
functions.
:param timestamp: The time when the face was detected, if the face was
detected in a video.
"""
self.bounding_box = face.get("BoundingBox")
self.confidence = face.get("Confidence")
self.landmarks = face.get("Landmarks")
self.pose = face.get("Pose")
self.quality = face.get("Quality")
age_range = face.get("AgeRange")
if age_range is not None:
self.age_range = (age_range.get("Low"), age_range.get("High"))
else:
self.age_range = None
self.smile = face.get("Smile", {}).get("Value")
self.eyeglasses = face.get("Eyeglasses", {}).get("Value")
self.sunglasses = face.get("Sunglasses", {}).get("Value")
self.gender = face.get("Gender", {}).get("Value", None)
self.beard = face.get("Beard", {}).get("Value")
self.mustache = face.get("Mustache", {}).get("Value")
self.eyes_open = face.get("EyesOpen", {}).get("Value")
self.mouth_open = face.get("MouthOpen", {}).get("Value")
self.emotions = [
emo.get("Type")
for emo in face.get("Emotions", [])
if emo.get("Confidence", 0) > 50
]
self.face_id = face.get("FaceId")
self.image_id = face.get("ImageId")
self.timestamp = timestamp
def to_dict(self):
"""
Renders some of the face data to a dict.
:return: A dict that contains the face data.
"""
rendering = {}
if self.bounding_box is not None:
rendering["bounding_box"] = self.bounding_box
if self.age_range is not None:
rendering["age"] = f"{self.age_range[0]} - {self.age_range[1]}"
if self.gender is not None:
rendering["gender"] = self.gender
if self.emotions:
rendering["emotions"] = self.emotions
if self.face_id is not None:
rendering["face_id"] = self.face_id
if self.image_id is not None:
rendering["image_id"] = self.image_id
if self.timestamp is not None:
rendering["timestamp"] = self.timestamp
has = []
if self.smile:
has.append("smile")
if self.eyeglasses:
has.append("eyeglasses")
if self.sunglasses:
has.append("sunglasses")
if self.beard:
has.append("beard")
if self.mustache:
has.append("mustache")
if self.eyes_open:
has.append("open eyes")
if self.mouth_open:
has.append("open mouth")
if has:
rendering["has"] = has
return rendering
# snippet-end:[python.example_code.rekognition.helper.RekognitionFace]
# snippet-start:[python.example_code.rekognition.helper.RekognitionCelebrity]
class RekognitionCelebrity:
"""Encapsulates an Amazon Rekognition celebrity."""
def __init__(self, celebrity, timestamp=None):
"""
Initializes the celebrity object.
:param celebrity: Celebrity data, in the format returned by Amazon Rekognition
functions.
:param timestamp: The time when the celebrity was detected, if the celebrity
was detected in a video.
"""
self.info_urls = celebrity.get("Urls")
self.name = celebrity.get("Name")
self.id = celebrity.get("Id")
self.face = RekognitionFace(celebrity.get("Face"))
self.confidence = celebrity.get("MatchConfidence")
self.bounding_box = celebrity.get("BoundingBox")
self.timestamp = timestamp
def to_dict(self):
"""
Renders some of the celebrity data to a dict.
:return: A dict that contains the celebrity data.
"""
rendering = self.face.to_dict()
if self.name is not None:
rendering["name"] = self.name
if self.info_urls:
rendering["info URLs"] = self.info_urls
if self.timestamp is not None:
rendering["timestamp"] = self.timestamp
return rendering
# snippet-end:[python.example_code.rekognition.helper.RekognitionCelebrity]
# snippet-start:[python.example_code.rekognition.helper.RekognitionPerson]
class RekognitionPerson:
"""Encapsulates an Amazon Rekognition person."""
def __init__(self, person, timestamp=None):
"""
Initializes the person object.
:param person: Person data, in the format returned by Amazon Rekognition
functions.
:param timestamp: The time when the person was detected, if the person
was detected in a video.
"""
self.index = person.get("Index")
self.bounding_box = person.get("BoundingBox")
face = person.get("Face")
self.face = RekognitionFace(face) if face is not None else None
self.timestamp = timestamp
def to_dict(self):
"""
Renders some of the person data to a dict.
:return: A dict that contains the person data.
"""
rendering = self.face.to_dict() if self.face is not None else {}
if self.index is not None:
rendering["index"] = self.index
if self.bounding_box is not None:
rendering["bounding_box"] = self.bounding_box
if self.timestamp is not None:
rendering["timestamp"] = self.timestamp
return rendering
# snippet-end:[python.example_code.rekognition.helper.RekognitionPerson]
# snippet-start:[python.example_code.rekognition.helper.RekognitionLabel]
class RekognitionLabel:
"""Encapsulates an Amazon Rekognition label."""
def __init__(self, label, timestamp=None):
"""
Initializes the label object.
:param label: Label data, in the format returned by Amazon Rekognition
functions.
:param timestamp: The time when the label was detected, if the label
was detected in a video.
"""
self.name = label.get("Name")
self.confidence = label.get("Confidence")
self.instances = label.get("Instances")
self.parents = label.get("Parents")
self.timestamp = timestamp
def to_dict(self):
"""
Renders some of the label data to a dict.
:return: A dict that contains the label data.
"""
rendering = {}
if self.name is not None:
rendering["name"] = self.name
if self.timestamp is not None:
rendering["timestamp"] = self.timestamp
return rendering
# snippet-end:[python.example_code.rekognition.helper.RekognitionLabel]
# snippet-start:[python.example_code.rekognition.helper.RekognitionModerationLabel]
class RekognitionModerationLabel:
"""Encapsulates an Amazon Rekognition moderation label."""
def __init__(self, label, timestamp=None):
"""
Initializes the moderation label object.
:param label: Label data, in the format returned by Amazon Rekognition
functions.
:param timestamp: The time when the moderation label was detected, if the
label was detected in a video.
"""
self.name = label.get("Name")
self.confidence = label.get("Confidence")
self.parent_name = label.get("ParentName")
self.timestamp = timestamp
def to_dict(self):
"""
Renders some of the moderation label data to a dict.
:return: A dict that contains the moderation label data.
"""
rendering = {}
if self.name is not None:
rendering["name"] = self.name
if self.parent_name is not None:
rendering["parent_name"] = self.parent_name
if self.timestamp is not None:
rendering["timestamp"] = self.timestamp
return rendering
# snippet-end:[python.example_code.rekognition.helper.RekognitionModerationLabel]
# snippet-start:[python.example_code.rekognition.helper.RekognitionText]
class RekognitionText:
"""Encapsulates an Amazon Rekognition text element."""
def __init__(self, text_data):
"""
Initializes the text object.
:param text_data: Text data, in the format returned by Amazon Rekognition
functions.
"""
self.text = text_data.get("DetectedText")
self.kind = text_data.get("Type")
self.id = text_data.get("Id")
self.parent_id = text_data.get("ParentId")
self.confidence = text_data.get("Confidence")
self.geometry = text_data.get("Geometry")
def to_dict(self):
"""
Renders some of the text data to a dict.
:return: A dict that contains the text data.
"""
rendering = {}
if self.text is not None:
rendering["text"] = self.text
if self.kind is not None:
rendering["kind"] = self.kind
if self.geometry is not None:
rendering["polygon"] = self.geometry.get("Polygon")
return rendering
# snippet-end:[python.example_code.rekognition.helper.RekognitionText]