-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp2.py
118 lines (105 loc) · 2.87 KB
/
exp2.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
import math
import imutils
import numpy as np
import cv2 as cv2
import glob
import csv
from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier
# from sklearn.svm import LinearSVC
from sklearn.model_selection import KFold
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import label_binarize
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
from scipy import interp
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
import pickle
def returnLocalizedFace(img):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray =cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
# cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
# print(faces)
# cv2.imshow/('im',img)
# cv2.waitKey(0)
if len(faces) == 0:
return img
crop_img = img[y:y + h, x:x + w]
return crop_img
def getImage(path):
return cv2.imread(path)
def show(img):
cv2.imshow('im', img)
cv2.waitKey(0)
X = []
y = []
def read(imagesPath, label):
for filename in glob.glob(imagesPath + '/*.*'):
# print(filename.split(imagesPath + '/')[1])
# print(filename)
win_size = (64, 128)
img = getImage(filename)
win_size = (64, 128)
img = cv2.resize(img, win_size)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
d = cv2.HOGDescriptor()
hog = d.compute(img)
X.append(hog.transpose()[0])
y.append(label)
def fromIndexToFeatures(X, indecies):
features = []
for i in indecies:
features.append(X[i])
return np.asarray(features)
def fromIndexToLabels(y, indecies):
labels = []
for i in indecies:
labels.append(y[i])
return np.asarray(labels)
#
read('HAPPY',0)
read('CONTEMPT',1)
read('ANGER',2)
read('DISGUST',3)
read('FEAR',4)
read('SADNESS',5)
read('SURPRISE',6)
read('NEUTRAL',7)
classes = ["HAPPY", "CONTEMPT", "ANGER", "DISGUST", "FEAR", "SADNESS", "SURPRISE", "NEUTRAL"]
y = np.asarray(y)
X = np.asarray(X)
#
#
#
clf = OneVsRestClassifier(SVC(kernel='linear', probability=True, tol=1e-3))
clf.fit(X, y)
# #
filename = 'finalized_model.sav'
pickle.dump(clf, open(filename, 'wb'))
#
#
clf = pickle.load(open(filename, 'rb'))
#
img = getImage('testImages\surprise.png')
#
#
win_size = (64, 128)
#
img = cv2.resize(img, win_size)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
d = cv2.HOGDescriptor()
hog = d.compute(img)
#
hog = hog.transpose()[0]
hog = np.asarray(hog)
print(hog)
print(clf.predict([hog]))
# # print(X)
# # print(y)