Matthieu Napoli
Matthieu Napoli

Reputation: 49713

Slow face detection on OpenCV?

I compiled and installed OpenCV (last version from the SVN) on Mac Os X (this is maybe the source of the problem).

The sample works, but the face detection algorithm seems slow to me. The detection time for a face is around 400ms (I just used the example included). The FPS is then quite low.

On youtube and all, I see super-smooth video with real time face detection (even on the iPhone) so I feel confuse. I remember it being even faster on my old Windows PC.

Is 400 ms a correct detection time ?

Note : my Macbook is not old (2009) and everything runs fine on it. I use the iSight webcam (integrated webcam). I have just one face (my face) on the webcam. And it is around the same time if there is no face.

Upvotes: 8

Views: 14343

Answers (6)

Shamshirsaz.Navid
Shamshirsaz.Navid

Reputation: 2362

I resized the frame in video sequence with the factor of 10 and it works great. Also for faster process Use Face Detector in each x frame and then use Face Tracker instead in x-1 frames between (To avoid drifting).

checkout this link: Tracking vs Detecting


And also maybe a sample code helps someone (Its a Simple Detecting not Tracking or Recogniton):

THIS IS ANDROID EXAMPLE but it is pretty much alike in opencv for other platforms and languages"

mRgba = inputFrame.rgba();
mGray = inputFrame.gray();

int resizeFactor=10;//or any other number based on your input resolution
Imgproc.resize(mGray,mGray,newSize(mGray.width()/resizeFactor,mGray.height()/resizeFactor));
mRgba = proc(mRgba, mGray,resizeFactor);

And "proc" function something like this - I found and upgrade this from OpenCV4Android Face detection sample:

public Mat proc(Mat mRgba, Mat mGray, int resizeFactor) {
MatOfRect faces = new MatOfRect();
        if (mJavaDetector != null)
            mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, new Size(0,0), new Size());//change this according to your usage-> Size(0, 0)

        Rect[] facesArray = faces.toArray();
        for (Rect rect : facesArray) {
            Point t1 = rect.tl();
            t1.x *= resizeFactor;
            t1.y *= resizeFactor;
            Point br = rect.br();
            br.x *= resizeFactor;
            br.y *= resizeFactor;
            Imgproc.rectangle(mRgba, t1, br, FACE_RECT_COLOR, 3);
        }

        return mRgba;
    }

CPU I used: Snapdragon 720G


Also from a related forum, I found that LBP is much faster than HAAR. I am not sure about this and the performance and Quality but I thought it would be good to mention this point as well.

Upvotes: 0

William
William

Reputation: 71

Recently I had found a Simd Library, which has an implementation of HAAR and LBP cascade classifiers. It can use standard HAAR and LBP casscades from OpenCV. This implementation has SIMD optimizations with using of SSE4.1, AVX2 and NEON(ARM), so it works in 2-3 times faster then original OpenCV implementation.

Upvotes: 4

saurabheights
saurabheights

Reputation: 4574

When running on image, you should downscale to certain limits. In case of videos, along with Face detection, you can also try tracking. You can do face detection every alternate frames and track the position of face in between frames.

Also, OpenCv supports use of Canny to discard regions where chances of finding Face is none.

Upvotes: 1

Barney Szabolcs
Barney Szabolcs

Reputation: 12554

Adding to the previous answers:

you can also speed things up by setting the Max and most importantly the Min size for detectMultiScale.

[Also, as the previous answers say, heavy scaling-down is in order as Haar detector uses very simple features (for the relations of upto 6 pixels; on larger scales you add up rectangle-like areas as if it was just one pixel). On standard mac/mbp2011 I could get around 60fps that is more than enough.]

For an even better speedup you could also eliminate non-changing areas, using say templateMatching.

Upvotes: 4

keukpa
keukpa

Reputation: 27

I was having the same problem, on a Quad Core machine with 4GB RAM was 500ms per detection, however I've noticed there is a Scale option....getting this to:

./facedetect --scale=4

I get detection rates of <20ms

Hope that helps,

Keukpa

Upvotes: 1

Krish
Krish

Reputation: 1837

What is the size of the input image. I am guessing 640x480. Generally people who post YouTube videos resize the image to 160x120. IN full resolution of 640x480 it is very difficult to get more than 2-3 fps. Try to send 160x120 image. You should be getting at least 10fps.

Upvotes: 10

Related Questions