【Face Detection】Super real-time face tracking algorithm based on optical flow and CNN

Updated on December 20, 2018

  1. Removed all content that does not affect tracking performance, including time-consuming initialization operations.

  2. Added support for opencv3.

  3. Now, tracking only takes about 3ms on my PC.

  4. Deleted the init function, so the detNumToTack parameter is also useless and has been removed.


The built-in tracking algorithm in OpenCV is not very effective, so I found an open-source optical flow tracking algorithm on GitHub, which performs much better. Based on this algorithm, I further removed unnecessary content and adjusted some parameters to improve the algorithm's speed without affecting its effectiveness. On my PC (Intel® Xeon(R) CPU E5-2673 v3 @ 2.40GHz × 48), it takes 16ms (MTCNN, ncnn) for a 320*240 image, 30ms for initialization, and 10ms for optical flow tracking.

This algorithm has the following advantages:

  1. It can track faces stably without excessive bounding box shaking. I have optimized the flashing bounding box issue in the examples folder on GitHub, so it will no longer occur.

  2. It achieves fast face tracking without relying on deep learning.

  3. It has no other dependencies and only uses ncnn and OpenCV (required version: 2.4.x).

Main Content#

Let's discuss some logical matters in the void Impl::Detect() function.

1. Regarding the significance of the detNumToTack parameter: When there are objects repeatedly obstructing the face or in certain situations where the presence of a face is intermittent, without the detNumToTack parameter, the algorithm would perform detection immediately after initialization. I think this is a waste of time, so I added the detNumToTack parameter. After performing detection detNumToTack times, it initializes and then starts tracking.

  1. The purpose of RNet is to evaluate the results of each tracking and perform re-detection when the result is below a threshold. This is done to prevent tracking of other objects. Initially, without RNet, if a hand slowly moves in front of the face, the bounding box would follow the hand.

  2. I have fixed the width and height of the tracking box to achieve stable results. After running the processFrame tracking function, the bounding box is reset to its initial width and height.

  3. The face detection part can be replaced as desired, and the RNet part can also be replaced with the PCN-2 network.

GitHub link: https://github.com/HansRen1024/Face-Tracking-Using-Optical-Flow-and-CNN

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.