Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to train OpenCV cascade classifier

📅 2014-Sep-01 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

OpenCV ships with an application that can be used to train a cascade classifier. The steps to prepare your data and train the classifier can be quite elaborate. I have detailed the steps that I used below to train the classifier to identify an object (say car):

$ opencv_createsamples -info obj-rects.txt -w 50 -h 50 -vec pos-samples.vec

Here, obj-rects.txt is a text file that has the information of the rectangles where the object is located in each image. See step above for details. The output of this program is stored in pos-samples.vec.

$ opencv_createsamples -vec pos-samples.vec -w 50 -h 50

Note that it switches to viewing mode when you only provide these three parameters and their values should match what you provided to create the positive samples.

$ opencv_traincascade -data obj-classifier -vec pos-samples.vec -bg neg-filepaths.txt -precalcValBufSize 2048 -precalcIdxBufSize 2048 -numPos 200 -numNeg 2000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave

obj-classifier is a directory where we are asking the classifier files to be stored. Note that this directory should already be created by you. pos-samples.vec is the file we generated in step above. neg-filepaths.txt is a file with list of paths to negative sample files. 2048 is the amount of MB of memory we are requesting the program to use. The more the memory, the faster the training. 200 is the number of positive samples in pos-samples.vec. This number is also reported by opencv_createsamples when it finishes its execution. 2000 is the number of negative sample image paths we have specified in neg-filepaths.txt. 20 is the number of stages in the classifier we wish. 50x50 is the size of object in these images. This should be same as what was specified with opencv_createsamples. 0.999 and 0.5 are self-explanatory. Details on all these parameters are found in the documentation.

Related: See my tips and other posts on using OpenCV Cascade Classifier.

Tried with: OpenCV 2.4.9 and Ubuntu 14.04