forked from SujanKoju/FERA_HCOE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
75 lines (66 loc) · 2.71 KB
/
classifier.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
#The Emotion Face detection Scripts
#You can modify this script as you wish
import cv2
import glob as gb
import random
import numpy as np
import subprocess
#Emotion list
emojis = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"]
#Initialize fisher face classifier
fisher_face = cv2.face.FisherFaceRecognizer_create()
data = {}
#Function defination to get file list, randomly shuffle it and split 67/33
def getFiles(emotion):
files = gb.glob("D:\\Final Year Project\\FERA\\final_ dataset\\%s\\*" %emotion)
random.shuffle(files)
training = files[:int(len(files)*0.67)] #get first 67% of file list
prediction = files[-int(len(files)*0.33):] #get last 33% of file list
return training, prediction
def makeTrainingAndValidationSet():
print ("Traing and validation set creation : Started")
training_data = []
training_labels = []
prediction_data = []
prediction_labels = []
for emotion in emojis:
training, prediction = getFiles(emotion)
#Append data to training and prediction list, and generate labels 0-7
for item in training:
image = cv2.imread(item) #open image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #convert to grayscale
training_data.append(gray) #append image array to training data list
print (gray.shape)
training_labels.append(emojis.index(emotion))
for item in prediction: #repeat above process for prediction set
image = cv2.imread(item)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
prediction_data.append(gray)
prediction_labels.append(emojis.index(emotion))
return training_data, training_labels, prediction_data, prediction_labels
def runClassifier():
training_data, training_labels, prediction_data, prediction_labels = makeTrainingAndValidationSet()
print ("training classifier using the training data")
print ("size of training set is:", len(training_labels), "images")
fisher_face.train(training_data, np.asarray(training_labels))
fisher_face.save("result/result.xml")
print ("classification prediction")
counter = 0
right = 0
wrong = 0
for image in prediction_data:
pred, conf = fisher_face.predict(image)
if pred == prediction_labels[counter]:
right += 1
counter += 1
else:
wrong += 1
counter += 1
return ((100*right)/(right + wrong))
#Now run the classifier
metascore = []
for i in range(0,10):
right = runClassifier()
print ("got", right, "percent right!")
metascore.append(right)
print ("\n\nend score:", np.mean(metascore), "percent right!")