-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathface_rec.py
214 lines (163 loc) · 7.73 KB
/
face_rec.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
213
214
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 26 20:42:42 2019
@author: Youssef Kishk
"""
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import scipy
from sklearn.neighbors import KNeighborsClassifier
import warnings
warnings.simplefilter("ignore")
#************************************************************************************
def read_images():
file_name = "att_faces"
data_directory = ""+file_name
data = np.zeros((400,10304))
labels = np.zeros((400,1))
i=0
for name in os.listdir(data_directory):
folderPath = os.path.join(data_directory,name)
for ImageName in os.listdir(folderPath):
Image_path = os.path.join(folderPath,ImageName)
img = cv2.imread(Image_path,0)
data[i,:] = img.flatten()
labels[i] = int(name)
i+=1
return data,labels
#************************************************************************************
def train_test_split(data,labels):
train_split_value = int(data.shape[0]*(5/10))
test_split_value = data.shape[0] - train_split_value
train_data = np.zeros((train_split_value,10304))
train_labels = np.zeros((train_split_value,1))
test_data = np.zeros((test_split_value,10304))
test_labels = np.zeros((test_split_value,1))
#odd rows for train data, even rows for test data
i_train=0
i_test=0
for i in range(data.shape[0]):
#even
if i%2==0:
test_data[i_test,:] = data[i]
test_labels[i_test] = labels[i]
i_test+=1
#odd
else:
train_data[i_train,:] = data[i]
train_labels[i_train] = labels[i]
i_train+=1
return train_data,train_labels,test_data,test_labels
#************************************************************************************
# compute the mean matrix for rach of the 40 classes
def compute_classes_mean_matrix(train_data,train_labels):
means = np.zeros((40,10304))
train_test_split_ratio = 5
for i in range(1,41):
temp = np.where(train_labels == i)[0]
temp_sum = np.zeros((1,10304))
for j in range (train_test_split_ratio):
temp_sum += train_data[temp[j],:]
means[i-1,:] = temp_sum / train_test_split_ratio
return means
#************************************************************************************
#the overall mean for all the 40 classes
#10304*1
def compute_overall_mean_matrix(classes_means):
temp_sum = np.zeros((1,10304))
for i in range(0,40):
temp_sum +=classes_means[i,:]
overall_mean = temp_sum / 40
return overall_mean.T
#************************************************************************************
#the matrix of the overall scatter between all the 40 classes
def compute_between_class_scatter_matrix(classes_means,overall_mean):
n=5
#10304*10304
Sb = np.zeros((classes_means.shape[1],classes_means.shape[1]))
for i in range(classes_means.shape[0]):
Sb = np.add(Sb,n* ((classes_means[i] - overall_mean) * (classes_means[i] - overall_mean).T))
return Sb
#************************************************************************************
def compute_center_class_matrix(train_data,train_labels,classes_means):
Z = np.zeros(train_data.shape)
for i in range(train_data.shape[0]):
Z[i,:] = train_data[i,:] - classes_means[int(train_labels[i])-1,:]
return Z
#************************************************************************************
def compute_class_scatter_matrix(Z):
S = np.zeros((10304,10304))
S = np.dot(Z.T,Z)
return S
#************************************************************************************
def data_dimencionality_reduction(train_data,test_data):
train_data_dimensionally_reductuted = np.zeros((200,40))
test_data_dimensionally_reductuted = np.zeros((200,40))
i=0
for img in train_data:
train_data_dimensionally_reductuted[i,:]=np.dot(img,eigen_vectors)
i+=1
i=0
for img in test_data:
test_data_dimensionally_reductuted[i,:] = np.dot(img,eigen_vectors)
i+=1
return train_data_dimensionally_reductuted,test_data_dimensionally_reductuted
#************************************************************************************
def plot_accuracy_graph(accuracy):
plt.figure(figsize=(12, 6))
plt.plot(range(1, 25), accuracy, color='red', linestyle='dashed', marker='o',
markerfacecolor='blue', markersize=10)
plt.ylim(50, 100)
plt.title('Accuracy for each K Value')
plt.xlabel('K Value')
plt.ylabel('Accuracy %')
#************************************************************************************
#************************************************************************************
if __name__ == '__main__':
data,labels = read_images()
print('Done images reading')
print('-----------------------------------------------------------')
train_data,train_labels,test_data,test_labels = train_test_split(data,labels)
print('Done Train Test Split')
print('-----------------------------------------------------------')
classes_means = compute_classes_mean_matrix(train_data,train_labels)
print('Done classes means computing')
print('-----------------------------------------------------------')
overall_mean = compute_overall_mean_matrix(classes_means)
print('Done overall mean computing')
print('-----------------------------------------------------------')
S_between = compute_between_class_scatter_matrix(classes_means,overall_mean)
print('Done between class scater matrix computing')
print('-----------------------------------------------------------')
Z = compute_center_class_matrix(train_data,train_labels,classes_means)
print('Done center class scatter matrix computing')
print('-----------------------------------------------------------')
S_classes = compute_class_scatter_matrix(Z)
print('Done within class scatter matrix computing')
print('-----------------------------------------------------------')
W_value = np.dot(np.linalg.inv(S_classes),S_between)
print('Done W = S^(-1)B computing')
print('-----------------------------------------------------------')
#40 largest eigen values
eigen_values,eigen_vectors = scipy.linalg.eigh(W_value,eigvals=((10304-40),(10304-1)))
print('Done eigen values and vectors computing')
print('-----------------------------------------------------------')
#reduce dimensionality of both train and test data sets
train_data_dimensionally_reductuted,test_data_dimensionally_reductuted = data_dimencionality_reduction(train_data,test_data)
accuracy = []
#Apply KNN
for i in range(1, 25):
classifier = KNeighborsClassifier(n_neighbors=i)
classifier.fit(train_data_dimensionally_reductuted, train_labels)
test_predict = classifier.predict(test_data_dimensionally_reductuted)
true_predicted_count=0
for j in range(0,200):
if test_predict[j] ==test_labels[j]:
true_predicted_count+=1
accuracy.append((true_predicted_count/200)*100)
#plot graph for different K values
plot_accuracy_graph(accuracy)
#************************************************************************************
#************************************************************************************