Flutter Face Detection with Firebase ML Kit and CustomPainter

Flutter Face Detection with Firebase ML Kit and CustomPainter

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.


  • Get an image and convert it into a format that can be understood by our Firebase ML Kit.
  • Feed the image to the detector and get it to scan the image for possible faces.
  • Pull out the faces found and feed it to the CustomPainter.
  • Allow the CustomPainter to get the coordinates of the faces and then use them to draw rects.


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


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:

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

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);
  isLoading = true;

Then let’s convert this image into bytes so it can be understood by the Machine Learning 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.

class FacePainter extends CustomPainter {
  final ui.Image image;
  final List<Face> faces;
  final List<Rect> rects = [];

FacePainter(this.image, this.faces) { for (var i = 0; i < faces.length; i++) { rects.add(faces[i].boundingBox); } }

@override void paint(ui.Canvas canvas, ui.Size size) { final Paint paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 15.0 ..color = Colors.yellow;

canvas.drawImage(image, Offset.zero, Paint());
for (var i = 0; i &lt; faces.length; i++) {
  canvas.drawRect(rects[i], paint);


@override bool shouldRepaint(FacePainter oldDelegate) { return image != oldDelegate.image || faces != oldDelegate.faces; } }

Step 5 — Code the UI

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

  Widget build(BuildContext context) {
    return Scaffold(
      body: isLoading
          ? Center(child: CircularProgressIndicator())
          : (_imageFile == null)
              ? Center(child: Text('No image selected'))
              : Center(
                  child: FittedBox(
                    child: SizedBox(
                      width: _image.width.toDouble(),
                      height: _image.height.toDouble(),
                      child: CustomPaint(
                        painter: FacePainter(_image, _faces),
      floatingActionButton: FloatingActionButton(
        onPressed: _getImageAndDetectFaces,
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),

Step 6— Enjoy

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

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: GitHub

flutter firebase mobile-apps dart image

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Google's Flutter 1.20 stable announced with new features - Navoki

Google has announced new flutter 1.20 stable with many improvements, and features, enabling flutter for Desktop and Web

Top 25 Flutter Mobile App Templates in 2020

Flutter has been booming worldwide from the past few years. While there are many popular mobile app development technologies out there, Flutter has managed to leave its mark in the mobile application development world. In this article, we’ve curated the best Flutter app templates available on the market as of July 2020.

How To Succeed In Mobile App Wireframe Design?

This article covers everything about mobile app wireframe design: what to do and what not, tools used in designing a mobile or web app wireframe, and more.

What is Flutter and why you should learn it?

Flutter is an open-source UI toolkit for mobile developers, so they can use it to build native-looking Android and iOS applications from the same code base for both platforms. Flutter is also working to make Flutter apps for Web, PWA (progressive Web-App) and Desktop platform (Windows,macOS,Linux).

How much does it cost to make a Flutter app for your business?

Get a Free Quote on Android App Development, iPhone App Development, Ionic App Development, Video Development, ASO, SEO, Google Ads/Adwords, SEO for your app Idea.