Flutter Face Detection

Detect with Firebase ML Kit + Draw with CustomPainter

Akora Ing. DKB
Sep 22, 2019 · 3 min read
Image for post
Image for post

In this article, we’ll explore the basics of detecting faces within an image with Firebase ML Kit and make it visible with the help of CustomPainter.

Workflow

Dependencies:

We will need the image_picker and firebase_ml_vision packages to work out this project.

Implementation

Step 1 — Firebase Project and Dependencies

First create a Firebase project (refer to the docs) and add the image_picker and firebase_ml_vision dependencies to the pubspec.yaml file. Our dependencies should now look like this:

Image for post
Image for post

Also add the face model dependency to our app levelbuild.gradle file.

Image for post
Image for post

Step 2 — Fetch and Process the Image

Here, we first need to fetch the image from either the camera or the device gallery using the image_picker plugin.

final imageFile = await ImagePicker.pickImage(source:ImageSource.gallery);
seState((){
isLoading = true;
});

Then let’s convert this image into bytes so it can be understood by the ML model and the custom painter as well.

final data = await file.readAsBytes();
await decodeImageFromList(data).then(
(value) => setState(() {
_image = value;
isLoading = false;
}),
);

Step 3 — Detect the Faces

We will now create an instance of the Face Detector and task it to process the image to find any possible faces.

final faceDetector = FirebaseVision.instance.faceDetector();
List<Face> faces = await faceDetector.processImage(image);

Step 4 — CustomPainter

The face detector will return a list of Face class which contains the Rect coordinates. The Painter will then use these coordinates to draw a rectangular box around the faces using these coordinates. Below is the code for our CustomPainter.

Step 5 — Code the UI

We can now use the custom painter inside our widget tree like so:

Step 6— Enjoy

Now, let’s run the app and hope that we had everything right.

Image for post
Image for post
Image for post
Image for post

Yaaayyy!!! We’ve got our face detection app with some few lines of code thanks to the Flutter framework and Firebase.

Checkout out the source code of this project:


Flutter Community

Articles and Stories from the Flutter Community

Akora Ing. DKB

Written by

Flutter Developer | Android (Java/Kotlin) | Dart | Arduino | Node.js | Python. Electrical Engineering Student, KNUST and Member of the Flutter Ghana Community.

Flutter Community

Articles and Stories from the Flutter Community

Akora Ing. DKB

Written by

Flutter Developer | Android (Java/Kotlin) | Dart | Arduino | Node.js | Python. Electrical Engineering Student, KNUST and Member of the Flutter Ghana Community.

Flutter Community

Articles and Stories from the Flutter Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store