-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
62 lines (57 loc) · 2.53 KB
/
models.py
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
import os
from keras.models import *
from keras.layers import *
from keras.applications import *
from keras import backend as K
import tensorflow as tf
import mtcnn_detect_face
def emotion_recognition_model(weight_path):
base_model = MobileNet(weights=None, include_top=False, input_shape=(128, 128, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Reshape((1, 1, 1024), name='reshape_1')(x)
x = Dropout(1e-3, name='dropout')(x)
x = Conv2D(7, (1, 1), padding='same', name='conv_preds')(x)
x = Activation('softmax', name='act_softmax')(x)
predictions = Reshape((7,), name='reshape_2')(x)
model = Model(inputs=base_model.input, outputs=predictions)
model.load_weights(weight_path)
return model
def create_mtcnn(sess, model_path):
with tf.variable_scope('pnet2'):
data = tf.placeholder(tf.float32, (None,None,None,3), 'input')
pnet = mtcnn_detect_face.PNet({'data':data})
pnet.load(os.path.join(model_path, 'det1.npy'), sess)
with tf.variable_scope('rnet2'):
data = tf.placeholder(tf.float32, (None,24,24,3), 'input')
rnet = mtcnn_detect_face.RNet({'data':data})
rnet.load(os.path.join(model_path, 'det2.npy'), sess)
with tf.variable_scope('onet2'):
data = tf.placeholder(tf.float32, (None,48,48,3), 'input')
onet = mtcnn_detect_face.ONet({'data':data})
onet.load(os.path.join(model_path, 'det3.npy'), sess)
return pnet, rnet, onet
def get_src_landmarks(x0, x1, y0, y1, pnts):
"""
x0, x1, y0, y1: (smoothed) bbox coord.
pnts: landmarks predicted by MTCNN
"""
src_landmarks = [(int(pnts[i+5][0]-x0),
int(pnts[i][0]-y0)) for i in range(5)]
return src_landmarks
def process_mtcnn_bbox(bboxes, im_shape):
"""
output bbox coordinate of MTCNN is (y0, x0, y1, x1)
Here we process the bbox coord. to a square bbox with ordering (x0, y1, x1, y0)
"""
for i, bbox in enumerate(bboxes):
y0, x0, y1, x1 = bboxes[i,0:4]
w, h = int(y1 - y0), int(x1 - x0)
length = (w + h)/2
center = (int((x1+x0)/2),int((y1+y0)/2))
new_x0 = np.max([0, (center[0]-length//2)])#.astype(np.int32)
new_x1 = np.min([im_shape[0], (center[0]+length//2)])#.astype(np.int32)
new_y0 = np.max([0, (center[1]-length//2)])#.astype(np.int32)
new_y1 = np.min([im_shape[1], (center[1]+length//2)])#.astype(np.int32)
bboxes[i,0:4] = new_x0, new_y1, new_x1, new_y0
return bboxes