Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting face landmarks points in android

Tags:

android

dlib

I am developing app in which I need to get face landmarks points on a cam like mirror cam or makeup cam. I want it to be available for iOS too. Please guide me for a robust solution. I have used Dlib and Luxand.

DLIB: https://github.com/tzutalin/dlib-android-app

Luxand: http://www.luxand.com/facesdk/download/

Dlib is slow and having a lag of 2 sec approximately (Please look at the demo video on the git page) and luxand is ok but it's paid. My priority is to use an open source solution. I have also use the Google vision but they are not offering much face landmarks points. So please give me a solution to make the the dlib to work fast or any other option keeping cross-platform in priority. Thanks in advance.

like image 693
DevChamp Avatar asked Jul 18 '16 11:07

DevChamp


People also ask

Which method is used to detect the face in Android?

To detect faces in an image, create an InputImage object from either a Bitmap , media. Image , ByteBuffer , byte array, or a file on the device. Then, pass the InputImage object to the FaceDetector 's process method. For face detection, you should use an image with dimensions of at least 480x360 pixels.

What is facial landmark detection?

What Are Facial Landmarks? Facial landmark detection is a computer vision task in which a model needs to predict key points representing regions or landmarks on a human's face – eyes, nose, lips, and others.

Which algorithm is used for face detection?

The OpenCV method is a common method in face detection. It firstly extracts the feature images into a large sample set by extracting the face Haar features in the image and then uses the AdaBoost algorithm as the face detector.

How can I make my face detection more accurate?

How can you improve the accuracy of face recognition? Facial recognition results highly rely on the quality of the image and the influence of factors such as lighting, occlusion, the person's pose, and race. One way to improve face recognition is to collect versatile training datasets with detailed visual data.


1 Answers

You can make Dlib detect face landmarks in real-time on Android (20-30 fps) if you take a few shortcuts. It's an awesome library.

Initialization

Firstly you should follow all the recommendations in Evgeniy's answer, especially make sure that you only initialize the frontal_face_detector and shape_predictor objects once instead of every frame. The frontal_face_detector will initialize faster if you deserialize it from a file instead of using the get_serialized_frontal_faces() function. The shape_predictor needs to be initialized from a 100Mb file, and takes several seconds. The serialize and deserialize functions are written to be cross-platform and perform validation on the data, which is robust but makes it quite slow. If you are prepared to make assumptions about endianness you can write your own deserialization function that will be much faster. The file is mostly made up of matrices of 136 floating point values (about 120000 of them, meaning 16320000 floats in total). If you quantize these floats down to 8 or 16 bits you can make big space savings (e.g. you can store the min value and (max-min)/255 as floats for each matrix and quantize each separately). This reduces the file size down to about 18Mb and it loads in a few hundred milliseconds instead of several seconds. The decrease in quality from using quantized values seems negligible to me but YMMV.

Face Detection

You can scale the camera frames down to something small like 240x160 (or whatever, keeping aspect ratio correct) for faster face detection. It means you can't detect smaller faces but it might not be a problem depending on your app. Another more complex approach is to adaptively crop and resize the region you use for face detections: initially check for all faces in a higher res image (e.g. 480x320) and then crop the area +/- one face width around the previous location, scaling down if need be. If you fail to detect a face one frame then revert to detecting the entire region the next one.

Face Tracking

For faster face tracking, you can run face detections continuously in one thread, and then in another thread, track the detected face(s) and perform face feature detections using the tracked rectangles. In my testing I found that face detection took between 100 - 400ms depending on what phone I used (at about 240x160), and I could do 7 or 8 face feature detections on the intermediate frames in that time. This can get a bit tricky if the face is moving a lot, because when you get a new face detection (which will be from 400ms ago), you have to decide whether to keep tracking from the new detected location or the tracked location of the previous detection. Dlib includes a correlation_tracker however unfortunately I wasn't able to get this to run faster than about 250ms per frame, and scaling down the resolution (even drastically) didn't make much of a difference. Tinkering with internal parameters produced increase speed but poor tracking. I ended up using a CAMShift tracker based on the chroma UV planes of the preview frames, generating the color histogram based on the detected face rectangles. There is an implementation of CAMShift in OpenCV, but it's also pretty simple to roll your own.

Hope this helps, it's mostly a matter of picking the low hanging fruit for optimization first and just keep going until you're happy it's fast enough. On a Galaxy Note 5 Dlib does face+feature detections at about 100ms, which might be good enough for your purposes even without all this extra complication.

like image 156
samgak Avatar answered Sep 29 '22 13:09

samgak