This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vj2_1.py
377 lines (347 loc) · 18.9 KB
/
vj2_1.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import cv2
import math
#import numpy as np
frontDir = '/home/o/Desktop/Programming_Projects/Project_Onion/Onion_Excercises/Or_excercises/dataset_meir/'
posFile1 = open(frontDir + "croppedFrontFace/myPics.info", "r")
negFile1 = open(frontDir + "croppedNegative/bg.txt", "r")
kRows = 24
kCols = 24
haarValue=75000
class weakClassifier:
def __init__(self, features=[], alphaSum=0, thresh=0.5):
self.features=features
self.alphaSum=alphaSum
self.thresh=thresh
class haarFeature:
def __init__(self, fType, y, x, h, w,alpha):
self.fType=fType
self.y=y
self.x=x
self.h=h
self.w=w
self.alpha=alpha
class imageDB:
def __init__(self, img, numPositives, rows = 24, cols = 24, weight = 0.0):
self.img = img
self.numPositives = numPositives
self.rows = rows
self.cols = cols
self.weight = weight
def integralImage(self):
intImage = []
for i in range(self.rows):
if i != 0:
intImage.append([int(self.img[i][0]) + int(intImage[i - 1][0])])
else:
intImage.append([int(self.img[0][0])])
for j in range(1,self.cols):
if i != 0:
intImage[i].append(int(self.img[i][j]) + int(intImage[i][j - 1])+int(intImage[i - 1][j]) - int(intImage[i - 1][j - 1]))
else:
intImage[i].append(int(self.img[i][j]) + int(intImage[i][j - 1]))
return intImage
def calcRect(self,y,x,h,w):
# -1 from everything because if i am on 23 and my width is 1 than 23+1=24 and I am out of bounds. essentially width of one pixel is no width at all
bottomRight = self.img[y + h-1][x + w-1]
if y >= 1:
topRight = self.img[y - 1 -1][x + w-1]
if x >= 1:
topLeft = self.img[y - 1 -1][x - 1 -1]
bottomLeft = self.img[y + h -1][x - 1 -1]
else:
topLeft = 0
bottomLeft = 0
else:
topRight = 0
topLeft = 0
if x >= 1:
bottomLeft = self.img[y + h -1][x - 1 -1]
else:
bottomLeft = 0
return bottomRight - bottomLeft - topRight + topLeft
def testFeature(self,featureType,y,x,h,w):
if featureType == 1:
#White-Black horizontal
if abs(self.calcRect(y,x + w,h,w) - self.calcRect(y,x,h,w)) <= haarValue:
return 1
if featureType == 2:
#Black-White vertical
if abs(self.calcRect(y, x,h, w) - self.calcRect(y +h, x, h, w)) <= haarValue:
return 1
if featureType == 3:
#White-Black-White horizontal
if abs((- 1) * self.calcRect(y,x,h,w) + self.calcRect(y,x + w,h,w) - 1 * self.calcRect(y,x + w * 2,h,w)) <= haarValue:
return 1
if featureType == 4:
#White-Black-White-Black diagonal
if abs((- 1) * self.calcRect(y,x,h,w) + self.calcRect(y,x + w,h,w) + self.calcRect(y + h,x,h,w) - self.calcRect(y + h,x + w,h,w)) <= haarValue:
return 1
if featureType == 5:
#White-Black-White vertica
if abs((- 1 )* self.calcRect(y,x,h,w) + self.calcRect(y+h,x,h,w) - 1 * self.calcRect(y+h*2,x,h,w)) <= haarValue:
return 1
return 0
#This function accepts the amount of features to make a strong classifier from (T) and returns the classifier
def adaboost(T,images, currentCascade):
for t in range(T):
print "adaboosting feature: " + str (t)
#Normalizing the weights (weighing up for new feature stage)
wSum = 0.0
for img in images:
wSum += img.weight
for img in images:
img.weight = img.weight / wSum
def findFeature():
minError=2
minFeature=haarFeature(-1,-1,-1,-1,-1,-1)
# print str(minFeature.fType)+" "+str(minFeature.y)+" "+str(minFeature.x)+" "+str(minFeature.h)+" "+str(minFeature.w)
n = 0
# flag=True
# Features test
for y in range(kRows):
for x in range(kCols):
for h in range(1, kRows+1):
for w in range(1, kCols+1):
# if y==1 and x==0 and h==21 and w==11:
# print "9999999999999999999999 " + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
# if minFeature.fType==1 and minFeature.y==1 and minFeature.x==0 and minFeature.h==21 and minFeature.w==11 and flag:
# print "00000000000000000000000 " + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
# flag=False
f1 = True
f2 = True
f3 = True
f4 = True
f5 = True
for cascadeLevel in currentCascade:
for feature in cascadeLevel.features:
if feature.x==x and feature.y==y and feature.h==h and feature.w==w:
if feature.fType==1:
f1=False
# print "f1 "+str(f1)
if feature.fType==2:
f2=False
# print "f2 " + str(f2)
if feature.fType==3:
f3=False
# print "f3 " + str(f3)
if feature.fType==4:
f4=False
# print "f4 " + str(f4)
if feature.fType==5:
f5=False
# print "f5 " + str(f5)
# print "qqqqqqqq "+ str(feature.fType)+" "+str(feature.y)+" "+str(feature.x)+" "+str(feature.h)+" "+str(feature.w)
# print "wwwwwwww "+str(minFeature.fType) + " " + str(minFeature.y) + " " + str(minFeature.x) + " " + str(minFeature.h) + " " + str(minFeature.w)
if y+h<=kRows:
if w <= (kCols-x) / 2:
#Feature1 (White-Black horizontal)
# print "1111111111" + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
if f1:
# if y==1 and x==0 and h==21 and w==11:
# print "ggggggggggggg "+str(f1)+" "+str(y)+" "+str(x)+" "+str(h)+" "+str(w)
n+=1
fError = 0.0
for img in images:
yi = 0
if img.numPositives > 0:
yi = 1
fError += img.weight * abs(img.testFeature(1, y, x, h, w) - yi)
if fError < minError:
minError = fError
minFeature = haarFeature(1,y,x,h,w,math.log((1-minError)/minError))
# if y == 1 and x == 0 and h == 21 and w == 11:
# print "1111111111111111111111 " + str(f1) + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
if f3 and w <= (kCols - x) / 3:
# Feature3 (White-Black-White)
# print "33333333333" + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
n += 1
fError = 0.0
for img in images:
yi = 0
if img.numPositives > 0:
yi = 1
fError += img.weight * abs(img.testFeature(3, y, x, h, w) - yi)
if fError < minError:
minError = fError
minFeature = haarFeature(3, y, x, h, w, math.log((1 - minError) / minError))
# if y == 1 and x == 0 and h == 21 and w == 11:
# print "33333333333333333333 " + str(f1) + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
if x+w<=kCols:
if h <= (kRows-y)/2:
# Feature2 (Black-White vertical)
# print "2222222222"+str(y)+" "+str(x)+" "+str(h)+" "+str(w)
if f2:
n += 1
fError = 0.0
for img in images:
yi = 0
if img.numPositives > 0:
yi = 1
fError += img.weight * abs(img.testFeature(2, y, x, h, w) - yi)
if fError < minError:
minError = fError
minFeature = haarFeature(2, y, x, h, w,math.log((1-minError)/minError))
# if y == 1 and x == 0 and h == 21 and w == 11:
# print "222222222222222222222 " + str(f1) + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
if f5 and h <= (kRows - y) / 3:
# Feature5 (White-Black-White verticle)
# print "555555555555" + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
n += 1
fError = 0.0
for img in images:
yi = 0
if img.numPositives > 0:
yi = 1
fError += img.weight * abs(img.testFeature(5, y, x, h, w) - yi)
if fError < minError:
minError = fError
minFeature = haarFeature(5, y, x, h, w, math.log((1 - minError) / minError))
# if y == 1 and x == 0 and h == 21 and w == 11:
# print "55555555555555555 " + str(f1) + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
if f4 and h <= (kRows-y) / 2 and w <= (kCols - x) /2:
# Feature4 (White-Black-White-Black diagonal)
# print "444444444444" + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
n += 1
fError = 0.0
for img in images:
yi = 0
if img.numPositives > 0:
yi = 1
fError += img.weight * abs(img.testFeature(4, y, x, h, w) - yi)
if fError < minError:
minError = fError
minFeature = haarFeature(4, y, x, h, w,math.log((1-minError)/minError))
# if y == 1 and x == 0 and h == 21 and w == 11:
# print "444444444444444444444444 " + str(f1) + " " + str(y) + " " + str(x) + " " + str(h) + " " + str(w)
print n
# print "yyyyyyyyyyyyyyyy"+str(minFeature.fType) + " " + str(minFeature.y) + " " + str(minFeature.x) + " " + str(minFeature.h) + " " + str(minFeature.w)
return (minFeature,minError)
# if T==1:
# currentCascade.append([minFeature])
# else:
# currentCascade[-1].append(minFeature)
nextFeature=findFeature()
# print str(nextFeature[0].fType) + " " + str(nextFeature[0].y) + " " + str(nextFeature[0].x) + " " + str(nextFeature[0].h) + " " + str(nextFeature[0].w)
(currentCascade[-1].features).append(nextFeature[0])
#re-weigh
for img in images:
img.weight *= ((nextFeature[1]/(1-nextFeature[1]))**(1-img.testFeature(nextFeature[0].fType,nextFeature[0].y,nextFeature[0].x,nextFeature[0].h,nextFeature[0].w)))
return currentCascade
def createCascade(posFile,negFile,f,d,Ftarget,threshChange):
numPos = 0
numNeg = 0
for _ in posFile:
numPos += 1
for _ in negFile:
numNeg += 1
posFile.seek(0)
negFile.seek(0)
images = []
for line in posFile:
line = line.split()
img = cv2.imread(frontDir + "croppedFrontFace/" + line[0], 0)
curr = imageDB(img, 1, 24, 24, 1.0 / (2 * numPos))
curr.img=curr.integralImage()
images.append(curr)
posFile.seek(0)
for line in negFile:
line=line.strip()
img = cv2.imread(frontDir + "croppedNegative/" + line, 0)
curr = imageDB(img, 0, 24, 24, 1.0 / (2 * numNeg))
curr.img=curr.integralImage()
images.append(curr)
negFile.seek(0)
finalCascade=[]
F=1.0
D=1.0
n1=0
n2=0
while F>Ftarget:
newWeakClassifier = weakClassifier()
print newWeakClassifier.features
newWeakClassifier.features=[]
n1+=1
print "testing"
for cascadeLevel in finalCascade:
for feature in cascadeLevel.features:
print str(feature.fType) + " " + str(feature.y) + " " + str(feature.x) + " " + str(feature.h) + " " + str(feature.w)
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
finalCascade.append(newWeakClassifier)
FPrev = F
while F>f*FPrev:
DPrev = D
print "sending to adaboost"
for cascadeLevel in finalCascade:
for feature in cascadeLevel.features:
print str(feature.fType) +" "+str(feature.y) +" "+str(feature.x) +" "+str(feature.h) +" "+str(feature.w)
print "**********************"
finalCascade=adaboost(1,images, finalCascade)
print "got from adaboost"
for cascadeLevel in finalCascade:
for feature in cascadeLevel.features:
print str(feature.fType) + " " + str(feature.y) + " " + str(feature.x) + " " + str(feature.h) + " " + str(feature.w)
print "&&&&&&&&&&&&&&&&&&&&&&&"
n2+=1
FPrev2=F
while True:
F=FPrev2
D=DPrev
fi=0.0
di=0.0
for img in images:
result = 0
finalCascade[-1].alphaSum=0
for feature in finalCascade[-1].features:
result += feature.alpha * img.testFeature(feature.fType, feature.y, feature.x, feature.h, feature.w)
finalCascade[-1].alphaSum += feature.alpha
# print str(result) + " " + str(alphaSum * thresh) + " " + str(thresh) + " " + str(feature[3]) + " " + str(alphaSum) + " " + str(D) + " " + str(F) + " " + str(di) + " " + str(fi)
# print finalCascade[-1].alphaSum
# False positive
if result >= finalCascade[-1].thresh * finalCascade[-1].alphaSum and img.numPositives == 0:
fi += 1
# Real Positive
if result >= finalCascade[-1].thresh * finalCascade[-1].alphaSum and img.numPositives > 0:
di += 1
print "fi: " + str(fi)+" di: " +str(di)
fi /=numNeg
di /= numPos
F*=fi
D*=di
print "F:" + str(F) + " D: " + str(D) +" "+str(finalCascade[-1].thresh)+" "+str(d*DPrev)+ " "+ str(f*FPrev)+" "+str(FPrev)+" "+str(n1)+" "+str(n2)
if D > d * DPrev:
print "break"
break
finalCascade[-1].thresh*=threshChange
N=[]
if F>Ftarget:
for img in images:
if img.numPositives==0:
result=0
add=True
finalCascade[-1].alphaSum = 0
for cascadeLevel in finalCascade:
for feature in cascadeLevel.features:
result += feature.alpha * img.testFeature(feature.fType, feature.y, feature.x, feature.h,feature.w)
finalCascade[-1].alphaSum += feature.alpha
# False positive
if not(result >= cascadeLevel.thresh*finalCascade[-1].alphaSum):
add=False
break
if add:
N.append(img)
# finalCascade.pop(0)
return finalCascade
if __name__ == "__main__":
falsePositivRatePerLevel=0.4
truePPositiveRatePerLevel=0.98
falsePositiveTarget=0.00809
threshChangePerLevel=0.5
cascade=createCascade(posFile1,negFile1,falsePositivRatePerLevel,truePPositiveRatePerLevel,falsePositiveTarget,threshChangePerLevel)
with open(frontDir + "finalCascade.txt", "w") as cascadeFile:
for cascadeLevel in cascade:
cascadeFile.write(str(cascadeLevel.alphaSum)+" "+str(cascadeLevel.thresh)+" ")
for feature in cascadeLevel.features[0:-1]:
cascadeFile.write(str(feature.fType)+" "+str(feature.y)+" "+str(feature.x)+" "+str(feature.h)+" "+str(feature.w)+" "+str(feature.alpha)+" & ")
cascadeFile.write(str(cascadeLevel.features[-1].fType)+" "+str(cascadeLevel.features[-1].y)+" "+str(cascadeLevel.features[-1].x)+" "+str(cascadeLevel.features[-1].h)+" "+str(cascadeLevel.features[-1].w)+" "+str(cascadeLevel.features[-1].alpha)+"\n")
posFile1.close()
negFile1.close()