1

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!

user2766019
  • 577
  • 4
  • 7
  • 20
  • 2
    Use [Scipy](http://docs.scipy.org/doc/scipy/reference/) and [Numpy](http://docs.scipy.org/doc/numpy/reference/) to improve performance. Also, you can use [OpenCV](https://opencv-python-tutroals.readthedocs.org/en/latest/index.html) to perform [face detection](https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html#haar-cascade-detection-in-opencv) – Vincent Oct 10 '13 at 14:40
  • `def EvaluateHaar(List,(x,y,h,w,f,p))` ARGH! Tuple-unpacking in function signatures is deprecated(and removed in python3!). Avoid that! – Bakuriu Oct 10 '13 at 17:59
  • @Bakuriu: What does this mean can you please elaborate it? – user2766019 Oct 12 '13 at 13:55
  • @user2766019 I mean that you should do `def EvaluateHaar(List, x_y_h_w_f_p): x,y,h,w,f,p = x_y_h_w_f-p`. In python3 the definition you gave would raise a `SyntaxError`. – Bakuriu Oct 12 '13 at 14:31
  • @Bakuriu: Hi, Let me make sure, you mean I should use 'underscore' instead of 'comma' for haar feature definition. Is that so? I am using python 2.7, and the definition works fine for me, while using this x_y_h_w_f-p gives me the error for invalid syntax. – user2766019 Oct 12 '13 at 15:56
  • 1
    @user2766019 The `-p` was a typo, it should have been another underscore: `_p`. Actually it could have been `def EvaluateHaar(List, a): x, y, h, w, f, p = a`. The important thing is: [*tuple unpacking inside a function signature is a deprecated feature*](http://www.python.org/dev/peps/pep-3113/). – Bakuriu Oct 12 '13 at 16:02
  • Why do you say it is slow? How many Haar-like features are you using to evaluate the image? – Ramiro Oct 14 '13 at 13:17
  • @Ramiro: I am using 1,62,336 haar-features and near about 8000 images of size 24*24. – user2766019 Oct 14 '13 at 15:40
  • @user2766019 The whole detector has 162,336 classifiers? Did you implement a cascade detector or a monolithic one? And how long is it taking? – Ramiro Oct 15 '13 at 02:18
  • @Ramiro: I am going to implement cascade detector. But in the training process I need to calculate the threshold of each feature in each round which one I feel is a time consuming job. with a multiprocessing stuff I can select one Haar feature in each round with minimum of 2.5 hours which is long enough. – user2766019 Oct 15 '13 at 07:36

0 Answers0