-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognition.py
200 lines (169 loc) · 6.83 KB
/
face_recognition.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
from commonfunctions import *
IMAGE_DIR = 'data'
import json
DEFAULT_SIZE = [200, 200]
def read_images_test(image_path=IMAGE_DIR, default_size=DEFAULT_SIZE,data="test_set"):
images = []
images_names = []
image_names = [image for image in os.listdir(image_path) if not image.startswith('.')]
for image_name in image_names:
image = Image.open (os.path.join(image_path, image_name))
image = image.convert ("L")
# resize to given size (if given )
if (default_size is not None ):
image = image.resize (default_size , Image.ANTIALIAS )
images.append(np.asarray (image , dtype =np. uint8 ))
#image_name_ = image_name.partition('.')[0]
if data=="test_set":
image_name_=image_name.split(".")[0]
else:
image_name_ = image_name[:3]
images_names.append(image_name_)
#images_names = list(dict.fromkeys(images_names))
images = np.array(images)
return [images,images_names]
def read_images(image_path=IMAGE_DIR, default_size=DEFAULT_SIZE):
images = []
images_names = []
image_dirs = [image for image in os.listdir(image_path) if not image.startswith('.')]
for image_dir in image_dirs:
print(image_dir)
dir_path = os.path.join(image_path, image_dir)
image_names = [image for image in os.listdir(dir_path) if not image.startswith('.')]
for image_name in image_names:
image = Image.open (os.path.join(dir_path, image_name))
image = image.convert ("L")
# resize to given size (if given )
if (default_size is not None ):
image = image.resize (default_size , Image.ANTIALIAS )
images.append(np.asarray (image , dtype =np. uint8 ))
images_names.append(image_dir)
return [images,images_names]
def as_row_matrix (X):
print('inside Reshaping...')
if len (X) == 0:
return np. array ([])
mat = np. empty ((0 , X [0].size ), dtype =X [0]. dtype )
for row in X:
mat = np.vstack(( mat , np.asarray( row ).reshape(1 , -1))) # 1 x r*c
return mat
def get_number_of_components_to_preserve_variance(eigenvalues, variance=.95):
for ii, eigen_value_cumsum in enumerate(np.cumsum(eigenvalues) / np.sum(eigenvalues)):
if eigen_value_cumsum > variance:
return ii
def pca (X, y, num_components =0):
# n : samples , d : dimension of each sample as a row
print("starting PCA.....")
[n,d] = X.shape
if ( num_components <= 0) or ( num_components >n):
num_components = n
mu = X.mean( axis =0)
X = X - mu
if n>d:
C = np.dot(X.T,X) # Covariance Matrix
[ eigenvalues , eigenvectors ] = np.linalg.eigh(C)
else :
C = np.dot (X,X.T) # Covariance Matrix
[ eigenvalues , eigenvectors ] = np.linalg.eigh(C)
eigenvectors = np.dot(X.T, eigenvectors )
for i in range (n):
print(i)
eigenvectors [:,i] = eigenvectors [:,i]/ np.linalg.norm( eigenvectors [:,i])
# sort eigenvectors descending by their eigenvalue
print('Outside PCA')
idx = np.argsort (- eigenvalues )
eigenvalues = eigenvalues [idx ]
eigenvectors = eigenvectors [:, idx ]
num_components = get_number_of_components_to_preserve_variance(eigenvalues)
# select only num_components
eigenvalues = eigenvalues [0: num_components ].copy ()
eigenvectors = eigenvectors [: ,0: num_components ].copy ()
return [ eigenvalues , eigenvectors , mu]
def dist_metric(p,q):
p = np.asarray(p).flatten()
q = np.asarray (q).flatten()
return np.sqrt (np.sum (np. power ((p-q) ,2)))
def predict (W, mu , projections, y, X):
minDist = float("inf")
minClass = -1
Q = np.dot (X.reshape (1 , -1) - mu , W)
print("P",len(projections))
for i in range (len(projections)):
dist = dist_metric( projections[i], Q)
if dist < minDist:
minDist = dist
minClass = i
return minClass
def subplot ( title , images , rows , cols , sptitle ="", sptitles =[] , colormap = plt.cm.gray, filename = None, figsize = (10, 10) ):
fig = plt.figure(figsize = figsize)
# main title
fig.text (.5 , .95 , title , horizontalalignment ="center")
for i in range ( len ( images )):
ax0 = fig.add_subplot( rows , cols ,( i +1))
plt.setp ( ax0.get_xticklabels() , visible = False )
plt.setp ( ax0.get_yticklabels() , visible = False )
if len ( sptitles ) == len ( images ):
plt.title("%s #%s" % ( sptitle , str ( sptitles [i ]) ) )
else:
plt.title("%s #%d" % ( sptitle , (i +1) ) )
plt.imshow(np.asarray(images[i]) , cmap = colormap )
if filename is None :
plt.show()
else:
fig.savefig( filename )
def train_pca():
[X, y] = read_images()
X_rows = as_row_matrix(X)
[eigenvalues, eigenvectors, mu] = pca(X_rows, y, 15)
#print(y)
jsonobject = json.dumps({'eigenvectors': eigenvectors.tolist(), 'mu': mu.tolist()}, indent=4)
#print(jsonobject)
#jsonFile = open("pca.json", "w")
with open("pca.json", "w") as file:
file.write(jsonobject)
file.close()
def non_max_suppression_fast(boxes, overlapThresh=0.7):
# if there are no boxes, return an empty list
if len(boxes) == 0:
return []
# if the bounding boxes integers, convert them to floats --
# this is important since we'll be doing a bunch of divisions
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
# initialize the list of picked indexes
pick = []
# grab the coordinates of the bounding boxes
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
# compute the area of the bounding boxes and sort the bounding
# boxes by the bottom-right y-coordinate of the bounding box
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
# keep looping while some indexes still remain in the indexes
# list
while len(idxs) > 0:
# grab the last index in the indexes list and add the
# index value to the list of picked indexes
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
# find the largest (x, y) coordinates for the start of
# the bounding box and the smallest (x, y) coordinates
# for the end of the bounding box
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
# compute the width and height of the bounding box
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
# compute the ratio of overlap
overlap = (w * h) / area[idxs[:last]]
# delete all indexes from the index list that have
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))
# return only the bounding boxes that were picked using the
# integer data type
return boxes[pick[0]].astype("int")