Haar Cascade

eLtronics villa
6 min readJun 5, 2019

--

Haar like feature are digital image features used in object recognition.Working with image RGB pixel value at each and every pixel of image made the task of feature calculation computationally expensive.It is better to use an alternate feature set based on Haar wavelets instead of the usual image intensities.

In the Viola–Jones object detection framework, the Haar-like features are therefore organized in something called a classifier cascade to form a strong learner or classifier.It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images for object detection.

For example, with a human face, it requires a lot of positive images (images of faces) and negative images (images without faces) to train the classifier. It is a common observation that among all faces the region of the eyes is darker than the region of the nose. Therefore a common Haar feature (shown in the below image - like convolutional kernel) for face detection.

It is a set of two adjacent rectangles that lie above the eye and the nose region. The position of these rectangles is defined relative to a detection window that acts like a bounding box to the face(object) . Each feature is a single value obtained by subtracting sum of pixels under the white rectangle from sum of pixels under the black rectangle.

Obviously, there will be errors or misclassifications. We select the features with minimum error rate, which means they are the features that most accurately classify the face and non-face images. (The process is not as simple as this. Each image is given an equal weight in the beginning. After each classification, weights of misclassified images are increased. Then the same process is done. New error rates are calculated. Also new weights. The process is continued until the required accuracy or error rate is achieved or the required number of features are found).

Because such a Haar-like feature is only a weak learner or classifier,a large number of Haar-like features are necessary to describe an object with sufficient accuracy.It is achieved by Adaboost technique. Instead of applying all 6000 features on a window, the features are grouped into different stages of classifiers and applied one-by-one. If a window fails the first stage, discard it. We don’t consider the remaining features on it. If it passes, apply the second stage of features and continue the process. The window which passes all stages is a face region.

Creating your own Haar Cascade

Installations:

< sudo apt-get update > # update your Ubuntu system

<mkdir haar_workspace > # create your workspace directory

<cd haar_workspace > # switch to your directory

< sudo apt-get install git > # install git

<git clone https://github.com/opencv/opencv.git> # clone the opencv

<sudo apt-get install build-essential > # install compiler

<sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev > # install required libraries

<sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394–22-dev > # install python bindings

<sudo apt-get install libopencv-dev> # install opencv development library

you can install python opencv using below command.

<sudo apt-get install python3-OpenCV>

Prepare data set

We will prepare data set of positive images and negative images,Positive images contains the object you want to find and negative image can be anything , except they cannot contain your object.Keep it small 50x50 or 20x20.

Download the positive and negative images using below link.

You can convert to grayscale and resize it.we can use simple counter to rename it.Create two folder name ‘pos’ and ‘neg’ and modify below function and run a python script file for this to generate .txt file.The set of negative samples must be prepared manually, whereas set of positive samples is created using the opencv_createsamples application. Note that negative samples and sample images are also called background samples or background images.Described images may be of different sizes. However, each image should be equal or larger than the desired training window size (which corresponds to the model dimensions, most of the times being the average size of your object), because these images are used to subsample a given negative image into several image samples having this training window size.

Positive Samples

Positive samples are created by the opencv_createsamples application. They are used by the boosting process to define what the model should actually look for when trying to find your objects of interest. The application supports two ways of generating a positive sample dataset.

  1. You can generate a bunch of positives from a single positive object image.
  2. You can supply all the positives yourself and only use the tool to cut them out, resize them and put them in the opencv needed binary format.

You can run below command to create positive sample based on your image pos_img.jpg.

opencv_createsamples -img Pos/pos_img.jpg -bg bg.txt -info info/info.lst -pngoutput info -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 1950

Based on the img we specify, bg is the background information, info where we will put the info.list output (which is a lot like the bg.txt file), then the -pngoutput is wherever we want to place the newly generated images. Finally, we have some optional parameters to make our original image a bit more dynamic and then =num for the number of samples we want to try to create.This info.lst file contains basically image name , number of image and co-ordinates of object (annotations) in the image.

Now that we have positive images, we now need to create the vector file, which is basically where we stitch all of our positive images together. We will actually be using opencv_createsamples again for this!

opencv_createsamples -info info/info.lst -num 1950 -w 20 -h 20 -vec positives.vec

we want to place the output somewhere, so let’s create a new data directory.You can prepare your folder structure like below.

Now let’s run the train command:

opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 1800 -numNeg 900 -numStages 10 -w 20 -h 20

Here, we say where we want the data to go, where the vector file is, where the background file is, how many positive images and negative images to use, how many stages, and the width and height. Note that we use significantly less numPos than we have.

nohup opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 1800 -numNeg 900 -numStages 10 -w 20 -h 20 &

This will allow the command to continue running, even after you close the terminal.

If you stopped it from running, you should have a bunch of stageX.xml files in your “data” directory. Open that up, see how many stages you did, and then you can run the opencv_traincascadeagain, with that number of stages, and you will be immediately given a cascade.xml file.

Evaluate your model

You can rename stageX.xml files to test your trained model performance (here I rename it to my_trained_haarcascade.xml),below is simple pseudo python code.You also put pre-trained cascade classifier to check the performance comparison of your model. haarcascade_frontalface_default.xml and haarcascade_eye.xml files should be there in your present working directory.

Note : To avoid long time for training , we can write text over the object ,rather than drawing a box. You can modify the above code as mentioned below.

cv2.putText(img,'my_object',(x-w,y-h), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (11,255,255), 2, cv2.LINE_AA)

Well, that’s all for now, you can also check here for c++ reference code.

Happy Learning 😍

--

--

eLtronics villa

Artificial Intelligence,Machine Learning,Deep Learning professsional