Skip to content

Instantly share code, notes, and snippets.

@dan-silver
Last active July 17, 2017 22:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dan-silver/747c562140bd1638a308 to your computer and use it in GitHub Desktop.
Save dan-silver/747c562140bd1638a308 to your computer and use it in GitHub Desktop.
Generating Haar Classifiers

####Resources

####Goals

  • Parameter tuning
  • Logical code analysis
  • Algorithm enhancements

####Generating negative samples

  • To generate tons of negative samples fast, download a YouTube video (http://www.clipconverter.cc/) and grab frames with the command ffmpeg -i file.mp4 -r 2 %03d.jpg in Ubuntu
  • Use mogrify to resize the images to a maximum width and height mogrify -resize 256x256 *.jpg
  • Convert all pngs to jpegs mogrify -format jpg *.png

Tips

  • You can escape and continue the opencv_traincascade program. It will load the previously finished stage and continue. This is useful since you can modify the buffer size while training the classifier.
  • To monitor memory usage, use the free -mh command, or watch -n1 free -mh to refresh it every 1s.

Optimization thoughts

  • What are the exact conditions before a stage is finished? Is time wasted after maxFalseAlarm is hit before the next stage starts?

Bugs Found

  • The total time elapsed counter is way off. It's showing over 4 hours so far, and it's on stage 4 after 50 minutes.

Common Errors

  • OpenCV Error: Assertion failed (_img.rows * _img.cols == vecSize) in get... Delete the params xml file from the previous cascade and try again! If the command line params don't match, it may be looking for files that don't exist.

Commands

Training the classifier with a vector file

opencv_traincascade -data classifier -vec positives.vec -bg negatives.txt\
  -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 52\
  -numNeg 600 -w 24 -h 24 -mode ALL -precalcValBufSize 1024\
  -precalcIdxBufSize 1024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment