Giter Club home page Giter Club logo

face_mask_detector_flutter_tflite's Introduction

Logo

Face Mask Detector App

Face Mask Detection mobile application built with Flutter and TensorFlow lite in order to detect face masks using images and live camera.

โญ Features

  • Detect mask on the live detectoin.
  • Detect mask from a photo (camera or gallery)

Installation

1- Install Packages

image_picker: pick image from gallery
https://pub.dev/packages/image_picker
tflite: run our trained model
https://pub.dev/packages/tflite
camera: get the streaming image buffers
https://pub.dev/packages/camera
  1. Configure Project Android
android/app/build.gradle

android {
    ...
    aaptOptions {
        noCompress 'tflite'
        noCompress 'lite'
    }
    ...
}

minSdkVersion 21

  1. Train our model
* Download the dataset for training
    https://www.kaggle.com/prasoonkottarathil/face-mask-lite-dataset

* Training
    - go to https://teachablemachine.withgoogle.com to train our model
    - Get Started
    - Image Project
    - Edit `Class 1` for any Label(example `With_Mask`)
    - Edit `Class 2` for any Label(example `Without_Mask`)
    - Update image from dataset download above
    - Click `Train Model`(using default config) and waiting...
    - Click `Export Model` and select `Tensorflow Lite`
    - Download (include: *.tflite, labels.txt)

4. Load model
loadModel() async {
    Tflite.close();
    await Tflite.loadModel(
        model: "assets/model/model.tflite", 
        labels: "assets/model/labels.txt",
        //numThreads: 1, // defaults to 1
        //isAsset: true, // defaults: true, set to false to load resources outside assets
        //useGpuDelegate: false // defaults: false, use GPU delegate
    );
  }

5. Logic for Run Model on Image from (Camera or Gallery).
  _loadImage({required bool isCamera}) async {
    try {
      final XFile? image = await _picker.pickImage(
        source: isCamera ? ImageSource.camera : ImageSource.gallery,
      );
      if (image == null) {
        return null;
      }
      _image = File(image.path);
      _detectImage(_image!);
    } catch (e) {
      checkPermissions(context);
    }
  }
  _detectImage(File image) async {
    var recognitions = await Tflite.runModelOnImage(
      path: image.path,
      numResults: 2,
      threshold: 0.6,
      imageMean: 127.5,
      imageStd: 127.5,
    );
    setState(() {
      _loading = false;
      _recognitions = recognitions!;
      print(_recognitions[0]);
    });
  }
  1. Logic for Run Model on Image Stream (Live Detection).

Global Variables

  List<CameraDescription>? cameras; //Global Variable
  cameras = await availableCameras(); //init this in main before run app.

Live Detector Screen

  CameraImage? cameraImage;
  CameraController? cameraController;
  String outPut = "";
  int selectedCamera = 0;

  _toggleCameras() async {
    setState(() {
      selectedCamera = selectedCamera == 1 ? 0 : 1;
    });
    _initCamera();
  }

_initCamera() {
  cameraController =
      CameraController(cameras![selectedCamera], ResolutionPreset.max);
  cameraController!.initialize().then((value) {
    if (!mounted) {
      return;
    }
    setState(() {
      cameraController!.startImageStream((imageStream) {
        cameraImage = imageStream;
        _runModelOnFrame();
      });
    });
  });
}


_runModelOnFrame() async {
  if (cameraImage != null) {
    var recognitions = await Tflite.runModelOnFrame(
      bytesList: cameraImage!.planes.map((plane) {
        return plane.bytes;
      }).toList(),
      imageHeight: cameraImage!.height,
      imageWidth: cameraImage!.width,
      imageMean: 127.5,
      imageStd: 127.5,
      rotation: 90,
      numResults: 2,
    );
    for (var recognition in recognitions!) {
      outPut = recognitionResult(recognition);
      setState(() {});
    }
  }
}

Output format:
  [{
    index: 0,
    label: "With_Mask",
    confidence: 0.989
  },...]

  Get the Output from this simple function:

    recognitionResult(recognition) {
    double confidence = (recognition['confidence'] * 100);
    var label = recognition['label'].split("_").join(" ");
    return "$label (${confidence.roundToDouble()}%)";
    }

  Output --> With Mask (100%)

Screenshots


Download APK

Download APK File and install it, Enjoy.

Download zip

face_mask_detector_flutter_tflite's People

Contributors

yousefshabaneg avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

face_mask_detector_flutter_tflite's Issues

TFlite

hello I tried to replace the model with the training results using yolo5, but why can't it be used?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.