Hi I am new to python and implementing Viola-Jones face detection algorithm using python. While haar-feature selection using Adaboost, my code is taking extreme time (near about 18 hours) to select one haar-feature in each round of boosting. Somehow I find it out that the time taken to evaluate haar features is the most time consuming job in my code. I am presenting python code below:
List: list of integral images
(x,y,h,w,f,p): Haar feature at position (x,y). h and w are height and width of one rectangle in haar feature, which implies that haar feature of two rectangle ( |----|----|) have total height of h and total width of 2*w. f is haar feature type and p is parity.
def EvaluateHaar(List,(x,y,h,w,f,p)):
def Zero():
if x==0 and y==0:
bright = (i[x+h-1,y+w-1]+0)-(0+0)
dark = (i[x+2*h-1,y+w-1]+0)-(i[x+h-1,y+w-1]+0)
elif y==0:
bright = (i[x+h-1,y+w-1]+0)-(i[x-1,y+w-1]+0)
dark = (i[x+2*h-1,y+w-1]+0)-(i[x+h-1,y+w-1]+0)
elif x==0:
bright = (i[x+h-1,y+w-1]+0)-(0+i[x+h-1,y-1])
dark = (i[x+2*h-1,y+w-1]+i[x+h-1,y-1])-(i[x+h-1,y+w-1]+i[x+2*h-1,y-1])
else:
bright = (i[x+h-1,y+w-1]+i[x-1,y-1])-(i[x-1,y+w-1]+i[x+h-1,y-1])
dark = (i[x+2*h-1,y+w-1]+i[x+h-1,y-1])-(i[x+h-1,y+w-1]+i[x+2*h-1,y-1])
return bright, dark
def One():
if x==0 and y==0:
bright = (i[x+h-1,y+2*w-1]+0)-(0+i[x+h-1,y+w-1])
dark = (i[x+h-1,y+w-1]+0)-(0+0)
elif y==0:
bright = (i[x+h-1,y+2*w-1]+i[x-1,y+w-1])-(i[x-1,y+2*w-1]+i[x+h-1,y+w-1])
dark = (i[x+h-1,y+w-1]+0)-(i[x-1,y+w-1]+0)
elif x==0:
bright = (i[x+h-1,y+2*w-1]+0)-(0+i[x+h-1,y+w-1])
dark = (i[x+h-1,y+w-1]+0)-(0+i[x+h-1,y-1])
else:
bright = (i[x+h-1,y+2*w-1]+i[x-1,y+w-1])-(i[x-1,y+2*w-1]+i[x+h-1,y+w-1])
dark = (i[x+h-1,y+w-1]+i[x-1,y-1])-(i[x-1,y+w-1]+i[x+h-1,y-1])
return bright, dark
options = {0 : Zero,
1 : One,
}
R = []
append1 = R.append
for i in List:
bright,dark = options[f]()
if p == 1:
hf = (dark-bright)
else:
hf = (bright-dark)
append1(hf)
return R
Above code evaluates two haar feature type two-rectangle horizontal haar-feature and two-rectangle vertical haar feature.
Is there any fastest way to evaluate haar-features using python? Any suggestion to improve above code so that I can deal with timing factor. Here I am most concerning about timing factor than anything else.
Thanks!