Android Face Detection Example

person Vinothkumar Arputharajfolder_openAndroidaccess_time November 1, 2017

Google has introduced Face Detection API with the Mobile Vision API. This Face Detection API is capable of detecting faces in images and video streams. You can also detect whether the eyes are open or closed and various states of your nose and mouth. Mobile Vision API also allows you to scan Barcodes and recognize Text.

Face Detection API specifically provides 4 important features:

  1. Face Recognition
  2. Face Tracking
  3. Find Landmarks in the detected face (nose, left eye, right eye, left cheek, right ear, etc.)
  4. Classification of the detected face (smiling, eyes open)

See the following:

  1. Create a Face Detector.
  2. Detect Face and Landmarks.
  3. Get the status of the detected face.

Creating a custom View class

Note: Google Mobile Services (GMS) will download the required native libraries if your app is using Face API for the first time. That means, the device should be connected to internet and there should be sufficient disk storage when the Face API is called for the first time. If the download is incomplete, face detection will not work.

1. Dependencies

Add the below line in your module’s build.gradle file:

Do Gradle Sync your project. We will start adding Face API implementation now.

 

2. app/build.gradle

Your app/build.gradle  will look similar to this.

 

3. Manifest File

We have to add  com.google.android.gms.vision.DEPENDENCIES  metadata to the manifest file.

 

4. Initiate Face Detection

Build an instance of FaceDetector with the required Landmark and Classification settings. For this example, I’m setting setTrackingEnabled to false because we are going to detect faces in an image. If you are trying face detection in Video or Live Stream, you can set the value to true.

 

5. Get the detected faces

FaceDetector requires a Frame instance to detect faces. The method detector.detect(frame) will return an SparseArray of detected Face instances. You can play around with the detected faces to add circular or square border, mark circles on the detected landmarks, etc.

In this tutorial, we are going to draw rectangular border over the detected faces and mark small circles on the identified landmarks.

6. Layout for MainActivity

 

7. MainActivity.java

For this tutorial, I have used an image available here https://cdn.vox-cdn.com/uploads/chorus_asset/file/6304257/sandler-chronies.jpg

8. Demonstration

Android Face Detection
Android Face Detection

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">