Training Haar-cascade


Training Haar-cascade

Haar-cascade is an object detection algorithm used to locate faces, pedestrians, objects and facial expressions in an image (Kumar R. & Bindu A.; 2006), and mainly used for face detection. In Haar-cascade, the system is provided with several numbers of positive images (like faces of different persons at different backgrounds) and negative images (images that are not faces but can be anything else like chair, table, wall, etc.), and the feature selection is done along with the classifier training using Adaboost and Integral images.

Features used by Haar-cascade

In general, three kinds of features are used in which the value of a two rectangular features is the difference sum of the pixels within two rectangular regions. These regions have same shape and size and are horizontally or vertically adjacent as shown in Fig 1. Where as in the three rectangular features are computed by taking the sum of two outside rectangles and then subtracted with the sum in a center rectangle. Moreover, in the four rectangles feature computes the difference between diagonal pairs of rectangles (Viola P. & Jones M.; 2001).

 

Fig 1: Example rectangle features use in Haar-cascade. The sum of pixels in the white rectangles is subtracted for the sum of the pixels in the grey rectangles. Here A and B are two rectangle feature, and C and D are three and four rectangle feature (Viola P. & Jones M.; 2001).

Learning Classification Functions

Classification learning process requires a set of positive and negative images for training and a set of features are selected using Ada-boost for training the classifier. To increase the learning performance of the algorithm (which is sometime called as weak learner), the Ada-boost algorithm is used. Ad-boost provides guarantees in several procedures.

The process of ‘Boosting’ works with the learning of single simple classifier and rewriting the weight of the data where errors were made with higher weights. Afterwards a second simple classifier is learned on the weighted classifier, and the data is reweighted on the combination of 1st and 2nd classifier and so on until the final classifier is learned. Therefore, the final classifier is the combination of all previous n-classifiers, which is shown in fig 2. The Ada-boost cascade of classifiers is one of the fastest and robust methods of detection and characterization, however, it presents some limitations on complex scenes especially those that changes shape (Sialat et al; 2009).

 

Fig 2: Ada-boost learning process

Face detection using Haar-cascade is based upon the training of a classifier using number of positive images that represent the object to be recognized and even large number of negative images that represent objects or feature not to be detected. However, OpenCV is already provided with the program for training a classifier to recognize any object, which is known as HaarTraining function.

Data Prepartion

First of all the utilities “createsamples.exe”, “haartraining.exe” and performance.exe” should be available with your OpenCV.

Positive Images

It is good to use real world positive samples, because of its advantage of having different reflections, illuminations and backgrounds. We can also obtain a cropped frontal face database from CMU PIE Database.

These positive samples will be stored in a folder “C:\images\positiveImages”. Then a “positive.txt” file have to be create that contains location of the image, number of positive samples present in that image and coordinates of all those samples in that image. Below is the sample of “positive.txt” file, which contain location of img1.jpg with 4 positive samples and img2.jpg and img3.jpg with 1 sample each. Where in third sample two 0 shows that x & y coordinates is 0 and 60 and 79 is the width and height of the sample.

positiveImages/img1.jpg 4 209 97 74 43 108 94 71 44 134 172 112 72 249 146 96 68
positiveImages/img2.jpg 1 322 173 107 65
positiveImages/img3.jpg 1 0 0 60 79

After that, we have to create “vec” file of these positive images using:

createsamples.exe -info positiveImages/positive.txt -vec positives.vec -num 1300 -w 20 –h 20

Where, 1300 is the number of positive samples that are used for training, and 20 will be the width and height of the positive samples. This vec file is created at “C:\images “, which you can see using:

createsamples.exe -vec positives.vec -w 20 –h 20

Negative Images

Negative images could be anything that does not contain positive samples, it can be anything like chair, table that helpful in differentiating detecting object from the world. A collection of 3000 negative samples is available for download at here.

Collect all negative images at “C:\images\negativeImages” and create “negatives.txt” file containing full path of all negative images. The easiest way of creating this is by using ‘command prompt’ and move to the folder “C:\images\negativeImages” and type “dir /b /s > negatives.txt”. It will create the file “negatives.txt”, then copy this file at ““C:\images” where “positives.vec” file is present.

Training

The training will be started by the following call:

haartraining.exe -data data/cascade -vec positives.vec -bg negatives.txt -npos 1300 -nneg 5350 -nstages 20 -mem 1300 -mode ALL -w 20 -h 20

Where:
-npos : is the number of positive images
-nneg : is the number of negative images
-nstages : is the number of Adaboost stages
-mem : is the memory allowed for processing, more the memory faster is the cascade preparation

For more information regarding training Haar-cascade refer [4] and [5].

References:

[1] Kumar R. & Bindu A. (2006) An Efficient Skin Illumination Compensation Model for Efficient Face Detection IEEE Industrial Electronics, IECON 2006 – 32nd Annual Conference 3444 – 3449

[2] Viola P. & Jones M (2001) Rapid Object Detection using a Boosted Cascade of Simple Features Computer Vision and Pattern Recognition Proceedings of the 2001 IEEE Computer Society Conference vol. 1: I-511 – I-518

[3] Sialat M., Khlifat N., Bremond F. & Hamrouni K. (2009) People detection in complex scene using a cascade of Boosted classifiers based on Haar-like-features Intelligent Vehicles Symposium, 2009 IEEE 83 – 87

[4] http://note.sonots.com/SciSoftware/haartraining.html#v6f077ba

[5]https://docs.google.com/viewer?a=v&q=cache:qoMNUx3vTQkJ:https://code.ros.org/trac/opencv/export/7138/branches/OpenCV4Tegra2.3.2/doc/opencv_user.pdf+&hl=en&gl=uk&pid=bl&srcid=ADGEESj_Kc5Xn7WLZ9onGtqXjNGgT77X4KUo_bh5ymqJDBTSpLBz2-4RxcBYpf1qQCIzUPWPY14PsaVemJp6J2hrWBmUVp460VD21tIjDzHE6DAgOfp8bwHVyQDsw_J3NJj5jHgaOgge&sig=AHIEtbQhqlPsHqf1CrIEpFqEIMhqr-THCg

5 Comments

  1. Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It will always be useful to read through articles from other writers and use something from their websites.

Leave a comment