Today, you can see your face getting recognized by android phone or anywhere. That’s very cool! But recognizing of faces in images or in video is also talking every days. So let we talk now about detecting and recognizing somethings of our choice using haar cascade opencv.
In the first word, I think you need let time to watch the tutorial after reading this post for easier to understand. Because this task takes a lot of time for me to learn. And that is why I am hurry to write this post.
And this page is an open source computer vision library.
Starting with a green housing:
Preparation of Hardware and Software
- Ubuntu 14.04: Link Ubuntu Installation.
- Installation of OpenCV 2.4.11: Link OpenCV Installation.
- Installation of C++ library, make, etc: Link development library installation.
Train our own OpenCV Haar Cascade classifier
Firstly, we talk about some theories.
To train our own classifier, we need many samples. A sample is the training data that contains positive sample and negative sample. In particular, a positive simple contains a lot of images that show the object we want to detect. And a negative contains images without object. In addition, the numbers of samples depend on a variety of factors, including the quality of the images, the object you want to recognize, the method to generate the samples, etc. For example, we have success with 2000 samples created using 100 positive images and 135 negative images with createsamples tool of OpenCV.
Positive image contain the object to recognize which is made by crop object’s part of the large image. So we need get positive images different angle, side, etc. But in our project, we only recognizes the object from the front view.
Some positive samples:
The negative image don’t contain the object to recognize. The negative image maybe created by crop the part which don’t contain the object. However, for easier and faster, we can take an image without any object.
Some negative samples:
Preparing data by OpenCV’s tool
You can manually create the training data. Or using other program like “object locator”. However, in this post, we will talk about using OpenCV’s tool.
- Positive images folder: positive
- Negative images folder: negative
- Clone the reference code: https://github.com/Booppey/haarcascade.git
After that, you could create your “samples.txt” file:
cd to workspace find positive -iname "*.png" > positives.txt find negative -iname "*.png" > negatives.txt perl code_ref/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 60 -h 60" find samples -name '*.vec' > samples.txt
Then create your “samples.vec” used to train the detector.
cp opencv-haar-classifier-training/src/mergevec.cpp ~/opencv-2.4.11/apps/haartraining cd ~/opencv-2.4.11/apps/haartraining g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp cvhaartraining.cpp cvsamplesoutput.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgproc -lopencv_features2d -lopencv_flann -lopencv_nonfree -lopencv_calib3d -lopencv_objdetect cd your_workspace_name cp ~/opencv-2.4.11/apps/haartraining/mergevec . ./mergevec samples.txt samples.vec
Training the classifier
Now train our detector using samples.vec by typing on terminal:
opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1500 -numNeg 135 -w 60 -h 60 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048
Using our own classifier for object detection
After the training, the detector can understand what is the object will be detected by the cascade.xml file. So now you need to use this file to detection. Finally, check our github for see the source code for this task.
Check our github
git clone https://github.com/Booppey/haarcascade.git
Haar Cascade is a method simple and effective for detect 1 type of object at time. But this approach can detect only one object. If you want to detect multi type of object, you can also use other method like Bag of Visual Word (BoVW) using SIFT/SURF, KMEAN, SVM. Those kinds of method not only detect 1 object, but its also detect multiple objects.