Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit cfb44d8

Browse files
Xuan (Sean) Huhuxuan
authored andcommitted
Merged PR 3142: Million Scale Python Sample Update
Code format & update for sample.
1 parent 0b6a6bb commit cfb44d8

File tree

11 files changed

+279
-299
lines changed

11 files changed

+279
-299
lines changed

sample/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from view import MyApp
99

10-
1110
if __name__ == "__main__":
1211
app = MyApp(False)
1312
app.MainLoop()

sample/model/face.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
class Rect(object):
1313
"""Face Rectangle."""
14+
1415
def __init__(self, rect):
1516
super(Rect, self).__init__()
1617
self.set_rect(rect)
@@ -25,6 +26,7 @@ def set_rect(self, rect):
2526

2627
class Attribute(object):
2728
"""Attributes for face."""
29+
2830
def __init__(self, attr):
2931
super(Attribute, self).__init__()
3032
self.set_attr(attr)
@@ -41,29 +43,26 @@ def set_attr(self, attr):
4143
else:
4244
self.hair = max(
4345
attr['hair']['hairColor'],
44-
key=lambda x: x['confidence']
45-
)['color']
46+
key=lambda x: x['confidence'])['color']
4647
self.facial_hair = sum(attr['facialHair'].values()) > 0 and 'Yes' \
4748
or 'No'
4849
self.makeup = any(attr['makeup'].values())
4950
self.emotion = util.key_with_max_value(attr['emotion'])
5051
self.occlusion = any(attr['occlusion'].values())
5152
self.exposure = attr['exposure']['exposureLevel']
5253
self.head_pose = "Pitch: {}, Roll:{}, Yaw:{}".format(
53-
attr['headPose']['pitch'],
54-
attr['headPose']['roll'],
55-
attr['headPose']['yaw']
56-
)
54+
attr['headPose']['pitch'], attr['headPose']['roll'],
55+
attr['headPose']['yaw'])
5756
if not attr['accessories']:
5857
self.accessories = 'NoAccessories'
5958
else:
6059
self.accessories = ' '.join(
61-
[str(x['type']) for x in attr['accessories']]
62-
)
60+
[str(x['type']) for x in attr['accessories']])
6361

6462

6563
class Face(object):
6664
"""Face Model for each face."""
65+
6766
def __init__(self, res, path, size=util.MAX_THUMBNAIL_SIZE):
6867
super(Face, self).__init__()
6968
self.path = path
@@ -76,18 +75,13 @@ def __init__(self, res, path, size=util.MAX_THUMBNAIL_SIZE):
7675
self.persisted_id = res['persistedFaceId']
7776
if res.get('faceRectangle'):
7877
self.rect = Rect(res['faceRectangle'])
79-
self.bmp = self.bmp.GetSubBitmap(wx.Rect(
80-
self.rect.left,
81-
self.rect.top,
82-
self.rect.width,
83-
self.rect.height,
84-
))
78+
self.bmp = self.bmp.GetSubBitmap(
79+
wx.Rect(self.rect.left, self.rect.top, self.rect.width,
80+
self.rect.height))
8581
if res.get('faceAttributes'):
8682
self.attr = Attribute(res['faceAttributes'])
8783
self.bmp = util.scale_image(
88-
self.bmp.ConvertToImage(),
89-
size=size,
90-
).ConvertToBitmap()
84+
self.bmp.ConvertToImage(), size=size).ConvertToBitmap()
9185

9286
def set_name(self, name):
9387
"""Set the name for the face."""

sample/util.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
sys.path.insert(0, ROOT_DIR)
2121
import cognitive_face as CF
2222

23-
2423
IMAGE_WILDCARD = 'Image files (*.jpg, *.png)|*.jpg; *.png'
2524
INNER_PANEL_WIDTH = 710
2625
MAX_IMAGE_SIZE = 300
@@ -32,17 +31,14 @@
3231

3332
LOG_FACE_LIST_REQUEST = (
3433
'Request: Face List {} will be used for build person database. '
35-
'Checking whether group exists.'
36-
)
34+
'Checking whether group exists.')
3735
LOG_FACE_LIST_NOT_EXIST = 'Response: Face List {} does not exist before.'
3836
LOG_FACE_LIST_EXIST = 'Response: Face List {} exists.'
39-
LABEL_FACE = (
40-
'{}, {} years old\n'
41-
'Hair: {}, Facial Hair: {}\n'
42-
'Makeup: {}, Emotion: {}\n'
43-
'Occluded: {}, Exposure: {}\n'
44-
'{}\n{}\n'
45-
)
37+
LABEL_FACE = ('{}, {} years old\n'
38+
'Hair: {}, Facial Hair: {}\n'
39+
'Makeup: {}, Emotion: {}\n'
40+
'Occluded: {}, Exposure: {}\n'
41+
'{}\n{}\n')
4642

4743

4844
class SubscriptionKey(object):
@@ -67,7 +63,7 @@ def set(cls, key):
6763
"""Set the subscription key."""
6864
cls.key = key
6965
with file(SUBSCRIPTION_KEY_FILENAME, 'w') as fout:
70-
print >>fout, key
66+
print >> fout, key
7167
CF.Key.set(cls.key)
7268

7369
@classmethod
@@ -101,7 +97,7 @@ def set(cls, endpoint):
10197
"""Set the endpoint."""
10298
cls.endpoint = endpoint
10399
with file(ENDPOINT_FILENAME, 'w') as fout:
104-
print >>fout, endpoint
100+
print >> fout, endpoint
105101
CF.BaseUrl.set(cls.endpoint)
106102

107103
@classmethod
@@ -151,21 +147,16 @@ def draw_bitmap_rectangle(bitmap, faces):
151147
dc.SetTextBackground('black')
152148
dc.SetTextForeground('white')
153149
dc.SetBackgroundMode(wx.SOLID)
154-
dc.SetFont(wx.Font(8,
155-
wx.FONTFAMILY_DEFAULT,
156-
wx.FONTSTYLE_NORMAL,
157-
wx.FONTWEIGHT_BOLD))
150+
dc.SetFont(
151+
wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
152+
wx.FONTWEIGHT_BOLD))
158153
for face in faces:
159154
dc.DrawRectangle(
160-
face.rect.left * bitmap.scale,
161-
face.rect.top * bitmap.scale,
162-
face.rect.width * bitmap.scale,
163-
face.rect.height * bitmap.scale,
164-
)
155+
face.rect.left * bitmap.scale, face.rect.top * bitmap.scale,
156+
face.rect.width * bitmap.scale, face.rect.height * bitmap.scale)
165157
if face.name:
166158
text_width, text_height = dc.GetTextExtent(face.name)
167-
dc.DrawText(face.name,
168-
face.rect.left * bitmap.scale,
159+
dc.DrawText(face.name, face.rect.left * bitmap.scale,
169160
face.rect.top * bitmap.scale - text_height)
170161
dc.SelectObject(wx.NullBitmap)
171162
bitmap.bitmap.SetBitmap(bitmap.bmp)
@@ -185,8 +176,10 @@ def key_with_max_value(item):
185176

186177
def async(func):
187178
"""Async wrapper."""
179+
188180
def wrapper(*args, **kwargs):
189181
"""docstring for wrapper"""
190182
thr = Thread(target=func, args=args, kwargs=kwargs)
191183
thr.start()
184+
192185
return wrapper

sample/view/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
class MyLabelBook(LB.LabelBook):
2626
"""LabelBook part in Main Frame."""
27+
2728
def __init__(self, parent):
2829
agw_style = INB_LEFT | INB_FIT_LABELTEXT | INB_NO_RESIZE
2930
super(MyLabelBook, self).__init__(parent, agwStyle=agw_style)
@@ -44,6 +45,7 @@ def __init__(self, parent):
4445

4546
class MyTitle(wx.Panel):
4647
"""Title part in Main Frame."""
48+
4749
def __init__(self, parent):
4850
super(MyTitle, self).__init__(parent)
4951
self.SetBackgroundColour('#00b294')
@@ -66,6 +68,7 @@ def __init__(self, parent):
6668

6769
class MyFrame(wx.Frame):
6870
"""Main Frame."""
71+
6972
def __init__(self, parent):
7073
super(MyFrame, self).__init__(parent, title=TITLE, size=(1280, 768))
7174

@@ -83,8 +86,7 @@ def __init__(self, parent):
8386
status_text = (
8487
'Microsoft will receive the images you upload and may use them to '
8588
'improve Face API and related services. By submitting an image, '
86-
'you confirm you have consent from everyone in it.'
87-
)
89+
'you confirm you have consent from everyone in it.')
8890
self.status = wx.StatusBar(self)
8991
self.status.SetStatusText(status_text)
9092
sizer.Add(self.status, flag=wx.EXPAND)
@@ -95,6 +97,7 @@ def __init__(self, parent):
9597

9698
class MyApp(wx.App):
9799
"""The whole app."""
100+
98101
def OnInit(self):
99102
"""Show main frame."""
100103
frame = MyFrame(None)

sample/view/base.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class MyPanel(wx.Panel):
1515
"""Base Panel."""
16+
1617
def __init__(self, parent):
1718
super(MyPanel, self).__init__(parent)
1819
colour_window = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW)
@@ -21,6 +22,7 @@ def __init__(self, parent):
2122

2223
class MyStaticBitmap(MyPanel):
2324
"""Base StaticBitmap."""
25+
2426
def __init__(self, parent, bitmap=wx.NullBitmap, size=util.MAX_IMAGE_SIZE):
2527
super(MyStaticBitmap, self).__init__(parent)
2628
self.bmp = bitmap
@@ -49,7 +51,13 @@ def set_path(self, path):
4951

5052
class MyGridStaticBitmap(wx.Panel):
5153
"""Base Grid StaticBitmap."""
52-
def __init__(self, parent, rows=1, cols=0, vgap=0, hgap=0,
54+
55+
def __init__(self,
56+
parent,
57+
rows=1,
58+
cols=0,
59+
vgap=0,
60+
hgap=0,
5361
size=util.MAX_THUMBNAIL_SIZE):
5462
super(MyGridStaticBitmap, self).__init__(parent)
5563
self.sizer = wx.GridSizer(rows, cols, vgap, hgap)
@@ -78,6 +86,7 @@ def set_faces(self, faces):
7886

7987
class WrapCaptionFaceList(wx.WrapSizer):
8088
"""Wrap face list with caption under the face."""
89+
8190
def __init__(self, parent, confidence_faces, size=util.MAX_THUMBNAIL_SIZE):
8291
super(WrapCaptionFaceList, self).__init__()
8392
for face, confidence in confidence_faces:
@@ -96,6 +105,7 @@ def __init__(self, parent, confidence_faces, size=util.MAX_THUMBNAIL_SIZE):
96105

97106
class FindSimilarsResult(wx.Panel):
98107
"""The view for Find Similar result."""
108+
99109
def __init__(self, parent):
100110
super(FindSimilarsResult, self).__init__(parent)
101111
self.sizer = wx.BoxSizer(wx.VERTICAL)
@@ -104,7 +114,8 @@ def set_data(self, faces, res_tot, size=util.MAX_THUMBNAIL_SIZE):
104114
"""Set the data."""
105115
self.sizer.Clear(True)
106116
static_text_title = wx.StaticText(
107-
self, label='Find {} Similar Candidate Faces Results:'.format(
117+
self,
118+
label='Find {} Similar Candidate Faces Results:'.format(
108119
len(faces)))
109120
self.sizer.Add(static_text_title, 0, wx.EXPAND)
110121

@@ -133,6 +144,7 @@ def set_data(self, faces, res_tot, size=util.MAX_THUMBNAIL_SIZE):
133144

134145
class WrapFaceList(wx.Panel):
135146
"""Base wrap face list."""
147+
136148
def __init__(self, parent, faces, size=util.MAX_THUMBNAIL_SIZE):
137149
super(WrapFaceList, self).__init__(parent)
138150
self.sizer = wx.WrapSizer()
@@ -146,6 +158,7 @@ def __init__(self, parent, faces, size=util.MAX_THUMBNAIL_SIZE):
146158

147159
class CaptionWrapFaceList(wx.Panel):
148160
"""Wrap face list with a caption."""
161+
149162
def __init__(self, parent):
150163
super(CaptionWrapFaceList, self).__init__(parent)
151164
self.sizer = wx.BoxSizer(wx.VERTICAL)
@@ -165,6 +178,7 @@ def set_data(self, caption_faces_list, size=util.MAX_THUMBNAIL_SIZE):
165178

166179
class GroupResult(wx.Panel):
167180
"""The view for Group result."""
181+
168182
def __init__(self, parent):
169183
super(GroupResult, self).__init__(parent)
170184
self.sizer = wx.BoxSizer(wx.VERTICAL)
@@ -195,6 +209,7 @@ def set_data(self, faces, res, size=util.MAX_THUMBNAIL_SIZE):
195209

196210
class MyLog(wx.TextCtrl):
197211
"""The window for each scenario."""
212+
198213
def __init__(self, parent):
199214
style = wx.TE_MULTILINE | wx.TE_READONLY
200215
super(MyLog, self).__init__(parent, style=style)
@@ -210,6 +225,7 @@ def log(self, msg):
210225

211226
class MyFaceList(wx.VListBox):
212227
"""Face List."""
228+
213229
def __init__(self, parent, faces=[], **kwargs):
214230
super(MyFaceList, self).__init__(parent, **kwargs)
215231
self.SetItems(faces)
@@ -230,17 +246,10 @@ def OnDrawItem(self, dc, rect, index):
230246
textx = rect.x + 2 + face.bmp.GetWidth() + 2
231247
label_rect = wx.Rect(textx, rect.y, rect.width - textx, rect.height)
232248
label = util.LABEL_FACE.format(
233-
face.attr.gender,
234-
face.attr.age,
235-
face.attr.hair,
236-
face.attr.facial_hair,
237-
face.attr.makeup,
238-
face.attr.emotion,
239-
face.attr.occlusion,
240-
face.attr.exposure,
241-
face.attr.head_pose,
242-
face.attr.accessories
243-
)
249+
face.attr.gender, face.attr.age, face.attr.hair,
250+
face.attr.facial_hair, face.attr.makeup, face.attr.emotion,
251+
face.attr.occlusion, face.attr.exposure, face.attr.head_pose,
252+
face.attr.accessories)
244253
dc.DrawLabel(label, label_rect, wx.ALIGN_LEFT | wx.ALIGN_TOP)
245254

246255
def SetItems(self, faces):

sample/view/panel_detection.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
class DetectionPanel(base.MyPanel):
1616
"""Detection Panel."""
17+
1718
def __init__(self, parent):
1819
super(DetectionPanel, self).__init__(parent)
1920

@@ -25,12 +26,10 @@ def __init__(self, parent):
2526
self.hvsizer = wx.BoxSizer(wx.VERTICAL)
2627
self.hvsizer.SetMinSize((util.INNER_PANEL_WIDTH, -1))
2728

28-
label = (
29-
"To detect faces in an image, click the 'Choose Image' "
30-
"button. You will see a rectangle surrounding every face "
31-
"that the Face API detects. You will also see a list of "
32-
"attributes related to the faces."
33-
)
29+
label = ("To detect faces in an image, click the 'Choose Image' "
30+
"button. You will see a rectangle surrounding every face "
31+
"that the Face API detects. You will also see a list of "
32+
"attributes related to the faces.")
3433
self.static_text = wx.StaticText(self, label=label)
3534
self.static_text.Wrap(util.INNER_PANEL_WIDTH)
3635
self.hvsizer.Add(self.static_text, 0, wx.ALL, 5)
@@ -102,8 +101,7 @@ def async_detect(self, path):
102101
try:
103102
attributes = (
104103
'age,gender,headPose,smile,facialHair,glasses,emotion,hair,'
105-
'makeup,occlusion,accessories,blur,exposure,noise'
106-
)
104+
'makeup,occlusion,accessories,blur,exposure,noise')
107105
res = util.CF.face.detect(path, False, False, attributes)
108106
faces = [model.Face(face, path) for face in res]
109107
self.face_list.SetItems(faces)

0 commit comments

Comments
 (0)