-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ensemble_Final_Program.py
380 lines (274 loc) · 10.1 KB
/
Ensemble_Final_Program.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# import the necessary packages
from imutils import face_utils
from sklearn import svm
from collections import Counter
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
from sklearn.model_selection import StratifiedKFold
# from sklearn.ensemble import VotingClassifier
import numpy as np
import imutils
import dlib
import cv2
import sys
import os
import math
import pandas as pd
import pickle
model_file = "Finalized_model6epochs.sav"
training_directory = "Original Dataset"
emotion_labels = [0]*8
labels_dict = { '0': 'Neutral',
'1': 'Anger',
'2': 'Contempt',
'3': 'Disgust',
'4': 'Fear',
'5': 'Happy',
'6': 'Sadness',
'7': 'Surprise'}
# For total training images
count = 0
# Maintaining a numpy array for storing labels
labels = np.empty((0, 0))
# Maintaining a numpy array for storing landmarks(feature matrices) of each image
featureMatrix = np.empty( (0, 68) )
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def facialDetector(image):
# load the input image, resize it, and convert it to grayscale
# image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = detector(gray, 1)
# loop over the face detections
for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the landmark (x, y)-coordinates to a NumPy array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# visualize all facial landmarks with a transparent overlay
output = face_utils.visualize_facial_landmarks(image, shape)
return rect, shape
# flag 0 = For Training Images
# flag 1 = For Runtime Images
def extractLandmarks(frame, flag):
global featureMatrix
# Detecting facial landmarks & store the 68 2D-coordinates in landmarks(numpy array)
rect, landmarks = facialDetector(frame)
lt = []
# Converting the 68 (x, y) 2D-coordinates to 68 1D-coordinates by applying (x*x + y*y) ^ (1/2) to make it unique
for coordinates in landmarks:
temp = math.sqrt ( math.pow(coordinates[0],2) + math.pow(coordinates[1],2) )
# if we encounter (y, x) for which (x, y) has already been calculated
if temp in lt:
# if (x<y) then postive value otherwise negative value
if coordinates[0] < coordinates[1]:
lt.append(temp)
else:
lt.append(-temp)
else:
lt.append(temp)
# For Training Images we store landmarks in text document
if flag==0:
f = open("Training Features.txt", "a")
# Converting each float value in lt list to string to write it in the text file
f.write(','.join(str(number) for number in lt))
f.write("\n")
f.close()
# For Runtime Images insert landmarks in the numpy array
else:
featureMatrix = np.insert(featureMatrix, 0, np.array([lt]), axis=0)
return rect
def extractLabels(label_file):
global labels
global count
global emotion_labels
f = open(label_file)
label = f.read(4) # reading first 4 characters
label = label.lstrip() # deleting left whitespaces
if label != '':
emotion_labels[int(label)] += 1
labels = np.append(labels, label)
count += 1
f.close()
def extractFeatures(directory):
for file in os.listdir(directory):
location = directory + '/' + file
# If it is an image file
if "png" in file:
# Reading the image from above path
image = cv2.imread(location)
output = extractLandmarks(image, 0)
# If it is a text i.e emotion file
elif "txt" in file:
extractLabels(location)
else:
if os.path.isdir(location):
# print (file)
if "S" in file:
print (file)
extractFeatures(location)
def readLandmarks(file):
global featureMatrix
f = open(file)
line = f.readline()
while line:
# Splitting each line by ',' character to get landmarks for that image
temp = line.split(',')
# Converting the string value to float value for every elememt in the list
temp = list(map(float, temp))
# Appending the "lt" list which stores the face's landmark's location in numpy array
featureMatrix = np.append( featureMatrix, np.array([temp]), axis=0 )
line = f.readline()
f.close()
print ("Landmarks Read")
def trainModel():
global featureMatrix
global labels
global model_file
no_of_iterations = 6
print ()
print ("Training Epochs :")
# The possible values of tuning parameters of svm
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
'C': [0.001, 0.1, 10, 25, 50, 100, 1000]},
{'kernel': ['sigmoid'], 'gamma': [1e-2, 1e-3, 1e-4, 1e-5],
'C': [0.001, 0.1, 10, 25, 50, 100, 1000]},
{'kernel': ['linear'], 'C': [0.001, 0.1, 10, 25, 50, 100, 1000]}
]
# KFold cross-validation Method implemented with dataset divided into 10 folds
kf = StratifiedKFold( n_splits=no_of_iterations )
i = 1
for train_index, test_index in kf.split(featureMatrix, labels):
print (i)
print ("Train: ", len(train_index), " images\nTest: ", len(test_index), " images")
# Splitting the featureMatrix i.e. 9 folds contains training images & 1 fold contains testing images
train_feature, test_feature = featureMatrix[train_index], featureMatrix[test_index]
# Splitting the labels i.e. 9 folds contains training labels & 1 fold contains testing labels
train_labels, test_labels = labels[train_index], labels[test_index]
# Selecting the best combination of tuning paramters
grid_search = GridSearchCV(svm.SVC(), tuned_parameters, cv=3, iid='False')
# Training the train_dataset on tuned parameters
grid_search.fit(train_feature, train_labels)
i += 1
pickle.dump(grid_search, open(model_file, 'wb'))
# pickle.dump(model, open(file_name, 'wb'))
def countImagesPerEmotion():
global emotion_labels
extractFeatures(training_directory + '/' + 'Emotion Labels')
f = open("Images Per Emotion.txt","w")
i = 0
for l in emotion_labels:
print (labels_dict[str(i)], l)
f.write(labels_dict[str(i)] + ' ' + str(l))
f.write('\n')
i += 1
f.close()
def ensemble(test_feature, loaded_model1, loaded_model2, loaded_model3, loaded_model4, loaded_model5, loaded_model6 ):
global featureMatrix
global labels
pred1 = loaded_model1.predict(test_feature)
pred2 = loaded_model2.predict(test_feature)
pred3 = loaded_model3.predict(test_feature)
pred4 = loaded_model4.predict(test_feature)
pred5 = loaded_model5.predict(test_feature)
pred6 = loaded_model6.predict(test_feature)
estimators = []
estimators.append(pred1[0])
estimators.append(pred2[0])
estimators.append(pred3[0])
estimators.append(pred4[0])
estimators.append(pred5[0])
estimators.append(pred6[0])
return estimators
# return Counter(estimators).most_common(1)[0][0]
# estimators.append(('model1',loaded_model1))
# estimators.append(('model2',loaded_model2))
# estimators.append(('model3',loaded_model3))
# estimators.append(('model4',loaded_model4))
# estimators.append(('model5',loaded_model5))
# estimators.append(('model6',loaded_model6))
# ensemble = VotingClassifier(estimators)
# print ('Fitting starting....')
# ensemble.fit(featureMatrix, labels)
# print ('Ensembled Model fitted')
# pickle.dump(ensemble, open('Ensembled_model_file.sav', 'wb'))
def main():
global featureMatrix
global labels
global count
global emotion_labels
global model_file
# If model is not trained then train it and save it in the file
if not os.path.exists(model_file):
print ("Training Directories: ")
# Extracting landmarks only when the text document (containing the landmarks for every face in training dataset) does not exist
if not os.path.exists("Training Features.txt"):
print ("Images")
extractFeatures(training_directory + '/' + 'Images')
print ("Emotion Labels")
# Extracting Labels for the training dataset
extractFeatures(training_directory + '/' + 'Emotion Labels')
# Reading the landmarks from the text document
readLandmarks("Training Features.txt")
trainModel()
count = 10708
loaded_model1 = pickle.load(open('Finalized_model2epochs.sav', 'rb'))
loaded_model2 = pickle.load(open('Finalized_model3epochs.sav', 'rb'))
loaded_model3 = pickle.load(open('Finalized_model5epochs.sav', 'rb'))
loaded_model4 = pickle.load(open('Finalized_model6epochs.sav', 'rb'))
loaded_model5 = pickle.load(open('Finalized_model7epochs.sav', 'rb'))
loaded_model6 = pickle.load(open('Finalized_model8epochs.sav', 'rb'))
print ('Models Loaded')
print ("Training Done on {} Images".format(count))
cap = cv2.VideoCapture (0)
while True:
try:
featureMatrix = np.empty( (0, 68) )
ret, frame = cap.read ()
rect = extractLandmarks(frame, 1)
test_feature = featureMatrix
predicted_label = []
# Predicting the labels for testing images
# predicted_label = ensemble(test_feature)
# predicted_label = loaded_model.predict1(test_feature)
predicted_label = ensemble(test_feature, loaded_model1, loaded_model2, loaded_model3, loaded_model4, loaded_model5, loaded_model6)
x = rect.left() + 70
y = rect.top() - 25
w = rect.right() + 100
h = rect.bottom() + 50
freq = [0]*8
for predicted in predicted_label:
try :
freq[int(predicted)] += 1
except ValueError:
print (predicted)
# print (freq)
prob = [0]*8
for i in range(0,8):
prob[i] = (freq[i]/6)*100
cv2.rectangle(frame, (x, y), (w, h), (0,0,0), 2)
j = 90
for i in range(0,8):
# print (labels_dict[str(i)] + ": " + str(prob[i]))
maximum_prob = max(prob)
string = str(prob[i]).split(".")
probability = string[0] + "%"
if prob[i] == maximum_prob:
cv2.putText(frame, labels_dict[str(i)] + ": " + probability, (30, j), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
else:
cv2.putText(frame, labels_dict[str(i)] + ": " + probability, (30, j), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 1)
j += 30
cv2.imshow ('WebCam', frame)
# cv2.resize('WebCam', 700, 700)
if cv2.waitKey (1) == 27: #27 is the Escape Key
sys.exit()
except TypeError:
pass
# When everything done, release the capture
cap.release ()
cv2.destroyAllWindows ()
if __name__ == '__main__':
main()