Skip to content

Instantly share code, notes, and snippets.

@mjcreativeventures
Created February 15, 2016 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjcreativeventures/2ac510d130f0d9245239 to your computer and use it in GitHub Desktop.
Save mjcreativeventures/2ac510d130f0d9245239 to your computer and use it in GitHub Desktop.
Train Classifier
#!/bin/sh
find traffic/positive/ -type f -name '*.jpg' > positives.dat
find traffic/negative/ -type f -name '*.jpg' > negatives.dat
[ -f collection.dat ] && rm -f collection.dat
# get width and height of each image, output to collection.dat file
for f in $(cat positives.dat); do
dim=$(identify "$f" | cut -d ' ' -f 3 | tr 'x' ' ')
echo "$f 1 0 0 $dim"
done > collection.dat
NUMPOS=$(wc -l collection.dat)
[ -f samples.vec ] && rm -f samples.vec
# create samples.vec file needed by classifier
opencv_createsamples -info collection.dat -bgcolor 0 -bgthresh 0 \
-vec samples.vec -num $NUMPOS -w 20 -h 20
# train classifier to detect positive images
# for precalculation you can set the amount of memory to use
# set sample size of positive images, 20 x 20 seems to work well
opencv_traincascade -data classifier -featureType HAAR -vec samples.vec \
-bg negatives.dat -numPos 600 -numNeg 470 -numStages 20 \
-precalcValBufSize 6000 -precalcIdxBufSize 6000 \
-minHitRate 0.999 -maxFalseAlarmRate 0.5 -mode ALL \
-w 20 -h 20
@gravesjohnr
Copy link

FYI, you need to have a count of neg and positive:
NUMNEG=$(wc -l negatives.dat)
Then opencv_traincascade can use these vars:
opencv_traincascade -data classifier -featureType HAAR -vec samples.vec
-bg negatives.dat -numPos $NUMPOS -numNeg $NUMNEG -numStages 20
-precalcValBufSize 6000 -precalcIdxBufSize 6000
-minHitRate 0.999 -maxFalseAlarmRate 0.5 -mode ALL
-w 20 -h 20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment