|
4 | 4 | File: face.py |
5 | 5 | Description: Face model for Python SDK Sample. |
6 | 6 | """ |
7 | | - |
8 | 7 | import wx |
9 | 8 |
|
10 | 9 | import util |
11 | 10 |
|
12 | 11 |
|
| 12 | +class Rect(object): |
| 13 | + """Face Rectangle.""" |
| 14 | + def __init__(self, rect): |
| 15 | + super(Rect, self).__init__() |
| 16 | + self.set_rect(rect) |
| 17 | + |
| 18 | + def set_rect(self, rect): |
| 19 | + """docstring for set_rect""" |
| 20 | + self.left = int(rect['left']) |
| 21 | + self.top = int(rect['top']) |
| 22 | + self.width = int(rect['width']) |
| 23 | + self.height = int(rect['height']) |
| 24 | + |
| 25 | + |
| 26 | +class Attribute(object): |
| 27 | + """Attributes for face.""" |
| 28 | + def __init__(self, attr): |
| 29 | + super(Attribute, self).__init__() |
| 30 | + self.set_attr(attr) |
| 31 | + |
| 32 | + def set_attr(self, attr): |
| 33 | + """Set the attribute value.""" |
| 34 | + self.gender = attr['gender'] |
| 35 | + self.age = int(attr['age']) |
| 36 | + if not attr['hair']['hairColor']: |
| 37 | + if attr['hair']['invisible']: |
| 38 | + self.hair = 'Invisible' |
| 39 | + else: |
| 40 | + self.hair = 'Bald' |
| 41 | + else: |
| 42 | + self.hair = max( |
| 43 | + attr['hair']['hairColor'], |
| 44 | + key=lambda x: x['confidence'] |
| 45 | + )['color'] |
| 46 | + self.facial_hair = sum(attr['facialHair'].values()) > 0 and 'Yes' \ |
| 47 | + or 'No' |
| 48 | + self.makeup = any(attr['makeup'].values()) |
| 49 | + self.emotion = util.key_with_max_value(attr['emotion']) |
| 50 | + self.occlusion = any(attr['occlusion'].values()) |
| 51 | + self.exposure = attr['exposure']['exposureLevel'] |
| 52 | + self.head_pose = "Pitch: {}, Roll:{}, Yaw:{}".format( |
| 53 | + attr['headPose']['pitch'], |
| 54 | + attr['headPose']['roll'], |
| 55 | + attr['headPose']['yaw'] |
| 56 | + ) |
| 57 | + if not attr['accessories']: |
| 58 | + self.accessories = 'NoAccessories' |
| 59 | + else: |
| 60 | + self.accessories = ' '.join( |
| 61 | + [str(x['type']) for x in attr['accessories']] |
| 62 | + ) |
| 63 | + |
| 64 | + |
13 | 65 | class Face(object): |
14 | 66 | """Face Model for each face.""" |
15 | 67 | def __init__(self, res, path, size=util.MAX_THUMBNAIL_SIZE): |
16 | 68 | super(Face, self).__init__() |
17 | 69 | self.path = path |
18 | | - self.bmp = wx.Bitmap(path) |
| 70 | + img = util.rotate_image(path) |
| 71 | + self.bmp = img.ConvertToBitmap() |
19 | 72 | self.name = None |
20 | 73 | if res.get('faceId'): |
21 | 74 | self.id = res['faceId'] |
22 | 75 | if res.get('persistedFaceId'): |
23 | 76 | self.persisted_id = res['persistedFaceId'] |
24 | 77 | if res.get('faceRectangle'): |
25 | | - rect = res['faceRectangle'] |
26 | | - self.left = int(rect['left']) |
27 | | - self.top = int(rect['top']) |
28 | | - self.width = int(rect['width']) |
29 | | - self.height = int(rect['height']) |
| 78 | + self.rect = Rect(res['faceRectangle']) |
30 | 79 | self.bmp = self.bmp.GetSubBitmap(wx.Rect( |
31 | | - self.left, self.top, self.width, self.height)) |
| 80 | + self.rect.left, |
| 81 | + self.rect.top, |
| 82 | + self.rect.width, |
| 83 | + self.rect.height, |
| 84 | + )) |
32 | 85 | if res.get('faceAttributes'): |
33 | | - attr = res['faceAttributes'] |
34 | | - self.age = int(attr['age']) |
35 | | - self.gender = attr['gender'] |
36 | | - self.head_pose = "Pitch: {}, Roll:{}, Yaw:{}".format( |
37 | | - attr['headPose']['pitch'], |
38 | | - attr['headPose']['roll'], |
39 | | - attr['headPose']['yaw'] |
40 | | - ) |
41 | | - self.smile = float(attr['smile']) > 0 and 'Smile' or 'Not Smile' |
42 | | - self.facial_hair = sum(attr['facialHair'].values()) > 0 and 'Yes' \ |
43 | | - or 'No' |
44 | | - self.glasses = attr['glasses'] |
45 | | - self.emotion = max( |
46 | | - attr['emotion'], |
47 | | - key=lambda key: attr['emotion'][key] |
48 | | - ) |
49 | | - self.bmp = util.scale_bitmap(self.bmp, size) |
| 86 | + self.attr = Attribute(res['faceAttributes']) |
| 87 | + self.bmp = util.scale_image( |
| 88 | + self.bmp.ConvertToImage(), |
| 89 | + size=size, |
| 90 | + ).ConvertToBitmap() |
50 | 91 |
|
51 | 92 | def set_name(self, name): |
52 | 93 | """Set the name for the face.""" |
|
0 commit comments