OpenCV is an open-source image processing library originally created by Intel, and it powers a wide range of image processing projects.
Haar training is a set of procedures used to build detectors for objects such as faces and eyes. The wiki page below walks through creating a Haar classifier.
http://cgi.cse.unsw.edu.au/~cs4411/wiki/index.php?title=OpenCV_Guide
How to do Haar training in Windows 7
The stages of training and using a Haar classifier:
There is no definitive answer on how many images of each type you need. For Urban Challenge 2008 we used 1000 positive and 1000 negative images, while the earlier Grippered Bandit project used 5000. The Grippered Bandit classifier turned out to be considerably more accurate than ours, but training it took 3 weeks, compared with just 2 days for Urban Challenge 2008. Those numbers produced reasonable results, and testing classifier performance with different amounts of training images would make a worthwhile investigation for any future group.
Marking the images is a time consuming task. Get comfortable. The images in the directory provided to objectmarker are displayed one at a time. To mark an object, click with the mouse at the top left corner of the object and again at the bottom right corner, drawing a bounding box around the object. Take care to always start the bounding box at either the top left or bottom right corner. If you use the other two corners objectmarker will not write the coordinates to the output.txt file.
Press the Space Bar to confirm the rectangle. If you made a mistake drawing the rectangle, eg it was too small, just click to draw it again without pressing space. To save the marked objects and load the next image press Enter. We found it more convenient to change the key used to save and load the next image from Enter to B because B is closer to the 'Space Bar'. This can be changed in the source for ObjectMarker (ObjectMarker.cpp). When you are hand marking 1000+ images, this makes a big difference. Press Esc to close ObjectMarker and save the output.txt file.
Haar training is a set of procedures used to build detectors for objects such as faces and eyes. The wiki page below walks through creating a Haar classifier.
http://cgi.cse.unsw.edu.au/~cs4411/wiki/index.php?title=OpenCV_Guide
How to do Haar training in Windows 7
Haar Classifier
A Haar Classifier is a machine learning approach for visual object detection originally developed by Viola & Jones . It was originally intended for facial recognition but can be used for any object. The power of the Haar Classifier is that it will quickly reject regions that are highly unlikely to contain the object. It does this by making use of the cascade of classifiers. In this cascade, the early stages will quickly reject the majority of false regions and the object detection can move on to other regions. The later stages however require progressively more computational effort in order to reject the region. By doing this, the Haar Classifier will only spend substantial time on regions that are likely to contain the object being searched for.
OpenCV ships with the applications needed to implement a Haar classifier, and these can be used to train a detector for objects in an image. When this was written, the lab computers ran OpenCV 0.9.7. It is strongly advised to grab the latest version from Sourceforge, then extract and compile it inside your home directory.
The stages of training and using a Haar classifier:
- gather positive and negative training images
- annotate the positive images with objectmarker.cpp
- build a .vec file with createsamples.cpp
- train the classifier with haartraining.cpp
- run the trained classifier through cvHaarDetectObjects()
Gathering Training Images
As a rule of thumb, you need roughly 1000 images containing the object you intend to train for. For the Urban Challenge 2008 we drove the Pioneer robot around to capture images of a sign bearing the numeral 1 (see above for instructions on getting images from the robot). This set of positive images is what gets turned into the .vec file used to train the classifier. You must also provide a set of negative images, meaning images that do not contain the target object. Examples of both positive and negative images appear below.
Sample positive images used for Haar Classifier training |
Sample negative images used for Haar Classifier training |
There is no definitive answer on how many images of each type you need. For Urban Challenge 2008 we used 1000 positive and 1000 negative images, while the earlier Grippered Bandit project used 5000. The Grippered Bandit classifier turned out to be considerably more accurate than ours, but training it took 3 weeks, compared with just 2 days for Urban Challenge 2008. Those numbers produced reasonable results, and testing classifier performance with different amounts of training images would make a worthwhile investigation for any future group.
Annotating the Positive Images
In this step you produce a data file that records each image's file name along with the object's position inside it. The object marker utility generates this file. Compile the source code, then run it with the name of the text file that will receive the object location data and the path to your positive images, for example:
./ObjectMarker output.txt /home/group03/haarimages/sign1positive/
Marking the images is a time consuming task. Get comfortable. The images in the directory provided to objectmarker are displayed one at a time. To mark an object, click with the mouse at the top left corner of the object and again at the bottom right corner, drawing a bounding box around the object. Take care to always start the bounding box at either the top left or bottom right corner. If you use the other two corners objectmarker will not write the coordinates to the output.txt file.
Screenshot of the ObjectMarker utility |
Press the Space Bar to confirm the rectangle. If you made a mistake drawing the rectangle, eg it was too small, just click to draw it again without pressing space. To save the marked objects and load the next image press Enter. We found it more convenient to change the key used to save and load the next image from Enter to B because B is closer to the 'Space Bar'. This can be changed in the source for ObjectMarker (ObjectMarker.cpp). When you are hand marking 1000+ images, this makes a big difference. Press Esc to close ObjectMarker and save the output.txt file.
The output.txt file lists each image file name, followed by the number of objects marked in that image, the top left coordinate of the object's bounding rectangle, and then the rectangle's width and height, for example:
/home/group03/haarimages/sign1positive/image000001.jpg 2 35 178 35 26 73 112 19 17
/home/group03/haarimages/sign1positive/image000002.jpg 2 41 181 28 23 76 113 15 16
/home/group03/haarimages/sign1positive/image000003.jpg
/home/group03/haarimages/sign1positive/image000004.jpg 2 75 113 14 14 35 176 37 32
/home/group03/haarimages/sign1positive/image000005.jpg 2 73 111 19 20 33 179 34 26
/home/group03/haarimages/sign1positive/image000006.jpg 2 78 110 18 23 35 171 49 41
etc...
Building the Vector File
The marked images must be packed into a vector file, which is done with the createsamples utility (/opencv_library_install_path/opencv-1.0.0/bin), for example:
./opencv-createsamples -info /home/group03/haarimages/sign1positive/output.txt
-vec /home/group03/haarimages/sign1positive/positives.vec -w 24 -h 24
The -w and -h options define the sample width and height, the -info option points to the file produced by ObjectMarker, and the -vec option gives the name and location of the vector file that createsamples writes out.
Training the Classifier
Training is carried out with the opencv-haartraing utility (/opencv_library_install_path/opencv-1.0.0/bin/), for example:
$ opencv-haartraining -data /home/group03/trainout -vec /home/group03/haarimages/sign1positive/positives.vec -bg /home/group03/sign1_negative/negatives.txt -npos 1000 -nneg 1000 -nstages 20
Here the -data option sets where the trained classifier's .xml file is written, -vec is the vector file from the earlier createsamples step, -bg is a txt file listing the negative images, -npos is the count of positive images, -nneg is the count of negative images, and -nstages is how many classifier stages to train.
Give it a try.







13 comments:
is it possible to train with 100 positive and negative samples?
yes , of course. The time required to complete the haar training will depend on number of stages
Hi I am using openCV 2.4.3. How can I generate the createsamples.exe and haartraining.exe
Hi @Mostafiz, it can be already in the opencv folder when you build opencv or download the opencv installer binaries
hi when i train my image,the .xml file does not appear and the cmd stop at pos:5 5 1.000000 .Anything can help with?
Hi. I error "
*** 1 cluster ***
POS: 7 7 1.000000
Invalid background description file." please help me?
Hi,
Can you paste the command you give for training
PARAMETERS:
cascadeDirName: data
vecFileName: POS.vec
bgFileName: neg.txt
numPos: 69
numNeg: 114
numStages: 20
precalcValBufSize[Mb] : 512
precalcIdxBufSize[Mb] : 512
stageType: BOOST
featureType: LBP
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 69 : 69
Train dataset for temp stage can not be filled. Branch training terminated.
Cascade classifier can't be trained. Check the used training parameters.
Can you tell me what is the problem???.. please
Hey guys, I'm confused about the -w and -h requirements for createsamples. I have taken a fair bit of pictures of people and marked it using ObjectMarker. But I'm confused with the -w and -h options as the pictures I've taken (faces) have varying sizes due to the angle and distance from the len to the person.
What should I put in for -w and -h in this case?
Hi Stan
The following link helps you to know about opencv-create samples
http://man.cx/opencv_createsamples(1)
I'm trying to use 2500px x 2500px images for training but I can't find an object marker that supports images larger than monitor resolution (no zoom out in any of them that I can find). Any ideas? Thanks.
Hi! Great post! It helps me a lot. Thank you. I follow the steps that you show, but I have a problem. I have a positive sample vec of 2010 images and a negative data file with 2350 images. The original positive image size is 49x49.
I'm trying to train the cascade with the comand:
opencv_traincascade -data data -vec positive.vec -bg negative.txt -numPos 1900 -numNeg 2350 -numStages 20 -w 49 -h 49 -minhitrate 0.999 -maxFalseAlarmRate 0.5 -precalcValBufSize 22528 -precalcIdxBufSize 22528 -mode ALL
I choose 1900 for -numPos because read in many pages that the value of numPos have to be less than the number of the samples in vec. I also read that the number of negative images have to be greater that the numPos. However trying to run this training always throws me: Segmentation fault (core dumped). I'm working in a Centos 7 machine.
The only way it works is creating sample vec with -w 32 -h 32 size, and the training parameters are the following:
opencv_traincascade -data data -vec positive.vec -bg negative.txt -numPos 1578 -numNeg 1000 -numStages 20 -w 32 -h 32 -minhitrate 0.999 -maxFalseAlarmRate 0.5 -precalcValBufSize 25152 -precalcIdxBufSize 25152 -mode ALL
If I increase the number of numNeg or numPos it doesn't work. Also if I try to use the same amount of positive and negative images ( -numPos 1000 -numNeg 1000) or even only increase numNeg (-numPos 1500 -numNeg 2000). I also try to use less Stage number but I didn't work.
Can you help me to understand why different parameters don't work?
Thanks in advance.
Have a nice day!
Diana
i am making cascade classifier for bottle but not recognize whole bottle (top to bottom)
Post a Comment