Search code examples
performanceopencvface-detectionframe-rate

Increasing FPS while using OpenCV Face detection


I am aware that real time face detection is something that needs high cpu time, too much to implement it in a game(which is my goal). Therefore I am looking for a way to improve my FPS.

In the game, there should only be two faces. Those faces are nearly always on the same positions. One in the left lower middle of the screen, the other one in the right lower middle.

I CAN assume that there are ALWAYS exactly 2 Faces, which, like I said before, are roughly on the same positions as in the frame before.

My idea was to tell the algorithm WHERE he has to search.

First frame: calculates where there are faces on the screen. Coordinates of Faces are stored for next frame.

Following frames: use the coordinates of the frame before to start looking for faces in the area around the stored position. If nothing found, increase the distance from the position where it has to look for faces and search again.

Doing so would greatly improve my performance, however I didn't find any way to tell the algorithm where it has to look for faces.

Is there a way to do so?

Thanks.


Solution

  • If you want to use the OpenCV algorithm without modifying it, you can extract a sub-image around the location of the faces at the previous frame. In this way the OpenCV face detector performs a sliding window search on a much smaller region. Then you remap the face position in the full frame coordinate system. If your faces do not move too fast you can run this every n-frames and interpolate the position between the detection frames for a further speed-up. To get the subImg you can use:

    cv::Rect roi(xTl,yTl,w,h);
    cv::Mat subImg = img(roi);
    

    where xTl,yTl are the top left coordinates of the searching window and w,h the size.

    Alternatively once you detect the faces, you can use MeanShift/CamShift tracker (or other trackers) to find the position in every frame: http://docs.opencv.org/trunk/doc/py_tutorials/py_video/py_meanshift/py_meanshift.html .