Slow OclCascadeClassifier::detectMultiScale()

asked 2014-10-06 08:59:52 -0600

stepo gravatar image

updated 2014-10-06 12:00:31 -0600

Hi,

I have a simple demo application that detect faces in video using OclCascadeClassifier from OpenCL module. The problem is that the detectMultiScale() is really slow. First, I suspected the oclMat::upload() method, but it seems that the bottle neck is the detections itself.

Here is my code:

#include <opencv2/ocl/ocl.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <QDateTime>
#include <QDebug>

int main()
{
    cv::ocl::OclCascadeClassifier detector;
    detector.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml");
    cv::VideoCapture capture(0);
    cv::Mat frame;
    std::vector<cv::Rect> faces;
    cv::ocl::oclMat oclFrame;

    int counter = 0;
    double uploadTime = 0;
    double detectTime = 0;
    int N = 500;
    while (counter < N && capture.read(frame))
    {
        QDateTime beforeUpload = QDateTime::currentDateTime();
        oclFrame.upload(frame);
        QDateTime afterUpload = QDateTime::currentDateTime();

        detector.detectMultiScale(oclFrame, faces);
        QDateTime afteDetect = QDateTime::currentDateTime();

        uploadTime += beforeUpload.msecsTo(afterUpload);
        detectTime += afterUpload.msecsTo(afteDetect);

        for (const auto &r : faces)
            cv::rectangle(frame, r, cv::Scalar(0, 255, 0));

        cv::imshow("test", frame);
        cv::waitKey(1);
        counter++;
    }

    uploadTime /= N;
    detectTime /= N;

    qDebug() << "uploadTime:" << uploadTime;
    qDebug() << "detectTime:" << detectTime;
}

And this is the output:

uploadTime: 0.928
detectTime: 517.494

BTW I have nVidia GeForce GT 640M with proper driver.

Thanks. Stepan

edit retag flag offensive close merge delete

Comments

this is 500 milliseconds per image for what image size? Keep in mind that you are searching in all locations on every possible scale up to 24x24 pixels. Depending on your input resolution this processing time is completely normal!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-07 06:26:35 -0600 )edit

Well, it's not normal. I have 640*480 input frame resolution. If I'm running the same code but with CPU-based cv::CascadeClassifier I'm getting processing time 350ms per frame.

stepo gravatar imagestepo ( 2014-10-07 06:34:16 -0600 )edit

Hmm and using the exact same command? Or with different parameters?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-07 07:45:00 -0600 )edit

Exactly the same command, I set scaleFactor = 1.1 and minNeighbors = 3 in detectMultiScale parameters explicitly to be sure. And Btw, if I'm using cv::gpu::CascadeClassifier_GPU (CUDA), I get 80ms processing time.

stepo gravatar imagestepo ( 2014-10-07 07:53:43 -0600 )edit

Possibly something wrong then in the ocl build. I suggest creating a bug issue at the dev forum of openCV adding all this info and your complete system configuration.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-07 07:55:37 -0600 )edit