Showing posts with label haar-like features. Show all posts
Showing posts with label haar-like features. Show all posts

Tuesday, 4 March 2014

Clearing the haar

Hi folks,
             In this edition of the blog I will be writing about a unique feature of openCV, haar classifier training. This methodology allows us to train machines to recognize any object of our choice by learning features of the object. Though the method is pretty easy to execute and will end up with good results, the process is extremely time consuming. For our project, we are interested in getting the machine to recognize human palm. This will be useful for us to define gestures through continuous detection. After the previous post on Introduction to openCV, where we have explored many possibilities for hand gesture detection, we settled on using haar cascade training method.

Now how do we train a machine and make it self learn to recognize a particular object? This sure sounds very intriguing. This can be achieved by telling the machine how the object looks like and what the object does NOT look like and by also by teaching it about any special features of the object that stands out. Now once the machine knows  the features of the object, this machine trained data can be used on the input images to check the presence of the object.

As mentioned earlier, our intention is to detect human palm. Hence we collected around 80 positive images, all cropped to the same ratio(0.64) and around 1000 negative images. Its important that we crop all the positive images to the same ratio and also they must contain only the object of interest preferably with different background and lighting. Negative images must not contain the object of interest. More the number of image samples, better the results! (Takes longer time to train too!). Once we had the images, we were all set to start the training process. The steps we followed is derived from this blog which proved to be extremely helpful. The width and height  used for training was 64 and 100 respectively. Here are a few examples of positive images

 

and negative images included everything(well almost!) that is not human palm.

Steps to get training started:

Assuming that openCV is installed, the necessary scripts can be found here :
     git clone https://github.com/Srip/HaarTraining.git

Below are the steps for training :
1. Organize the data
    Save all the positive images in a directory, say positive_images and all the negative images in negative_images.

2. Once we have the images, all cropped to the same ratio lets save the relative paths of all images to a file.

    find ./positive_images -iname "*.jpg" >postives.txt
    find ./negative_images -iname "*.jpg" > negatives.txt

3. Next step is to create samples from the existing positive images (2000 samples for our cascade classifier).  For this I will be using a perl script found in this blog and also a tool from openCV "opencv_createsamples".Both these scripts can be found in the repository mentioned earlier.

    mkdir samples
    perl createsamples.pl positives.txt negatives.txt samples 2000 "opencv_createsamples -bgcolor 0\
    -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 64 -h 100"

4. Once the samples are created, we will need to merge these samples into a single *.vec file. For this purpose, we use the mergevec.cpp file.

    cp mergevec.cpp ~/opencv-2.4.6.1/apps/haartraining
    cd ~/opencv-2.4.6.1/apps/haartraining
    g++ -I. -o mergevec mergevec.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp\
    cvhaarclassifier.cpp cvhaartraining.cpp `pkg-config --libs --cflags opencv`

5. Once the execution is done, take the executable to the directory where all the scripts and images are stored. This can be used to merge all the *.vec files generated in the samples directory.
 
    find ./samples -name '*.vec' > samples.txt
    ./mergevec samples.txt samples.vec

6. Now for the final training command. Opencv_traincascade is the tool used to training the
machine to detect objects. The meaning of the various arguments in the command can be
understood here. Looking at what the parameter does, appropriate values can be set.

    mkdir classifier
    opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20\
    -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1500 -numNeg 992 -w 64 -h 100\
    -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 3072

The width and height parameters should have the same value as used when creating samples. Now this is going to take ALOT (about a week!) of time to execute and the final result will be in the form of an xml document. Since an xml file is generated for each stage of the classifier and later it is combined to give a single file, it is called as cascade classifier. A great advantage of this is that the program can be stopped at any point and on restarting the training continues from the last stopped stage.

Conclusion:

  The training process took around 9 days to complete . The results are not bad, but the rate of false positives is quite high. Hence we have decided the train the machine for the third time with more positive and negative images. As for the results, here is a screenshot.


Until next time.


Useful links:

Complete blog for haar training can be found here and here
Important documentation of openCV on  cascade-classifier and cascade classifier training.



Wednesday, 12 February 2014

Diving into OpenCv for Object Detection

OpenCV- Vision to computers O_O So shall we say that computers are new humans?


Could Computers and human interact through Gestures? Indeed yes and in fact, in many ways. So we thought of simple hand movements to get responses from computer.
Check few interesting tutorial here which we went through.

Object Detection Using OpenCv

So the next question that arises is HOW? How to detect object(hand)?

*Motion detection through Background subtraction technique.By the difference in frames motion could be detected. This difference can be used to get the shape of object in motion. This has few disadvantages and mainly cannot detect object in its static position




*Disparity mapping
Disparity refers to the difference in location of an object in corresponding two  images as seen by the left and right eye which is created due to parallax  of stereo cameras .This technique is very useful to detect the object depth, like hand which will be closest to camera during gestures. A very useful and highly accurate and may be the next big thing in Computer-Vision with cameras being replaced with kinects and other such powerful devices. However this technique is not possible with the web-camera presently available with the computers.



*Haar-Cascade classifiers
This idea, proposed by Viola and Jones, is used to rapidly detect any object, like human faces, eyes, hands and many more using AdaBoost classifier cascades that are based on Haar-like features and not pixels. This was the stepping stone in face detection. A haar-like feature contains a detection window. This window contains rectangular regions at specific locations. The pixel intensities are summed up in each region and the difference between these sums are calculated. This difference is used to detect subsections of the image. The target window is then moved over the input image and for each subsection and haar-like feature is calculated. This difference is then compared with the learned value that seperates the objects from non-objects.



Implementation of Haarcascades


Before continuing, we are talking about just the built-in camera and not Kinect or Stereo Camera or any other external sources.

We worked on the above mentioned ideas to get to know them well, may not be completely, but let us tell you it was indeed very helpful.As I already mentioned haar gives better accuracy and its the built-in camera that is being used. So we are building our own haar-like feature for hand and using it to detect hand(open palm).
By using the technique of Detection and Tracking, simple gestures can be defined and corresponding action can be taken. There are many other techniques available for detection of objects like based on color and lot more to explore and learn. Tracking can be done using Meanshift, Camshift and other such techniques which will be explained in the forthcoming posts.

Stay in touch folks cause we will be shortly telling how to make your own haar and about tracking techniques.
Until then see-ya.