-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntegrated.py
153 lines (117 loc) · 4.24 KB
/
Integrated.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
from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.models import model_from_json
from FER2013_Input_Keras import FER2013_Input_Keras
import csv
import numpy as np
import tensorflow as tf
from PIL import Image
from numpy import array
from scipy.misc import toimage
from resizeimage import resizeimage
from scipy.misc import toimage
import Face_Detection
#import Face_Detection_Scale1
#import Face_Detection_Scale2
#import Face_Detection_Scale3
import dlib
import math
from skimage import io
import sys
batch_size = 1
num_classes = 7
epochs = 1000
detect = Face_Detection.Preprocessing()
Faces_Scale1, Faces_Scale2, Faces_Scale3 = detect.Faces_Detection()
img_rows, img_cols = 42,42
#fer = FER2013_Input_Keras('/home/alaa/Desktop/GP/')
#Testing_labels, Testing_Images = fer.FER2013_Testing_Set()
#Testing_Images = Testing_Images[0]
Input_Images = Faces_Scale1[:]
Input_Images = Input_Images.reshape(Input_Images.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
Input_Images = Input_Images.astype('float32')
Input_Images /= 255
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights('model_weights.h5')
# evaluate loaded model on test data
loaded_model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
score1 = loaded_model.predict(Input_Images, verbose=0)
img_rows, img_cols = 84,84
#fer = FER2013_Input_Keras('/home/alaa/Desktop/GP/')
#Testing_labels, Testing_Images = fer.FER2013_Testing_Set()
#Testing_Images = Testing_Images[0]
Input_Images = Faces_Scale2.reshape(Input_Images.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
Input_Images = Input_Images.astype('float32')
Input_Images /= 255
# load json and create model
json_file = open('modelSecondScale.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights('modelSecondScale_weights.h5')
# evaluate loaded model on test data
loaded_model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
score2 = loaded_model.predict(Input_Images, verbose=0)
img_rows, img_cols = 90,90
#fer = FER2013_Input_Keras('/home/alaa/Desktop/GP/')
#Testing_labels, Testing_Images = fer.FER2013_Testing_Set()
#Testing_Images = Testing_Images[0]
Input_Images = Faces_Scale3.reshape(Input_Images.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
Input_Images = Input_Images.astype('float32')
Input_Images /= 255
# load json and create model
json_file = open('modelThirdScale1.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights('modelThirdScale1_weights.h5')
# evaluate loaded model on test data
loaded_model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
score3 = loaded_model.predict(Input_Images, verbose=0)
score = []
for i in range(len(score1)):
temp = []
for j in range(len(score1[i])):
temp.append((score1[i][j]+score2[i][j]+score3[i][j])/3)
score.append(temp)
'''score = loaded_model.predict_classes(Input_Images, verbose=0)
classes = {0:'Angry', 1:'Disgust', 2:'Fear', 3:'Happy', 4:'Sad', 5:'Surprise', 6:'Neutral'}
for i in range(len(Input_Images)):
toimage(out[i]).show()
print(classes[score[i]])
dlib.hit_enter_to_continue()
#print(score)
'''
classes = {0:'Angry', 1:'Disgust', 2:'Fear', 3:'Happy', 4:'Sad', 5:'Surprise', 6:'Neutral'}
score_exp = []
softmax = []
output = []
for i in range(len(Input_Images)):
score_exp.append([math.exp(j) for j in score[i]])
sum_exp = sum(score_exp[i])
softmax.append([round(j / sum_exp, 3) for j in score_exp[i]])
index = 0
maxVal = 0
for j in range(len(softmax[i])):
if(softmax[i][j]>=maxVal):
index = j
maxVal = softmax[i][j]
output.append(classes[index])
print(output)