Haar Cascade OpenCV Object Detection
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.
Demo
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.
SAMPLES
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 IMAGES
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:
NEGATIVE IMAGES
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.
Needed preparing:
- 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
Closing
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.
I do not even understand how I ended up right here, however I assumed this put up was once great. I don’t understand who you are however definitely you’re going to a famous blogger in case you are not already 😉 Cheers!
Hi @Daniel Cole,
I am sure that I am not a really blogger 😉
I was researched and working in this domain, so I just write here for share with others something I interest.
Thanks,
Thanks for another informative website. Where else may I get that kind of information written in such an ideal means? I have a venture that I’m simply now operating on, and I have been on the glance out for such info.
Hi @Paula Green,
Thanks for your comment! This post is not enough clearly to have a look in general?
Sorry, I really tried to write clearly. But in case you dont understand yet, you can search this keys “Haar Cascade” and read more. You could also download a book for more detail. http://docs.opencv.org/3.1.0/d7/d8b/tutorial_py_face_detection.html#gsc.tab=0
Very nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again very soon!
Hi @Richard Fuller,
OK, I am trying to improve!
thanks,
I like the valuable info you provide within your articles.
I will bookmark your weblog and look again here regularly.
I am just quite sure I’ll learn plenty of new stuff on this site!
Best of luck for the following!
“Hi there! I’m at work surfing around your blog from my new iphone 4! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the great work!”
Hi there to every , because I am truly keen of reading this web
site’s post to be updated regularly. It carries good stuff.
A big thank you for your article post.Much thanks again. Awesome.
Really enjoyed this article.Thanks Again. Cool.