Tuesday, August 13, 2013

OpenCv face detection procedure and Explanation

Hi all, Hope you all are doing all. Today I will explain the face detection procedure used in opencv.


Procedure :


Step1: create cascaded classifier using the training algorithm provided by opencv and get the .xml file.

Step2: load the pre-made .xml file.

Step3: input frame from camera/ input image and convert it to grey scale image.

Step4: use opencv’s ‘CascadeClassifier:: detectMultiScale()’ function to detect faces of different sizes in the input image.


Explanation of CascadeClassifier::detectMultiScale() –


Parameters:



i. image - Matrix of the type CV_8U containing an image where objects are detected.
ii. objects – Vector of rectangles where each rectangle contains the detected object.
iii. scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
iv. minNeighbors – Parameter specifying how many neighbour's each candidate rectangle should have to retain it.
v. minSize – Minimum possible object size. Objects smaller than that are ignored.
vi. maxSize – Maximum possible object size. Objects larger than that are ignored.

       Basically what CascadeClassifier:: detectMultiScale()’ does is it takes the original image and creates an image pyramid from it, using the resize factor and searches for faces/objects in it. Image pyramid is a multi-scale representation of an image, such that the face detection can be scale-invariant, i.e., detecting large and small faces using the same detection window.

       This gives the ability of detecting faces/objects at a single model scale, throughout different images scales, meaning that if a detection happens at a specific layer, the  bounding box will be rescaled the same amount as the original image was to reach that pyramid layer.
       Using this technique you can detect multiple people scales at only a single model scale, which is computationally less expensive than training a model for each possible scale and running those over the single image.
       To make a good detector you need to train the cascade properly with a good number of sample images. Here is an example-








Tuesday, July 30, 2013

Traning Haar cascaded classifier with OpenCv

 Hello Friends,

I have never thought that, I will write a blog someday. But last 2 weeks I struggled a lot to find-out how to use Opencv's utility to train a cascaded classifier. I could not find a single tutorial talking about the newest applications of OpenCV to train cascade classifier i.e opencv_traincascade. All the blogs and tutorials I found only talks about the older version which is opencv_haartraining.

Currently there are two applications in OpenCV to train cascade classifier: opencv_haartraining and opencv_traincascade. opencv_haartraining is now an obsolete application, and also you can find many tutorials talking about it so I will only talk about  opencv_traincascade.

A.  First thing first, Install Opencv. Once you have installed OpenCV look under  your opencv/apps folder you can see two folders "haartraining" and "traincascade". We will use "traincascade" folder for training. And We will use the "createsamples.cpp" from the "haartraining" folder to create the positive samples. I will explain it step by step.


B. First we will make a createsamples.exe by building the .cpp files using the Visual Studio 2010. To do so, follow the below steps-
     a. open Visual Studio 2010 -> new project ->project name(createsample)->empty Project->finish
     b. In the right hand in Solution Explorer->Header Files, right click on Header Files and add exiting files -  "_cvcommon.h" , "_cvhaartraining.h" , "cvclassifier.h", "cvhaartraining.h".
     c. Right click on  Solution Explorer->Source Files, And add existing files - "createsamples.cpp", "cvboost.cpp" , "cvcommon.cpp", cvhaarclassifier.cpp", "cvhaartraining.cpp", "cvsamples.cpp".
     d. Add all the include folders, library folder and additional dependencies in the Project property and build the solution.
      f. You can find the createsamples.exe in you project debug folder.

C.Similarly we will make traincascade.exe. For traincasacde.exe we will add "boost.h" , "cascadeclassifier.h", "haarfeature.h", imagestorage.h" , Ibpfeatures.h", "traincascade_features.h" in the "Heade Files" and in the "Source Files" we will add "boost.cpp", "cascadeclassifier.cpp", "features.cpp", "haarfeatures.cpp", "imagesstorage.cpp", "lbpfeatures.cpp", "traincascade.cpp".

D. Now that we have we have createsample.exe and traincascade.exewe can start the main process.
    a.Create a folder in a fresh path say, C:\haar
    b.Create 2 folders inside C:\haar i. Positive, ii. Negative
    c.Positive Images: These images contain the object to be detected. Inside the positive folder keep all the positive images. Then create a ‘positive.txt’ file which will have the location of the image, number of positive samples present in that images and coordinate of all those samples in the image. Below is the example of ‘positive.txt’ , which contain location of the image positive1, number of positive sample in each image, x & y coordinate of the sample and height and width of the image.

Positive\poisitive1.jpg 1 0 0 92 116
Positive\poisitive2.jpg 1 0 0 92 116
...

    d.After having the positive images and the information text file we will create a “vec” file. During haartraining positive samples should have different height and width , So original positive images are resized and packed as thumbs to vec file. Vec file has header: number of positive samples, width, height and contain positive thumbs in body.
To create the vec file we will use the command prompt(cmd) and type the follow-

    C:\haar\createsamples.exe –info Positive\positive.txt –vec positive.vec –num 100 –w 24 –h 24

Where 100 is the number of positive samples that are used for training and 24 will be the height and width of the positive samples. This ‘vec file’ will be created at “c:\haar” we can see it by typing the following in the cmd

    C:\haar\createsamples.exe –vec positives.ec –w 24 –h 24

    e.Negative Images: Negative images could be anything that does not contain positive samples. Keep all the negative images in “C:\haar\Negative” and create a ‘negative.txt’ file containing full paths of all negative images. something like this-

Negative\negative1.jpg
Negative\negative1.jpg

...E. Now that we have all the things we need to train a cascade classifier we can call the traincascade.exe from the command prompt. To start training you need to type the following command-

    C:\haar\traincascade.exe -data result/ -info positive.vec -bg negative.txt -numPos 2000 -numNeg 3000 -numStages 20 -w 24 -h 24

Where '-data' specifies the directory where the the result will be saves as a .xml file. The final result of the training will be saved in "cascade.xml" file , you can remove all other files inside the result folder. '-info' specifies the information file of the positive images i. e the .vec file. '-bg' specifies the background file that is the text file having information about the negative images. '-numPos' means the number of positive images and '-numNeg' menas the number of negative images. '-numStages' means the number of stages should be used for the training process. Once the training process starts you can see some thing like this-
Be patient , the whole process might take 4-5 days to complete.