How to create a Face Detection Android App using Machine Learning KIT on Firebase

Firebase ML KIT aims to make machine learning more accessible, by providing a range of pre-trained models that can use in the iOS and Android apps. Let’s use ML Kit’s Face Detection API which will identify faces in photos. By the end of this article, we’ll have an app that can identify faces in an image, and then display information about these faces, such as whether the person is smiling, or has their eyes closed with wonderful GUI.

Approach

Step 1: Create a New Project

  1. Open a new project in android studio with whatever name you want.
  2. We are gonna work with empty activity for the particular project.
  3. The minimum SDK required for this particular project is 23 , so choose any API of 23 or above.
  4. The language used for this project would be JAVA .
  5. Leave all the options other than those mentioned above, untouched.
  6. Click on FINISH .

Step 2: Connect with ML KIT on Firebase.

  1. Login or signup on Firebase .
  2. In Firebase console, create a new project or if you wanna work with an existing project then open that.
  3. Name the project as per your choice.
  4. Go to Docs .
  5. Click on Firebase ML, and in the left space, choose ‘ recognize text ‘ under Vision.
  6. Go through the steps mentioned for better understanding.
  7. Come back to Android Studio.
  8. Go to Tools -> Firebase -> Analytics -> Connect with Firebase -> Choose your project from the dialog box appeared -> Click Connect. (This step connects your android app to the Firebase )

Step 3: Custom Assets and Gradle

apply plugin: ‘com.google.gms.google-services’

Step 4: Designing the UI

Advantage of Firebase ML Kit

Simplified Development: Firebase ML Kit provides pre-trained models for face detection, eliminating the need for complex machine learning expertise. This significantly reduces development time and resources.

Step 5: Firebase App Initializer

Step 6: Inflating the Result Dialog Box

Step 7: Open Camera on a Real Device and Enabling Face Detection

FAST (default) | ACCURATE

Favor speed or accuracy when detecting faces.

NO_LANDMARKS (default) | ALL_LANDMARKS

Whether to attempt to identify facial “landmarks”:

eyes, ears, nose, cheeks, mouth, and so on.

NO_CONTOURS (default) | ALL_CONTOURS

Whether to detect the contours of facial features.

Contours are detected for only the most prominent face in an image.

NO_CLASSIFICATIONS (default) | ALL_CLASSIFICATIONS

Whether or not to classify faces into categories

such as “smiling”, and “eyes open”.

float (default: 0.1f )

The minimum size, relative to the image, of faces to detect.

false (default) | true

Whether or not to assign faces an ID, which

can be used to track faces across images.

Note that when contour detection is enabled,

only one face is detected, so face tracking doesn’t

produce useful results. For this reason, and to improve

detection speed, don’t enable both contour detection and face tracking.