I trained a HAAR classifier to detect hands in a LIVE VIDEO FEED from the webcam. I used 621 positives & 3712 negatives.
I used opencv_createsamples to generate the vec file for positives: ./opencv_createsamples -vec /Users/.../positivesvec.vec -info /Users/.../positiveDesc.txt -w 20 -h 30
And then, I used opencv_traincascade to train the classifier: opencv_traincascade -data /Users/.../hand -vec /Users/.../positivesvec.vec -bg /Users/.../negativeDesc.txt -numPos 621 -numNeg 3712 -numStages 15 minHitRate 0.999 maxFalseAlarmRate 0.5 -w 20 -h 30 -mode ALL
The training took around 30 hours or so and I got an xml file. However, when I use that xml file for detection, it is really VERY slow (1 frame in 3-4 seconds maybe).
I know that my object detection code is correct because it works perfectly for faces. This is what I use:
trained_cascade_name = "/Users/.../cascade.xml";
if( !cascade.load( trained_cascade_name ) ){ qDebug()<<"Error loading cascade file!"; return; };
std::vector<Rect> hands;
Mat frame_gray; // Haar works on grayscale images
cvtColor(frame, frame_gray, CV_RGB2GRAY);
equalizeHist(frame_gray, frame_gray);
cascade.detectMultiScale( frame_gray, hands, 1.1, 3, 0|CV_HAAR_DO_CANNY_PRUNING|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(30,30),cv::Size(100,100));
CvPoint topleft, bottomright;
for( int i = 0; i < hands.size(); i++ )
{
topleft.x=hands[i].x;
topleft.y=hands[i].y;
bottomright.x=hands[i].x+hands[i].width;
bottomright.y=hands[i].y+hands[i].height;
cv::rectangle(frame, topleft, bottomright, Scalar(255,0,255), 1, 8, 0);
}