Face Detection in Flutter using Firebase ML Kit
Face detection is a technique by which we can locate the human faces in the image given. Face detection is used in many places nowadays, especially the websites hosting images like Picasa, Photobucket, and Facebook. The automatically tagging feature adds a new dimension to sharing pictures among the people who are in the picture and also gives the idea to other people about who the person is in the image. And In this article, we are not using our own created Face detection machine learning algorithm. But We will be using the Firebase ML kit that gives us to use the face detection algorithm in Flutter.
Step by Step Implementation
First of all, Created an empty project in Flutter. You may refer to this article for the same. Before moving further let’s add some dependency into the pubspec.yaml file.
firebase_ml_vision: ^0.9.10 image_picker: ^0.6.7+17 firebase_core: ^0.5.3
We are using three dependencies for the project that gonna help us.
firebase_ml_vision and firebase_core are used for the face detection in an image and image_picker is used to pick the image from the camera or gallery and any other source. Our aim is to pick the image from the gallery using the button or from the camera and then show that image as an output with a human face detected.
Note: When you are this reading, the version of packages may change.
Now import these additional following packages that help in using the inbuilt firebase functions in our flutter project.
Now in void main( ) function call the runApp( ) method and call the class MyApp( ). Now create a new class named as MyApp( ) this will be going to be a stateful widget or class because our application does change its state at run time. And return the Scaffold( ). In scaffold, we have AppBar with title text Face Detection. Also, Add the floating action button to take the image, we are taking from the gallery in this project.
Our UI screen looks likes this:
Now Let’s create the _getImagegallery method for taking image. and call the setState method for making isLoading variable to true. And detecting faces in the image using the firebaseVision.
Now we have to use canvas to paint each face in the rectangular color that is detected and shown in the body of the Scaffold.