Cascade Classifer Is Very Slow

asked 2017-01-26 01:46:48 -0500

trav gravatar image

updated 2017-01-26 01:49:15 -0500

Hello, I'm trying to make my code more efficent in context of faster FPS but seems I am getting around 35 fps or so... very slow indeed. Can someone please tell me what I have done wrong with this code that is bottlenecking it? I don't have much experience with OpenCV any help appreciated. Code will compile and execution below is from top to bottom, thanks.

//

 called in main class first func
void start_camera::load_face_files(){
    // Load the cascades will trip if path is not correct
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade from open cv file\n");
  cout<<" \n ERROR: FILE PATH"<<face_cascade_name<<" \n NOT FOUND!"<<endl; // print error if face does not load
    };

    entry_func();
}


void start_camera::entry_func(){
  cap.open(0);

              // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    // open selected camera using selected APi
    cap.open(0,apiID);
    // check if we succeeded

    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";

    }
    cout << "Start grabbing" << endl
    << "Press any key to terminate" << endl;

    while(cap.read(frame)){

        if (frame.empty()) {
           cerr << "ERROR! blank frame grabbed\n";
        break;
       }

        imshow("live",frame);

        cam_start(frame);

        if (waitKey(1) >= 0)
            //break;
            cout<<"";
    }

}



void start_camera::cam_start(Mat frame){
     std::vector<Rect> faces;
    Mat frame_gray;
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);  // Convert to gray scale
    equalizeHist(frame_gray, frame_gray);       // Equalize histogram

    // Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30));

    // Iterate over all of the faces
    for( size_t i = 0; i < faces.size(); i++ ) {

        // Find center of faces
        Point center(faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2);

        // Draw ellipse around face
        ellipse(frame, center, Size(faces[i].width/2, faces[i].height/2),
                0, 0, 360, Scalar( 255, 0, 0 ), 4, 8, 0 );
    }

    imshow(window_name, frame);  // Display frame
}
edit retag flag offensive close merge delete

Comments

Perhaps it is too slow for you because you are asking it to do too much given your hardware. In that case, try reducing image resolution and limiting the scale range of the sliding window. You could also reduce the image to a ROI by some other means (skin color detection?) and then run the detector on the ROI, though that seems a little hacky to me. Or, if you really think it is slow and you could do better, then optimize the OpenCV source and make a pull request so all can benefit from your insights.

Der Luftmensch gravatar imageDer Luftmensch ( 2017-01-26 09:27:40 -0500 )edit

Hmm, I would like to think my 2013 mac air can keep up with the demands. Would have to do further investigating but good suggestion.

trav gravatar imagetrav ( 2017-01-26 13:09:26 -0500 )edit

Why is it "slow"? Compared to what? Compared to what you want? Why is that the universal standard of speed? Maybe your 'question' should be a question and not an observation or a complaint. Ask the community "How to speed up image processing?". If you have no ability to improve the source then the answer will always be, "reduce the problem size or parallelize".

Der Luftmensch gravatar imageDer Luftmensch ( 2017-01-27 09:30:44 -0500 )edit

Slow as in noticeably delayed time when processing the camera feed in context of the haarcascades. I was experimenting and researched a little and found this to be a common complaint in context of speed of executions so I used lbpcascades and the lag has drastically decreased. If you have any suggestions regarding that code though or how one could make it faster I would be interested in hearing your opinion, thanks.

trav gravatar imagetrav ( 2017-01-27 12:20:09 -0500 )edit