|
| 1 | +# Train your own OpenCV Haar classifier |
| 2 | + |
| 3 | +## Instructions |
| 4 | + |
| 5 | +1. Get OpenCV |
| 6 | + |
| 7 | + brew tap homebrew/science |
| 8 | + brew install --with-tbb opencv |
| 9 | + |
| 10 | +2. Clone this repository |
| 11 | + |
| 12 | + git clone https://github.com/mrnugget/opencv-haar-classifier-training |
| 13 | + |
| 14 | +3. Put your positive images in the `./positive_images` folder and create a list |
| 15 | +of them: |
| 16 | + |
| 17 | + find ./positive_images -iname "*.jpg" > positives.txt |
| 18 | + |
| 19 | +4. Put the negative images in the `./negative_images` folder and create a list of them: |
| 20 | + |
| 21 | + find ./negative_images -iname "*.jpg" > negatives.txt |
| 22 | + |
| 23 | +5. Create positive samples with the `bin/createsamples.pl` script and save them |
| 24 | +to the `./samples` folder: |
| 25 | + |
| 26 | + perl bin/createsamples.pl positives.txt negatives.txt samples 1500\ |
| 27 | + "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\ |
| 28 | + -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40" |
| 29 | + |
| 30 | +6. Compile the `mergevec.cpp` file in the `./src` directory: |
| 31 | + |
| 32 | + cp src/mergevec.cpp ~/opencv-2.4.6/apps/haartraining |
| 33 | + cd ~/opencv-2.4.6/apps/haartraining |
| 34 | + g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp\ |
| 35 | + cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp\ |
| 36 | + cvhaartraining.cpp |
| 37 | + |
| 38 | +7. Use the compiled executable `mergevec` to merge the samples in `./samples` |
| 39 | +into one file: |
| 40 | + |
| 41 | + find ./samples -name '*.vec' > samples.txt |
| 42 | + ./mergevec samples.txt samples.vec |
| 43 | + |
| 44 | +8. Start training the classifier with `opencv_traincascade`, which comes with |
| 45 | +OpenCV, and save the results to `./classifier`: |
| 46 | + |
| 47 | + opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\ |
| 48 | + -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\ |
| 49 | + -numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024\ |
| 50 | + -precalcIdxBufSize 1024 |
| 51 | + |
| 52 | +9. Wait until the process is finished (which takes a long time — a couple of |
| 53 | +days probably, depending on the computer you have and how big your images are). |
| 54 | + |
| 55 | +10. Use your finished classifier! |
| 56 | + |
| 57 | + cd ~/opencv-2.4.5/samples/c |
| 58 | + chmod +x build_all.sh |
| 59 | + ./build_all.sh |
| 60 | + ./facedetect --cascade="~/finished_classifier.xml" |
| 61 | + |
| 62 | + |
| 63 | +## Acknowledgements |
| 64 | + |
| 65 | +A huge thanks goes to Naotoshi Seo, who wrote the `mergevec.cpp` and |
| 66 | +`createsamples.cpp` tools and released them under the MIT licencse. His notes |
| 67 | +on OpenCV Haar training were a huge help. Thank you, Naotoshi! |
| 68 | + |
| 69 | +## References & Links: |
| 70 | + |
| 71 | +- [Naotoshi Seo - Tutorial: OpenCV haartraining (Rapid Object Detection With A Cascade of Boosted Classifiers Based on Haar-like Features)](http://note.sonots.com/SciSoftware/haartraining.html) |
| 72 | +- [Material for Naotoshi Seo's tutorial](https://code.google.com/p/tutorial-haartraining/) |
| 73 | +- [OpenCV Documentation - Cascade Classifier Training](http://docs.opencv.org/doc/user_guide/ug_traincascade.html) |
0 commit comments