项目作者: Andreluizfc

项目描述 :
AdaBoost + Haar Features Example
高级语言: C++
项目地址: git://github.com/Andreluizfc/OpenCV-Haar-AdaBoost.git
创建时间: 2017-06-14T19:35:40Z
项目社区:https://github.com/Andreluizfc/OpenCV-Haar-AdaBoost

开源协议:Other

下载


Train your own OpenCV Haar classifier

Important: This guide assumes you work with OpenCV 3.1

Instructions

  1. Install OpenCV & get OpenCV source
  1. Clone this repository
  1. Put your positive images in the ./positive_images folder and create a list
    of them:

    1. find ./positive_images -iname "*.jpg" > positives.txt
  2. Put the negative images in the ./negative_images folder and create a list of them:

    1. find ./negative_images -iname "*.jpg" > negatives.txt
  3. Create positive samples with the bin/createsamples.pl script and save them
    to the ./samples folder:

    1. perl bin/createsamples.pl positives.txt negatives.txt samples 1500\
    2. "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1\
    3. -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 80 -h 40"
  4. Use tools/mergevec.py to merge the samples in ./samples into one file:

    1. python ./tools/mergevec.py -v samples/ -o samples.vec

    Note: If you get the error struct.error: unpack requires a string argument of length 12
    then go into your samples directory and delete all files of length 0.

  5. Start training the classifier with opencv_traincascade, which comes with
    OpenCV, and save the results to ./classifier:

    1. opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\
    2. -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\
    3. -numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024\
    4. -precalcIdxBufSize 1024

    If you want to train it faster, configure feature type option with LBP:

    1. opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\
    2. -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\
    3. -numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024\
    4. -precalcIdxBufSize 1024 -featureType LBP

    After starting the training program it will print back its parameters and then start training. Each stage will print out some analysis as it is trained:

    1. ===== TRAINING 0-stage =====
    2. <BEGIN
    3. POS count : consumed 1000 : 1000
    4. NEG count : acceptanceRatio 600 : 1
    5. Precalculation time: 11
    6. +----+---------+---------+
    7. | N | HR | FA |
    8. +----+---------+---------+
    9. | 1| 1| 1|
    10. +----+---------+---------+
    11. | 2| 1| 1|
    12. +----+---------+---------+
    13. | 3| 1| 1|
    14. +----+---------+---------+
    15. | 4| 1| 1|
    16. +----+---------+---------+
    17. | 5| 1| 1|
    18. +----+---------+---------+
    19. | 6| 1| 1|
    20. +----+---------+---------+
    21. | 7| 1| 0.711667|
    22. +----+---------+---------+
    23. | 8| 1| 0.54|
    24. +----+---------+---------+
    25. | 9| 1| 0.305|
    26. +----+---------+---------+
    27. END>
    28. Training until now has taken 0 days 3 hours 19 minutes 16 seconds.

    Each row represents a feature that is being trained and contains some output about its HitRatio and FalseAlarm ratio. If a training stage only selects a few features (e.g. N = 2) then its possible something is wrong with your training data.

    At the end of each stage the classifier is saved to a file and the process can be stopped and restarted. This is useful if you are tweaking a machine/settings to optimize training speed.

  6. Wait until the process is finished (which takes a long time — a couple of days probably, depending on the computer you have and how big your images are).

  7. Use your finished classifier!

    1. cd ~/opencv-2.4.9/samples/c
    2. chmod +x build_all.sh
    3. ./build_all.sh
    4. ./facedetect --cascade="~/finished_classifier.xml"

Acknowledgements

A huge thanks goes to Naotoshi Seo, who wrote the mergevec.cpp and
createsamples.cpp tools and released them under the MIT licencse. His notes
on OpenCV Haar training were a huge help. Thank you, Naotoshi!