Android Face Detection Example


Android Face Detection Featured

Nowadays face filter apps are one of the most common apps available on a users phone, where a user can apply various funny filters on their face pictures. Interestingly the base tech that powers these sort of apps is similar to what we are going to discuss in this tutorial. That’s right we are going to discuss an Android Face Detection API. Interestingly to power all these apps, officially Google has released an Android Face Detection API in their Mobile Vision set of APIs. Basically all the face filter apps detect a face through a face detection API, and apply various overlays on the selected picture. Although when speaking of Face Detection APIs for Android, we have multiple options. But for this article we will be discussing Google backed Mobile Vision APIs only. As its fast and has deeply integrated Android development SDK/APIs.

 

Android Face Detection API – Mobile Vision

When speaking of face detection, it is often misunderstood by face recognition, therefore let me put it discretely. As of now Mobile Vision APIs do not support Face Recognition, right now they only support face detection. Although it does have the ability to identify the characteristics of a face, which includes eyes, nose, mouth and smile etc. But besides these characteristics Mobile Vision Face Detection API can also track a face in a video sequence, which is again not an application of face recognition, but face detection as it is identified by tracking the movement of that particular face in the sequence. Also since this API is a part of Google’s Play Services library, it is not bundled as a part of the APK, instead an ad-on package is downloaded internally by the play services itself if it is not present to support Android face detection. To do so, we need to set up the play services with the mobile vision dependency in build.gradle file as shown below:

Also the build.gradle(project) should have this:

 

Post this a <meta-data> tag for face detection api along with <uses-feature> and <uses-permission> tag for camera and accessing external storage needs to be added in the manifest as shown below:

 

Android Face Detection Library – Features 

Next lets understand the features of Mobile Vision Face Detection APIs. As amazing things can be done by correct usage of this API. Broadly speaking, this API can perform detailed facial analysis- like detection of facial features and identifying their states. Currently its features can be divided into three categories shown below:

1. Landmark Detection: One of the most important features of Mobile Vision Face Detection APIs is the detection of facial landmarks. In case you are wondering what are facial landmarks? They are the basic facial features or points of interest, like Nose, Eyes, and Mouth etc. Interestingly these features are detected very accurately by this Android Face Detection API. As of now following facial features are detected by this API:

  • Landmark.BOTTOM_MOUTH
  • Landmark.LEFT_CHEEK
  • Landmark.LEFT_EAR_TIP
  • Landmark.LEFT_EAR
  • Landmark.LEFT_EYE
  • Landmark.LEFT_MOUTH
  • Landmark.NOSE_BASE
  • Landmark.RIGHT_CHEEK
  • Landmark.RIGHT_EAR_TIP
  • Landmark.RIGHT_EAR
  • Landmark.RIGHT_EYE
  • Landmark.RIGHT_MOUTH

2. Facial Classification: Interestingly this API can also apply some logic on the detected facial landmarks and identify facial classifications. For ex. it can be detected through this API if the detected face has its eyes open or is smiling or not. Although this feature set may not sound very impressive as of now; but its very accurate and has a lot of room to grow. Maybe in near future some additional features might come in.

3. Face Tracking: Another very powerful feature of Mobile Vision Face Detection APIs is this face tracking feature. It simply gives us the ability to track a face in a video sequence. Once again, here I would like to clarify that this is not Face Recognition, this feature simply works on face detection only. As it tracks that face based on its movement in the consecutive video frames. 

 

Android Face Detection API – Example

Now that we have a basic understanding of how the Face Detection APIs work, here in this section we would build a short example where we showcase its capabilities. Since Android Face Detection is itself a huge topic we would limit the scope of this tutorial, and showcase the Facial Classification feature with Landmark Detection only. Also this would solve our primary use case of Face Detection. Therefore for this Android Face Detection Example we would simply take a picture from a camera and run face detection on it, by using the Mobile Vision Face Detection APIs. To start building, lets continue from the steps mentioned in the first section of this article and define a layout to take a picture as shown below:

Here for this layout I have used ConstraintLayout as my root layout, but its not mandatory to use this for your activity’s layout. You can also use the normal RelativeLayout or LinearLayout as per your needs, but if you wish to use ConstraintLayout, please don’t forget to add its dependency in your build.gradle file, as shown below:

Also full source code is available at the end of this tutorial. Next lets define the MainActivity for this Android Face Detection tutorial.

 

In the above piece of code, in onCreate method I have simply initialized the face detector by calling the FaceDetector.Builder(getApplicationContext()) builder. This would download the Google Play Service dependencies for performing face detection and initialize them. In a way this also works as a safety measure to download the dependencies even when we specified the app to download the dependencies for face detection in the manifest (shown in first step). Also to make it event more reliable, we have also put a check; to check whether the detector is operational or not just before scanning the actual image in scanFaces() method. Full source code is available here:

Full Source Code

Also as you can see above we have initialized the Mobile Vision Face Detector with two capabilities, i.e. setLandmarkType(FaceDetector.ALL_LANDMARKS) and setClassificationType(FaceDetector.ALL_CLASSIFICATIONS). This would identify all the facial landmarks and classifications on a detected face, rest of the code just shows how we have plotted it on screen, which is self explanatory. The end result would look something like this:

Android Face Detection

Additional Capabilities

In addition to all what we have discussed above in this Android Face Detection Example, there is one more capability present in these Face Detection APIs. That is the face tracking capability with MultiProcessor. The great thing about this feature is that it can not only track a single face, but can also track multiple faces in a video sequence.  But due to limited scope of this article it is not covered here. Also since the face detection APIs are a part of Google’s Mobile Vision Suite, we have the capability to build a multi detector. Where we can track multiple faces and multiple bar codes or QR codes in a single video sequence by using the MultiDetector class. This feature is something very new and very powerful which opens a whole new area to explore into. Connect with us on Twitter, Facebook and Google+ for more updates on this. 

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Android Face Detection Example

  • pourya

    hey mohit
    thank you for this code
    there one question and that is can this app run in background?i mean can we extend it using broadcast recever and service to detect face?
    i need it for a specific project
    any help will be appriciated