-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceClustering.py
256 lines (208 loc) · 9.69 KB
/
FaceClustering.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import cv2, os, math, sys
from sys import maxint
import numpy as np
import scipy as sp
from sklearn.preprocessing import normalize
import networkx as nx
import matplotlib.pyplot as plt
from scipy.spatial.distance import *
import scipy.ndimage as nd
from sklearn.cluster import *
from sklearn.preprocessing import MinMaxScaler
from scipy.stats import *
from numpy import *
from sklearn.metrics import *
from scipy.stats.mstats import zscore
from PIL import Image
import shutil
def faceCluster():
#data collection
def getFiles():
print('getting files')
imagesTitles = list()
images = list()
rootdir = os.path.dirname(os.path.abspath(__file__))
print("Rootdir " + str(rootdir))
# for subdir, dirs, files in os.walk(rootdir):
# # print(str(relDir) + " relDir")
# # os.chdir(rootdir)
# for file in files:
# # relDir = os.path.relpath(subdir, rootdir)
# f = os.path.join(subdir, file)
# if '.jpg' in f:
# imagesTitles.append(file)
path = '/Users/artrial/desktop/face_detect/face_clustering/FaceImageClustering/DataSet'
for filename in os.listdir(os.getcwd()+"/DataSet/"):
print(str(filename))
filename1 = os.getcwd()+"/DataSet/"+str(filename)
if '.jpg' in filename1:
imagesTitles.append(filename1)
# for filename in glob.glob(os.path.join(path, '*.jpg')):
print(imagesTitles)
imagesTitles.sort(key=str.lower)
for title in imagesTitles:
#print(title)
#raw_input()
images.append(cv2.imread(title, 0))
return images
#return resize(normalize_intensity(images))
def getFaces(images):
print('getting faces')
faceCascade = cv2.CascadeClassifier('classifier/haarcascade_frontalface_default.xml')
profileCascade = cv2.CascadeClassifier('classifier/haarcascade_profileface.xml')
faces = list()
for image in images:
#gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
frontalFaces = faceCascade.detectMultiScale(image, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE)
profileFaces = profileCascade.detectMultiScale(image, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE)
#print "Found {0} faces!".format(len(frontalFaces) + len(profileFaces))
for (x, y, w, h) in frontalFaces:
faces.append(np.array(image[y:y+h, x:x+w]))
for (x, y, w, h) in profileFaces:
faces.append(np.array(image[y:y+h, x:x+w]))
return normalize_intensity(resize(faces))
def resize(images, size=(100, 100)):
print('resizing')
images_norm = []
for image in images:
if image.shape < size:
image_norm = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
else:
image_norm = cv2.resize(image, size, interpolation=cv2.INTER_CUBIC)
images_norm.append(np.array(image_norm))
return images_norm
#data pre-processing
def normalize_intensity(images):
#https://github.com/rragundez/PyData/blob/master/face_recognition_system/operations.py
print('normalizing')
images_norm = []
for image in images:
is_color = len(image.shape) == 3
if is_color:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
images_norm.append(np.array(cv2.equalizeHist(image)))
# print(str(images_norm) + "84")
return images_norm
def createFaceMatrix(faces, faceSize=(100, 100)):
numPixels = faceSize[0]*faceSize[1]
faceMatrix = np.matrix(np.zeros((numPixels, len(faces)), dtype=np.float))
for i in range(0, len(faces)):
faceMatrix[:, i] = np.matrix(faces[i].reshape((numPixels, 1)))
return faceMatrix
def eigenFaces(faceMatrix):
print('computing eigenFaces')
if len(faces) == 0:
return faces
avg = np.mean(faceMatrix, axis=1)
A = faceMatrix - avg
values, vectors = np.linalg.eig(np.dot(A.T, A))
indices = np.argsort(values)
indices = indices[::-1]
k = int(math.floor(np.matrix(indices).shape[1]*0.2))
if k == 0:
indices = indices[0]
else:
indices = indices[:k]
eigenfaces = normalize(np.dot(A, vectors), axis=0, norm='l1')
eigenfaces = np.matrix(eigenfaces[:, indices])
eigenfaces = np.matrix(normalize(np.dot(eigenfaces.T, A), axis=0, norm='l1'))
return eigenfaces
def similarity(weights):
print('computing similarity matrix')
numRows = (weights.shape)[0]
numCols = (weights.shape)[1]
similarityMatrix = np.zeros((numCols, numCols), dtype=np.float)
for i in range(0, numCols):
for j in range(0, numCols):
similarityMatrix[i,j] = sp.spatial.distance.cosine(weights[:,i], weights[:,j])#np.power(np.linalg.norm(weights[:, i] - weights[:, j]), 2)
similarityMatrix = 1-(MinMaxScaler().fit_transform(similarityMatrix))
#similarityMatrix = kneighbors_graph(similarityMatrix, 5, mode='connectivity', include_self=True)
return similarityMatrix#similarityMatrix
#data post-processing
def groupLabels(clusterLabels):
indices = dict()
for i in range(0, len(clusterLabels)):
if clusterLabels[i] not in indices:
indices[clusterLabels[i]] = [i]
else:
indices[clusterLabels[i]].append(i)
return indices
def showFaces(faces, labels):
indices = groupLabels(labels)
for k,v in indices.iteritems():
# print(str(v) + "149")
for i in v:
# cv2.imshow('img', faces[i])
raw_input("cluster " + str(k))
def graphSubClusters(faces, similarityMatrix, labels):
for k,v in groupLabels(labels).iteritems():
# print('cluster {0}'.format(k) + " 155 cluster")
nx.draw(nx.from_numpy_matrix(np.matrix(similarityMatrix)[v, :][:, v]))
# plt.show()
def cleanClusters(faces, similarityMatrix, labels):
#first remove outlying clusters
indices = groupLabels(labels)
inter_cluster_variances = list()
toRemove = list()
for k,v in indices.iteritems():
print('cluster {0}'.format(k))
inter_cluster_variances.append(sum(sum(np.power(similarityMatrix[:, v][v, :], 2), 1))/(len(v)-1))
inter_cluster_zscores = zscore(inter_cluster_variances)
toRemove_inter_cluster = list()
for index in range(0, len(inter_cluster_zscores)):
if inter_cluster_zscores[index] <= (-1) or inter_cluster_zscores[index] >= 1:
toRemove_inter_cluster.append(index)
for i in toRemove_inter_cluster:
toRemove.extend(indices.pop(i, None))
similarityMatrix = np.delete(similarityMatrix, toRemove, 0)
similarityMatrix = np.delete(similarityMatrix, toRemove, 1)
labels = np.delete(labels, toRemove)
faces = np.delete(np.array(faces), toRemove, 0)
print(inter_cluster_zscores, toRemove_inter_cluster)
#them remove the individual images
silhouetteSamples = zscore(silhouette_samples(similarityMatrix, labels, metric='precomputed'))
print(silhouetteSamples)
below = (silhouetteSamples <= (-1))
above = (silhouetteSamples >= 1)
toRemove = list()
for index in range(0, len(below)):
if below[index]:
toRemove.append(index)
for index in range(0, len(above)):
if above[index]:
toRemove.append(index)
toRemove.sort()
print('toRemove', toRemove)
similarityMatrix = np.delete(similarityMatrix, toRemove, 0)
similarityMatrix = np.delete(similarityMatrix, toRemove, 1)
labels = np.delete(labels, toRemove)
faces = np.delete(np.array(faces), toRemove, 0)
return (faces, similarityMatrix, labels)
#writing data to files
def saveToFile(faces, labels, pretense='original'):
for k,v in groupLabels(labels).iteritems():
# print('cluster {0}'.format(k) + "clusters")
if not os.path.exists("results/cluster{0}".format(k)):
os.makedirs("results/cluster{0}".format(k))
for i in v:
# print(str(faces[i]))
Image.fromarray(faces[i]).save('/Users/artrial/desktop/face_detect/face_clustering/FaceImageClustering/results/cluster{0}/cluster{1}_{2}_{3}.jpg'.format(k,k,i, pretense))
if os.path.exists("results"):
shutil.rmtree("results")
os.mkdir('results')
faces = getFaces(getFiles())
faceMatrix = createFaceMatrix(faces)
eigfaces = eigenFaces(faceMatrix)
similarityMatrix = similarity(eigfaces)
labels = AffinityPropagation().fit_predict(similarityMatrix)
nx.draw(nx.from_numpy_matrix(similarityMatrix))
# plt.show()
# graphSubClusters(faces, similarityMatrix, labels)
# showFaces(faces, labels)
saveToFile(faces, labels)
(faces, similarityMatrix, labels) = cleanClusters(faces, similarityMatrix, labels)
nx.draw(nx.from_numpy_matrix(similarityMatrix))
# plt.show()
# graphSubClusters(faces, similarityMatrix, labels)
# showFaces(faces, labels)
saveToFile(faces, labels, pretense='cleaned')