-
Notifications
You must be signed in to change notification settings - Fork 27
/
EmoDetect.py
116 lines (83 loc) · 3.09 KB
/
EmoDetect.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
#!/usr/bin/python
#Title: Script to find emotion using facial expression
#Date:25/10/2015
#Author:Adithya Selvaprithiviraj
#PS: Not trained for nuetral expression
import argparse,sys
try:
from FeatureGen import*
except ImportError:
print "Make sure FeatureGen.pyc file is in the current directory"
exit()
try:
import dlib
from skimage import io
import numpy
import cv2
from sklearn.externals import joblib
except ImportError:
print "Make sure you have OpenCV, dLib, scikit learn and skimage libraries properly installed"
exit()
emotions={ 1:"Anger", 2:"Contempt", 3:"Disgust", 4:"Fear", 5:"Happy", 6:"Sadness", 7:"Surprise"}
def Predict_Emotion(filename):
print "Opening image...."
try:
img=io.imread(filename)
cvimg=cv2.imread(filename)
except:
print "Exception: File Not found."
return
win.clear_overlay()
win.set_image(img)
dets=detector(img,1)
if len(dets)==0:
print "Unable to find any face."
return
for k,d in enumerate(dets):
shape=predictor(img,d)
landmarks=[]
for i in range(68):
landmarks.append(shape.part(i).x)
landmarks.append(shape.part(i).y)
landmarks=numpy.array(landmarks)
print "Generating features......"
features=generateFeatures(landmarks)
features= numpy.asarray(features)
print "Performing PCA Transform......."
pca_features=pca.transform(features)
print "Predicting using trained model........"
emo_predicts=classify.predict(pca_features)
print "Predicted emotion using trained data is { " + emotions[int(emo_predicts[0])] + " }"
print ""
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(cvimg,emotions[int(emo_predicts[0])],(20,20), font, 1,(0,255,255),2)
win.add_overlay(shape)
cv2.namedWindow("Output")
cv2.imshow("Output",cvimg)
cv2.waitKey(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-i', type=str, nargs='+', help="Enter the filenames with extention of an Image")
arg=parser.parse_args()
if not len(sys.argv) > 1:
parser.print_help()
exit()
landmark_path="shape_predictor_68_face_landmarks.dat"
print "Initializing Dlib face Detector.."
detector= dlib.get_frontal_face_detector()
print "Loading landmark identification data..."
try:
predictor= dlib.shape_predictor(landmark_path)
except:
print "Unable to find trained facial shape predictor. \nYou can download a trained facial shape predictor from: \nhttp://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2"
exit()
win=dlib.image_window()
print "Loading trained data....."
try:
classify=joblib.load("traindata.pkl")
pca=joblib.load("pcadata.pkl")
except:
print "Unable to load trained data. \nMake sure that traindata.pkl and pcadata.pkl are in the current directory"
exit()
for filename in arg.i:
Predict_Emotion(filename)