Ask Your Question

Why is my haar classifier slow?

asked 2013-06-17 08:47:13 -0500

prerna1 gravatar image

updated 2013-06-18 09:24:06 -0500

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++ )
            cv::rectangle(frame, topleft, bottomright, Scalar(255,0,255), 1, 8, 0);


Could it be because of my dataset?

edit retag flag offensive close merge delete



Here is a well received answer on the topic

sammy gravatar imagesammy ( 2013-06-18 06:49:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-17 09:30:39 -0500

SR gravatar image

Try to decrease the image resolution.

edit flag offensive delete link more


By image resolution, what are you referring to? Can you be more specific please?

prerna1 gravatar imageprerna1 ( 2013-06-17 12:38:04 -0500 )edit

I know what image resolution is. But are you referring to the image resolution of the positives and negatives or are you referring to the image size specified while creating the vec file?

prerna1 gravatar imageprerna1 ( 2013-06-18 06:34:56 -0500 )edit

Sorry, I meant the resolution of those images you apply the detector to. If the image is large, the multi-scale sliding window search is slow because it depends quadratically on the image size (unless you don't change the step size between adjacent windows).

SR gravatar imageSR ( 2013-06-18 07:23:03 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-06-17 08:47:13 -0500

Seen: 713 times

Last updated: Jun 18 '13