This repository was archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathface_recogniser.py
More file actions
200 lines (158 loc) · 6.29 KB
/
face_recogniser.py
File metadata and controls
200 lines (158 loc) · 6.29 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
#!/usr/bin/env python3
"""Python script to train facial recognisers and test them on different sets.
I have defined 2 sets:
- Full Emotion List (8)
- The Emotion list without Fear, Sadness, and Neutral (5)
I have 3 classifiers:
- Fisher Faces
- Eigenfaces
- Local Binary Pattern Histograms
"""
# Import packages.
import os
import time
import multiprocessing
import numpy as np
from cv2 import face # OpenCV
# My imports.
import extraction_model as exmodel
from sort_database.utils import EMOTIONS_5, EMOTIONS_8
# Start the script.
script_name = os.path.basename(__file__) # The name of this script
print("\n{}: Beginning face recogniser tests...\n".format(script_name))
start = time.clock() # Start of the speed test. clock() is most accurate.
fisherface = face.FisherFaceRecognizer_create() # Fisherface classifier
eigenface = face.EigenFaceRecognizer_create() # Eigenface classifier
lbph = face.LBPHFaceRecognizer_create() # Local Binary Patterns classifier
def run_fisher_recognizer(X_train, y_train, X_test, y_test):
"""Train the fisherface classifier."""
print("\n***> Training fisherface classifier")
print("Size of the training set is {} images.".format(len(y_train)))
fisherface.train(X_train, np.array(y_train))
print("Predicting classification set.")
cnt = 0
correct = 0
incorrect = 0
for image in X_test:
pred, conf = fisherface.predict(image)
if (pred == y_test[cnt]):
correct += 1
cnt += 1
else:
incorrect += 1
cnt += 1
return ((correct / cnt) * 100)
def run_eigen_recognizer(X_train, y_train, X_test, y_test):
"""Train the eigenface classifier."""
print("\n***>: Training eigenface classifier")
print("Size of the training set is {} images.".format(len(y_train)))
eigenface.train(X_train, np.array(y_train))
print("Predicting classification set.")
cnt = 0
correct = 0
incorrect = 0
for image in X_test:
pred, conf = eigenface.predict(image)
if (pred == y_test[cnt]):
correct += 1
cnt += 1
else:
incorrect += 1
cnt += 1
return ((correct / cnt) * 100)
def run_lbph_recognizer(X_train, y_train, X_test, y_test):
"""Train the local binary pattern classifier."""
print("\n***>: Training local binary pattern classifier")
print("Size of the training set is {} images.".format(len(y_train)))
lbph.train(X_train, np.array(y_train))
print("Predicting classification set.")
cnt = 0
correct = 0
incorrect = 0
for image in X_test:
pred, conf = lbph.predict(image)
if (pred == y_test[cnt]):
correct += 1
cnt += 1
else:
incorrect += 1
cnt += 1
return ((correct / cnt) * 100)
X_train, y_train, X_test, y_test = exmodel.get_sets_as_images(EMOTIONS_8)
X_train1, y_train1, X_test1, y_test1 = exmodel.get_sets_as_images(EMOTIONS_5)
metascore1 = []
metascore2 = []
metascore3 = []
metascore4 = []
metascore5 = []
metascore6 = []
def threadme1():
"""Produce the results on thread 1."""
for i in range(0, 10):
f = run_fisher_recognizer(X_train, y_train, X_test, y_test)
print("***> Fisherfaces on 8 Emotions have a {} percent found rate."
.format(f))
metascore1.append(f)
def threadme2():
"""Produce the results on thread 2."""
for i in range(0, 10):
f1 = run_fisher_recognizer(X_train1, y_train1, X_test1, y_test1)
print("***> Fisherfaces on 5 Emotions have a {} percent found rate."
.format(f1))
metascore2.append(f1)
def threadme3():
"""Produce the results on thread 3."""
for i in range(0, 10):
e = run_eigen_recognizer(X_train, y_train, X_test, y_test)
print("***> Eigenfaces on 8 Emotions have a {} percent found rate."
.format(e))
metascore3.append(e)
def threadme4():
"""Produce the results on thread 4."""
for i in range(0, 10):
e1 = run_eigen_recognizer(X_train1, y_train1, X_test1, y_test1)
print("***> Eigenfaces on 5 Emotions have a {} percent found rate."
.format(e1))
metascore4.append(e1)
def threadme5():
"""Produce the results on thread 5."""
for i in range(0, 10):
l = run_lbph_recognizer(X_train, y_train, X_test, y_test)
print("***> Local Binary Pattern Histograms on 8 Emotions have a {} percent found rate."
.format(l))
metascore5.append(l)
def threadme6():
"""Produce the results on thread 6."""
for i in range(0, 10):
l1 = run_lbph_recognizer(X_train1, y_train1, X_test1, y_test1)
print("***> Local Binary Pattern Histograms on 5 Emotions have a {} percent found rate."
.format(l1))
metascore6.append(l1)
# Run tasks using processes.
processes = [multiprocessing.Process(target=threadme1()),
multiprocessing.Process(target=threadme2()),
multiprocessing.Process(target=threadme3()),
multiprocessing.Process(target=threadme4()),
multiprocessing.Process(target=threadme5()),
multiprocessing.Process(target=threadme6())]
[process.start() for process in processes]
[process.join() for process in processes]
with open('results/face_recogniser', "w") as text_file:
print("\n***> Final score for Fisherfaces on 8 Emotions, has a {} percent found rate."
.format(np.mean(metascore1)))
print("***> Final score for Fisherfaces on 5 Emotions, has a {} percent found rate."
.format(np.mean(metascore2)))
print("***> Final score for Eigenfaces on 8 Emotions, has a {} percent found rate."
.format(np.mean(metascore3)))
print("***> Final score for Eigenfaces on 5 Emotions, has a {} percent found rate."
.format(np.mean(metascore4)))
print("***> Final score for Local Binary Histograms on 8 Emotions, has a {} percent found rate."
.format(np.mean(metascore5)))
print("***> Final score for Local Binary Histograms on 5 Emotions, has a {} percent found rate."
.format(np.mean(metascore6)))
# End the script.
end = time.clock()
hours, rem = divmod(end - start, 3600)
minutes, seconds = divmod(rem, 60)
print("\n***> Time elapsed: {:0>2}:{:0>2}:{:05.2f}."
.format(int(hours), int(minutes), seconds))