-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaceDetector.py
282 lines (221 loc) · 7.31 KB
/
faceDetector.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import Image
import sys
import kneighbor
from hoc import hoc
from debug import *
from os import listdir
import os
import copy
from hog import hog
from skimage import data
from time import time
from random import choice
INITIAL_IMAGE_SIZE = 90
MAX_IMAGE_SIZE = 250
THRESHOLD_TO_WEIGHT_RECORD = 1.065
DEFAULT_THRESHOLD_TO_FACE = 0.89
RECORDS_GROUPS = 20.0 # Must be a float!
WINDOW_SIZE = 64
first_time = time()
def makeSquare(img, initPoint, winSize):
pix = img.load()
for t in range(winSize):
pix[initPoint[0]+t, initPoint[1]] = 255
pix[initPoint[0], initPoint[1]+t] = 255
pix[initPoint[0]+winSize-1, initPoint[1]+t] = 255
pix[initPoint[0]+t, initPoint[1]+winSize] = 255
class Model:
def __init__(self):
self.records = []
def record(self, value, descriptor):
weight = 0
#if len(self.records) > 0:
# near = kneighbor.nearNeighbor(descriptor, self.records)
# #print "Distance of nearest neighbor = "+str(near[2])
# if near[2] < THRESHOLD_TO_WEIGHT_RECORD and near[0] == value:
# #print "Weighting the existing image..."
# weight = near[3] + 1
# descriptor = near[1]
# self.records.remove((near[0],near[1],near[3]))
self.records.append((value, tuple(descriptor),weight))
def recordLearn(self, value, descriptor, weight):
self.records.append((value, tuple(descriptor), weight))
class FaceDetector(object):
classifier = None
descriptor = None
def callClassifier(self, imgVector, records):
return self.classifier(imgVector, records)
def callDescriptor(self, img):
return self.descriptor(img)
#isFace = 1 if image is a face
#isFace = 0, otherwise
def callTrain(self, imgVector, isFace):
model = Model()
for image in imgVector:
model.record(isFace, self.callDescriptor(image))
return model
# Project faces to the same image size
def normalizeFoundFaces(self, faces, img):
normFaces = []
for face in faces:
normX = img.size[0]*(float(face[0][0])/face[1][0])
normY = img.size[1]*(float(face[0][1])/face[1][1])
normWinSize = 64*(float(img.size[0])/face[1][0])
normFaces.append(((int(normX),int(normY)),normWinSize))
return normFaces
def drawFoundFaces(self, normFaces, img):
img = img.copy()
for face in normFaces:
makeSquare(img, face[0], int(face[1]))
img.show()
def callDetector(self, img, model, thresholdContinue = 0.2, thresholdFace = DEFAULT_THRESHOLD_TO_FACE):
# Faces list
faces = []
#xmask = [0] * img.size[1]
#mask = [xmask] * img.size[0]
# Dividing the record group in 3 groups (NEW)
print "Criando grupos de aprendizado..."
recordGroup = []
for i in range(int(RECORDS_GROUPS)):
recordGroup.append([])
for group in recordGroup:
for i in range(int(len(model.records)/RECORDS_GROUPS)):
x = choice(model.records)
group.append(x)
model.records.remove(x)
imgo = img
imgs = []
# Create != imgs sizes
if img.size[0]>img.size[1] and img.size[0]>INITIAL_IMAGE_SIZE:
for t in range(INITIAL_IMAGE_SIZE,MAX_IMAGE_SIZE,40): #was imgo.size[1]
tt = float(t)/imgo.size[1]
#print str(tt)
img = copy.copy(imgo)
img.thumbnail((int(imgo.size[0]*tt),int(imgo.size[1]*tt)), Image.ANTIALIAS)
imgs.append(img)
windowSize = WINDOW_SIZE
tupleRecords = tuple(model.records)
print "Quantidade de imgs="+str(len(imgs))
for tupleRecords in recordGroup:
tupleRecords = tuple(tupleRecords)
for img in imgs:
#img.show()
print "Analisando imagem de tamanho="+str(img.size)
# Sliding window
for i in range(0,img.size[1]-windowSize + 1,20):
for j in range(0, img.size[0]-windowSize + 1, 20):
#os.system("killall display")
#print "Analisando pixel="+str(j)+","+str(i)
#tempShow = img.copy()
#makeSquare(tempShow, (j, i), windowSize)
#tempShow.show()
#print "Starting pixel"
#print str(time()-first_time)
#print "Starting crop..."
# Crop the image with window
temp = img.crop((j,i,j+windowSize,i+windowSize))
#print str(time()-first_time)
#print "Calculate the prob..."
# Calculate the prob of face
#ft = time()
prob = self.callClassifier(tuple(self.callDescriptor(temp)), tupleRecords)
#print str(time()-ft)
# Compare with threshold
#print "Probabilidade="+str(prob)
if prob < thresholdContinue:
print "Anulando pixels..."
# If is a face
elif prob > thresholdFace:
print "uhul, its a faceee!!!!!!!!!!!!!"
print "at i="+str(i)+" and j="+str(j)
temp.show()
faces.append(((j,i,j+windowSize,i+windowSize),(img.size[0],img.size[1])))
self.drawFoundFaces(self.normalizeFoundFaces(faces, imgo),imgo)
raw_input("Pause...")
return faces
def readImagesInDir(self, path, isFace):
#files = listdir(path)
model = Model()
t=0
for dirname, dirnames, files in os.walk(path):
for f in files:
#t=t+1
#if t%100 == 0:
#print "Trained so far: "+str(t)
#print "Total vectors in database: "+str(len(model.records))
#img = data.load(os.getcwd()+"/"+str(os.path.join(dirname, f)))
img = Image.open(str(os.path.join(dirname, f)))
model.record(isFace, self.callDescriptor(img))
del img
print " Round="+str(len(model.records))
return model
def saveTrainedToDisk(self, path, trainedInstances):
myFile = open(path, "a")
for el in trainedInstances:
myFile.write(str(el[0]) + ":" + str(el[1]) + ":" + str(el[2]) + "\n")
myFile.close()
def loadTrainedFromDisk(self, path):
print "Abrindo arquivo de memoria..."
myFile = open(path)
mdl = Model()
face = 0
t=0
for el in myFile:
hist = []
face = int(el[0])
weight = int(el[-2])
for i in el[3:-4].split(','):
hist.append(int(i.strip(']')))
mdl.recordLearn(face, hist, weight)
t=t+1
myFile.close()
print "Terminado com "+str(t)+" imagens aprendidas!"
return mdl
def naiveBayes(imageVector, model, dist = 15):
nFace = 0
nNFace = 0
total = 0
# Calculates the number of faces and
# the number of images
for i in model.records:
total += 1
if i[0] == 1:
nFace += 1
# Prior probability
priorFace = float(nFace)/total
priorNFace = float(total-nFace)/total
distance = 0
def learn(path_nofaces = "imgs/nofaces/", path_faces = "imgs/faces/", path_learn = "acquired.ml"):
fd = FaceDetector()
fd.descriptor = hoc
faces = fd.readImagesInDir(path_faces, 1)
nfaces = fd.readImagesInDir(path_nofaces, 0)
#trained = fd.callTrain(faces[0], faces[1])
#trained2 = fd.callTrain(nfaces[0], nfaces[1])
fd.saveTrainedToDisk(path_learn, faces.records)
fd.saveTrainedToDisk(path_learn, nfaces.records)
print "Faces records="+str(len(faces.records))
print "No faces records="+str(len(nfaces.records))
print "Treinamento terminado com "+str(len(faces.records)+len(nfaces.records))+" imagens!"
def match(path_img, path_learn = "acquired.ml"):
fd = FaceDetector()
fd.classifier = kneighbor.kneighborCalc
fd.descriptor = hoc
model = fd.loadTrainedFromDisk(path_learn)
for i in fd.callDetector(Image.open(path_img), model):
i.show()
# classifier(hoc(Image.open("imgs/faces/1.jpg")), train(imgs, imgg))
# Parametros ideais:
if sys.argv[1] == "learn":
if len(sys.argv) > 2:
learn(sys.argv[2], sys.argv[3], sys.argv[4])
else:
learn()
else:
if sys.argv[1] == "match":
#argv[2] = path to image
#argv[3] = path to learnt file
if len(sys.argv) > 3:
match(sys.argv[2], sys.argv[3])
else:
match(sys.argv[2])