Mask Detection App in Flutter
Nowadays in the situation of Covid, it is very important to wear a Mask and protect yourself and others. So this App helps to detect whether the person has worn the Mask or Not. In this Application, the Mask detection is done with the help of TensorFlow Lite.
Follow the steps to implement Mask Detection in our Flutter App
- Add the dependency to pubspec.yaml file.
- Import the dependency to the main.dart file
Step 1: First add the following dependency in your pubspec.yaml file
Add the given dependency in pubspec.yaml file
Step 2: Now navigate to main.dart() file and return Material App().
First, we have declared MyApp() in runApp in the main function. Then we have created StatelessWidget for MyApp in which we have returned MaterialApp(). In this MaterialApp() we have given the title of our App then declared the theme of our App as primaryColor as blue. Then we have given our first screen of or slider app in the home: HomePage()
Step 3: Now declare StatefulWidget() for HomePage() class.
In this StatefulWidget() we have declared various variable under state function. And after that we have declare methods for loadImageGallery() and loadImageCamera(). These methods are used for picking the images from the gallery and camera with the help of the image_picker library which we have imported.
Step 4: Now Go On Google Teachable Machine.
Now Go On a Google Teachable Machine. Where you have o train the Model for Mask Detection. Now Click On Image Project. Here You have to create Two Classes One for Mask and the other for No Mask. Now in the Mask Class click on WebCam to record your face Mask from all Angles. In the same way you will record your face Without Mask in the No Mask Class from all Angles. Now click on Train Model.
After completing that you will click on Export Model.
After that click on Tensorflow Lite and in that select Floating point and then click on Download my model. Now After Downloading the Model Extract the file. Now add the extracted files to the assets folder in pubspec.yaml file.
Step 5: Now Add the Following Lines of Code to import your Model into your App.
Now we have created loadModel() in this we have added extracted files of our downloaded file in this method. And we have initialized the loadModel() in initState(). After that, we have created detectImage() for detecting the image. After that, we have set the state to update the image.
And we have Ended the function using dispose().
Step 6: After that, we have given the UI of our Application.
In this StateFulWidget() we have declared Scaffold(). In which we have declared Appbar() in this appbar we have given the title of our app in Text Widget. After the appbar in the body section, we have declared Column() in which the image and the buttons will appear one below another. When we click on Capture Image Button the camera of the device will open, and you have to capture the image that will detect the mask.
In the same way when you click on Pick Image from Gallery. Your File manager will open from where you can select Image which will be used for detecting the mask.
Full Code for Mask Detection App: