• Stars
    star
    729
  • Rank 60,302 (Top 2 %)
  • Language
    Perl
  • License
    Other
  • Created almost 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Learn how to train your own OpenCV Haar classifier

Train your own OpenCV Haar classifier

Important: This guide assumes you work with OpenCV 2.4.x. Since I no longer work with OpenCV, and don't have the time to keep up with changes and fixes, this guide is unmaintained. Pull requests will be merged of course, and if someone else wants commit access, feel free to ask!

This repository aims to provide tools and information on training your own OpenCV Haar classifier. Use it in conjunction with this blog post: Train your own OpenCV Haar classifier.

Instructions

  1. Install OpenCV & get OpenCV source

     brew tap homebrew/science
     brew install --with-tbb opencv
     wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
     unzip opencv-2.4.9.zip
    
  2. Clone this repository

     git clone https://github.com/mrnugget/opencv-haar-classifier-training
    
  3. Put your positive images in the ./positive_images folder and create a list of them:

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

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

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

     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.

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

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

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

      opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\
       -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\
       -numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024\
       -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:

    ===== TRAINING 0-stage =====
    <BEGIN
    POS count : consumed   1000 : 1000
    NEG count : acceptanceRatio    600 : 1
    Precalculation time: 11
    +----+---------+---------+
    |  N |    HR   |    FA   |
    +----+---------+---------+
    |   1|        1|        1|
    +----+---------+---------+
    |   2|        1|        1|
    +----+---------+---------+
    |   3|        1|        1|
    +----+---------+---------+
    |   4|        1|        1|
    +----+---------+---------+
    |   5|        1|        1|
    +----+---------+---------+
    |   6|        1|        1|
    +----+---------+---------+
    |   7|        1| 0.711667|
    +----+---------+---------+
    |   8|        1|     0.54|
    +----+---------+---------+
    |   9|        1|    0.305|
    +----+---------+---------+
    END>
    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.

  8. 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).

  9. Use your finished classifier!

     cd ~/opencv-2.4.9/samples/c
     chmod +x build_all.sh
     ./build_all.sh
     ./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!

References & Links:

More Repositories

1

fzz

fzz makes your command line interactive!
Go
185
star
2

vimconfig

My ~/.vim folder
Vim Script
81
star
3

scheme_x86

Writing a Scheme to x86 compiler by following Abdulaziz Ghuloum's "An Incremental Approach to Compiler Construction"
Scheme
63
star
4

dotfiles

My dotfiles, including configuration for zsh, tmux, psql
Shell
59
star
5

watchgopher

Watch a directory for changes
Go
54
star
6

helles

A prototype of a web server, using master-worker architecture, written in C.
C
40
star
7

virtual_brainfuck_machine

Companion repository for a blog post about Brainfuck, interpreters and VMs
Go
36
star
8

monkeylang

Official website for Monkey
SCSS
22
star
9

sceem

A small, simple and incomplete Lisp/Scheme interpreter written in Ruby
Ruby
12
star
10

data_structures

Implementations of common data structures in C
C
10
star
11

risp

A playground on which I hack on an incredibly tiny Lisp interpreter to learn Rust
Rust
8
star
12

windowcenter

GNOME extension to resize window to ~80% and center
JavaScript
7
star
13

tree-sitter-tucan

Tree-sitter parser for Tucan
C
6
star
14

tree-sitter-tucanir

Tree-sitter parser for Tucan IR
C
6
star
15

anygood

Was the movie any good? Check the ratings from different sites!
Ruby
5
star
16

ziggy

Zig playground
Zig
5
star
17

arm64-experiments

Just trying to see what it takes to cross-compile ARM64 assembly on x86-64 Linux and macOS
Makefile
3
star
18

sicp

My solutions (and attempts thereof) to the SICP exercises
Racket
3
star
19

vim-fzz

Use fzz and the_silver_searcher in Vim as an interactive search
Vim Script
2
star
20

pg_hello_world

pg_hello_world - A small example extension for PostgreSQL, written in C
C
1
star
21

emacs.d

Emacs configuration of a Vim lover
Emacs Lisp
1
star
22

non-native-fullscreen

Experiment, trying to get non-native fullscreen mode to work in an app
Swift
1
star
23

micktagger

Command line tool to tag files written in ruby
Ruby
1
star
24

testing-nextjs

Testing Next.js on Netlify, trying to figure out why prefetch doesn't work
TypeScript
1
star