Subscribe:

Ads 468x60px

Sunday, August 7, 2011

How to do OpenCV Haar training ..

                        Opencv is an image processing library made by Intel .It is an opensource library and it can be used for many image processing projects


                            Haar training is a set of procedures for doing  detections like face,eye etc..I am attaching the wiki page for doing 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.
The necessary applications for implementing a Haar classifier are included in OpenCV and these can be used to train a classifier for detecting objects in an image. At the time of writing, the version of OpenCV installed on the lab computers was 0.9.7. It is highly recommended to download the latest version from Sourceforge then extract and compile this into your home directory.


Steps in training and using a Haar classifier:
  • collect positive and negative training images
  • markup positive images using objectmarker.cpp
  • create a .vec file using createsamples.cpp
  • train the classifier using haartraining.cpp
  • run the classifier using cvHaarDetectObjects()

Collect Training Images

Generally you will need around 1000 images that contain the object you want to train for. When we did this for the Urban Challenge 2008 we used the Pioneer robot to collect images of a sign with the numeral 1 (see above for instructions on getting images from the robot. It is this collection of positive images that will be used to create the .vec file used to train the classifier. It is also necessary to supply the training with a number of negative images, i.e. ones that do not contain the object being trained for. Examples of positive and negative images are shown below.

Sign1 pos 4x1.jpg
Examples of positive images used to train Haar Classifier

Negative images 4x1.jpg
Examples of negative images used to train Haar Classifier


It is unclear exactly how many of each kind of image are needed. For Urban Challenge 2008 we used 1000 positive and 1000 negative images whereas the previous project Grippered Bandit used 5000. The result for the Grippered Bandit project was that their classifier was much more accurate than ours. However, it took 3 weeks to train the classifier for Grippered Bandit, whereas it only took 2 days to train for Urban Challenge 2008. These numbers gave reasonable results, however it would be a great investigation for any future group to test the performance of the classifier using varying numbers of training images.

Mark Positive Images

This step creates a data file containing the file name and the location of the object in the image. The data file is created using the object marker utility. Compile the source code and then run, giving the name of the text file to write the object location data into and the path where your positive images are located, eg:

./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.
Objectmarker screenshot.png
Screen-Shot of 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 will be a list of the image file names followed by the number of objects marked in the image, the coordinate of the top left corner of the rectangle bounding the object, then the width and height of the rectangle, eg,

/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...

Create Vector File

The marked images need to be packed into a vector file. The createsamples (/opencv_library_install_path/opencv-1.0.0/bin) utility is used to do this, eg:

./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 set the sample width and height respectively, the info option specifies the file made using ObjectMarker, and the -vec option specifies the name and location for the vector file output by the createsamples utility.

Train Classifier

The classifier is trained with the utility opencv-haartraing (/opencv_library_install_path/opencv-1.0.0/bin/), eg:

$ 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

Where the -data option specifies where to place the .xml file which is the trained classifier, -vec is our vector file from the previous createsamples step, -bg is a txt file containing a listing of the negative images, -npos is the number of positive images, -nneg is the number of negative images, and nstages is the number of classifier stages to train.

Try it........

13 comments:

Anonymous said...

is it possible to train with 100 positive and negative samples?

Lentin Joseph said...

yes , of course. The time required to complete the haar training will depend on number of stages

Mostafiz Mehebuba Hossain said...

Hi I am using openCV 2.4.3. How can I generate the createsamples.exe and haartraining.exe

Lentin Joseph said...

Hi @Mostafiz, it can be already in the opencv folder when you build opencv or download the opencv installer binaries

Unknown said...

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?

Lê Đức Sơn said...

Hi. I error "
*** 1 cluster ***
POS: 7 7 1.000000
Invalid background description file." please help me?

Lentin Joseph said...

Hi,

Can you paste the command you give for training

Unknown said...

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

Stan said...

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?

Lentin Joseph said...

Hi Stan
The following link helps you to know about opencv-create samples

http://man.cx/opencv_createsamples(1)

Unknown said...

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.

Anonymous said...

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

Unknown said...

i am making cascade classifier for bottle but not recognize whole bottle (top to bottom)

Post a Comment