0

I am using haar cascading to detect frontal faces. I have below code:

int main()
{
    Mat image;

    cv::VideoCapture cap;
    cap.open(1); 

    int frame_idx = 0;
    time_t fpsStartTime, fpsEndTime;
    time(&fpsStartTime);
    for (;;)
    {
        frame_idx = frame_idx + 1;
        cap.read(image);

        CascadeClassifier face_cascade;
        face_cascade.load("<PATH");
        std::vector<Rect> faces;
        face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, Size(30, 30));

        // Draw circles on the detected faces
        for (int i = 0; i < faces.size(); i++)
        {
            Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);

            ellipse(image, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
        }

        cv::imshow("Detected Face", image);
        char k = cv::waitKey(1);
        if (k == 27)
            break;

        time(&fpsEndTime);
        double seconds = difftime(fpsEndTime, fpsStartTime);
        double fps = frame_idx / seconds;
        std::string fps_txt = "FPS: " + std::to_string(fps);  // fps_str.str();
        cout << "FPS :  " << fps_txt << endl;

    }
    return 0;
}

This code is working fine but giving very low FPS. FPS is ~1fps which is very slow. I am running this on Windows 10 laptop with intel i5 CPU. I believe this should not be this much slow.

In debug mode, it gives ~1fps but in release mode it is 4-5fps which again is very slow. I have run some openvino demo's like pedestrian detection which uses 2 openvino model on same hardware and it gives ~17-20fps which is very good.

I am using USB 3.0 logitech brio 4k camera so this cannot be a reason of low fps. My question is why haar cascading is performing very slow. Is there anyway we can enhance its speed and make it more usable. Please help. Thanks

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • Possible duplicate of [OpenCV: Improving the speed of Cascades detection](https://stackoverflow.com/questions/24949499/opencv-improving-the-speed-of-cascades-detection) – Duck Dodgers Jul 22 '19 at 08:31
  • haar cascade classifier detection is expensive. OpenVINO is highly optimized for intel hardware and probably uses AVX instructions (and probably reduces the input size). Did you try openCV with IPP? You could resize the image to reduce the computation complexity, if resolution is still big enough. – Micka Jul 22 '19 at 10:28
  • What is the speed if you remove `cv::waitKey(1)` and the `imshow`? – wcochran Sep 20 '20 at 14:43

2 Answers2

1

You should not (re)load the classifier on every frame. It should load once before processing frames.

Move the following statements out of the for loop.

CascadeClassifier face_cascade;
face_cascade.load("<PATH");

See a demo on OpenCV Docs.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Oh yes I agree but again not much improvement. I am still getting 1-2fps in debug and 6-7fps in release mode. – S Andrew Jul 22 '19 at 08:35
  • @SAndrew Please run the demo I've linked to on your machine and compare its FPS with yours. – frogatto Jul 22 '19 at 08:44
0

Can you confirm if you are using right .lib and .dll file?

I have checked and seen that the opencv_world440.lib & opencv_world440.dll provide great speed compared to opencv_world440d.lib & opencv_world440d.dll files. My guess is that opencv_world440d.lib & opencv_world440d.dll are for debugging so slow speed.

Note::Your lib name may vary ie.., opencv_world<"SomeNumber">d.lib & opencv_world<"SomeNumber">.lib