-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmotionClass.py
212 lines (180 loc) · 7.62 KB
/
EmotionClass.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 19 23:53:25 2016
@author: KD5299
"""
import os
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import np_utils
from keras import backend as K
from skimage import exposure,transform,feature
import pandas as pd
from sklearn import preprocessing
import cv2
#GIT_PATH = "C:\Users\KD5299\Python-Project"
GIT_PATH = "/Users/ludoviclelievre/Documents/Python-Project"
# import haar cascade classifier
cascPath = os.path.join(GIT_PATH,'haarcascade_frontalface_default.xml')
faceCascade = cv2.CascadeClassifier(cascPath)
dico_emotion = {0:'Angry', 1:'Fear',
2:'Happy', 3:'Sad', 4:'Surprise', 5:'Neutral'}
#import getpass
#getpass.getuser()
# Importation des donnees
class Data:
def __init__(self,df):
self.data_emotion = df['emotion'].as_matrix(columns=None)
self.data_usage = df['Usage'].as_matrix(columns=None)
self.data_image = df[list(filter(lambda pxl: type(pxl)!=str ,df.columns.tolist()))].as_matrix(columns=None)
@property
def nb_example(self):
return int(self.data_emotion.shape[0])
@property
def dim(self):
return int(np.sqrt(self.data_image[0].shape[0]))
@property
def nb_classes(self):
return int(np.unique(self.data_emotion).shape[0])
@property
def input_shape(self):
if K.image_dim_ordering() == 'th':
return (1, self.dim, self.dim)
else:
return (self.dim, self.dim,1)
def CreateUsageSet(self,usage):
mask = np.in1d(self.data_usage, usage)
X = self.data_image[mask, :]
Y = self.data_emotion[mask]
if K.image_dim_ordering() == 'th':
X = X.reshape(X.shape[0], 1,self.dim, self.dim)
else:
X = X.reshape(X.shape[0], self.dim, self.dim, 1)
X = X.astype('float32')
Y = np_utils.to_categorical(Y, self.nb_classes)
return X,Y
# for second method
def lbp(self,P=24,R=8):
data_image_lbp = self.data_image*0
i = 0
for image in self.data_image:
data_image_lbp[i] = feature.local_binary_pattern(
image.reshape((self.dim, self.dim)),P,R,method = 'uniform').ravel()
i+=1
self.data_image = data_image_lbp
def HistoBlocks(self,nbBlock = 36,FeatDim = 20):
ImgDim = self.dim
SpatialDim= int(np.sqrt(nbBlock))
SpatialRatio = int(ImgDim/SpatialDim)
A= np.ones((SpatialRatio,SpatialRatio))
block = np.bmat([[a*A+b for a in range(SpatialDim)] for b in range(0,SpatialDim**2,SpatialDim)])
def comput_histo(image):
H,_,_ = np.histogram2d(np.asarray(block).ravel(),image,bins=(SpatialDim**2,FeatDim))
return H.ravel()
return np.apply_along_axis(
comput_histo,1,self.data_image)
def zoom(self,z):
data_image_zoom = np.ndarray((self.data_image.shape[0],
self.data_image.shape[1]/z**2))
i = 0
for image in self.data_image:
data_image_zoom[i] = transform.downscale_local_mean(
image.reshape((self.dim, self.dim)),(z,z)).ravel()
i=1+i
self.data_image = data_image_zoom
# self.dim = int(self.dim / z)
def EnhanceContrast(self):
self.data_image = np.apply_along_axis(
exposure.equalize_hist,1,self.data_image)
def Normalize(self):
self.data_image =self.data_image/255.
def ViewOneEmotion(self,example,ax,usage=False):
# fig = plt.figure()
# ax = fig.add_subplot(111)
if usage:
image = self.data_image[self.data_usage==usage][example]
emotion = self.data_emotion[self.data_usage==usage][example]
else:
image = self.data_image[example]
emotion = self.data_emotion[example]
pixels = image.reshape(self.input_shape[0:2])
ax.imshow(pixels, cmap='gray')
ax.set_title(dico_emotion[emotion])
plt.axis('off')
return ax
def ViewSomeEmotions(self,example_list,usage=False):
fig = plt.figure(figsize=(16,8))
i = 1
nrow = int(np.sqrt(len(example_list)+.25)-0.5)+1
for example in example_list:
ax = fig.add_subplot(nrow,nrow+1,i)
ax = self.ViewOneEmotion(example,ax,usage)
i = i+1
def ViewEmotionPredictions(self,usage,example_list,prediction_matrix):
nrow = 2*(int(np.sqrt(len(example_list)+.25)-0.5)+1)
ncol = (2*len(example_list))/nrow+1
fig = plt.figure(figsize=(12,12))
i = 1
for example in example_list:
ax = fig.add_subplot(nrow,ncol,i)
ax = self.ViewOneEmotion(usage,example,ax)
ax1 = fig.add_subplot(nrow,ncol,i+ncol)
ax1.bar(range(0,self.nb_classes), prediction_matrix[example],color =colors)
ax1.set_xticks(np.arange(0.5,6.5,1))
ax1.set_xticklabels(dico_emotion.values(), rotation=45, fontsize=7)
ax1.set_yticks(np.arange(0.0,1.1,0.5))
# if i%ncol==0:
# i = i+ncol
i = i+1+ncol*(i%ncol==0)
plt.tight_layout()
# Substract the mean value of each image
def SubstractMean(self):
mean = self.data_image.mean(axis=1)
self.data_image = self.data_image - mean[:, np.newaxis]
# set the image norm to 100 and standardized each pixels accross the image
def Normalization(self):
# set the image norm to 100
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0,100))
self.data_image = min_max_scaler.fit_transform(self.data_image)
# standardized each pixels accross the image
scaler = preprocessing.StandardScaler().fit(self.data_image[self.data_usage=='Training'])
self.data_image = scaler.transform(self.data_image)
def FlipTrain(self,usage):
flip_image = self.data_image[self.data_usage==usage]*0
i = 0
for image in self.data_image[self.data_usage==usage]:
flip_image[i] = np.fliplr(
image.reshape(self.input_shape[0:2])).ravel()
i=1+i
flip_emotion = self.data_emotion[self.data_usage==usage]
flip_usage = self.data_usage[self.data_usage==usage]+" flip"
self.data_image = np.concatenate(
(self.data_image,flip_image),axis=0)
self.data_emotion = np.concatenate(
(self.data_emotion,flip_emotion),axis=0)
self.data_usage = np.concatenate(
(self.data_usage,flip_usage),axis=0)
def Preprocess_opencv(self):
opencv_emotion = self.data_emotion
i=0
for image in self.data_image:
gray = image.reshape(48, 48)
faces = faceCascade.detectMultiScale(
gray.astype('uint8'),
scaleFactor=1.1,
minNeighbors=5,
)
if len(faces)==1:
for (x,y,w,h) in faces:
gray = gray[y:y+h, x:x+w]
gray = cv2.resize(gray.astype('uint8'), (48,48), interpolation = cv2.INTER_CUBIC)
image = gray.astype('int64').ravel()
else:
opencv_emotion[i] = -2
i+=1
self.data_emotion = opencv_emotion
def Supress_emotion(self, emotion):
for e in emotion:
self.data_usage = self.data_usage[self.data_emotion!=e]
self.data_image = self.data_image[self.data_emotion!=e]
self.data_emotion = self.data_emotion[self.data_emotion!=e]