-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfaceAlign_demo.py
70 lines (54 loc) · 1.82 KB
/
faceAlign_demo.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
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from keypoints_kernel import load,load2d
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Activation, Dropout, MaxPooling2D, Flatten, GlobalMaxPooling2D, BatchNormalization
from keras.optimizers import SGD, Adam
import matplotlib.pyplot as plt
from keras.preprocessing import image
import time
def cnn():
model = Sequential()
model.add(BatchNormalization(input_shape=(96,96,1)))
model.add(Conv2D(32, kernel_size=(3, 3),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128,kernel_size=(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=(3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(GlobalMaxPooling2D())
model.add(Dense(512)) #512
model.add(Activation('relu'))
model.add(Dense(30))
return model
model = cnn()
model.load_weights(filepath='weights.75-0.74206.hdf5')
# load a image
image_path='lena.jpg'
img = image.load_img(image_path,grayscale=True, target_size=(96, 96))
# preprocessing
x = image.img_to_array(img)
x /= 255
x = np.expand_dims(x, axis=0)
# time test
t1 = time.time()
pred = model.predict(x)
t2 = time.time()
t = t2 - t1
print('{:0.5f}s'.format(t))
points = pred[0]*48+48
points = points.clip(0, 96)
print(points)
fig = plt.figure()
axis = fig.add_subplot(111)
# plot face
axis.imshow(img, cmap='gray')
axis.scatter(points[0::2],points[1::2],c = 'r',marker = 'o')
plt.show()