使用 Tensorflow(Flutter) 进行面罩检测

使用 TFlite 进行口罩佩戴检测

信息

一款使用 flutter 和 tensor flow lite 进行人脸口罩检测的应用。

⭐ 特点

  • 实时摄像头检测口罩

  • 从照片中检测口罩

  • MVVM架构

?  安装

  1. 安装依赖项

camera: get the streaming image buffers
https://pub.dev/packages/camera

tflite: run our trained model
https://pub.dev/packages/tflite

image_picker: pick image from gallery
https://pub.dev/packages/image_picker

2. 配置项目

  • Android

android/app/build.gradle

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


minSdkVersion 21

3. 训练我们的模型

* Download the dataset for training
    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 `WithMask`)
    - Edit `Class 2` for any Label(example `WithoutMask`)
    - 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. 加载模型

loadModel() async {
    Tflite.close();
    await Tflite.loadModel(
        model: "assets/model.tflite", 
        labels: "assets/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. 运行模型

  Future<List<dynamic>?> runModelOnFrame(CameraImage image) async {
    var recognitions = await Tflite.runModelOnFrame(
        bytesList: image.planes.map((plane) {
          return plane.bytes;
        }).toList(),
        imageHeight: image.height,
        imageWidth: image.width,
        imageMean: 127.5,   //defaults to 127.5
        imageStd: 127.5,    //defaults to 127.5
        rotation: 90,       // defaults to 90, Android only
        numResults: 2,      // defaults to 5
        threshold: 0.5,     // defaults to 0.1
        asynch: true,       // defaults to true
    );      
    return recognitions;
  }

  Future<List<dynamic>?> runModelOnImage(File image) async {
    var recognitions = await Tflite.runModelOnImage(
      path: image.path,
      numResults: 2,
      threshold: 0.5,
      imageMean: 127.5,
      imageStd: 127.5,
    );
    return recognitions;
  }

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

6. 问题

* IOS
1.'vector' file not found
Open ios/Runner.xcworkspace in Xcode, click Runner > Tagets > Runner >Build Settings, 
search Compile Sources As, change the value to Objective-C++

2. 'tensorflow/lite/kernels/register.h' file not found
The plugin assumes the tensorflow header files are located in path "tensorflow/lite/kernels".
However, for early versions of tensorflow the header path is "tensorflow/contrib/lite/kernels".

7. 源代码

please checkout repo github
https://github.com/hiennguyen92/face_mask_detection_tflite

? 示例

  1. 演示说明: https://www.youtube.com/watch?v=2er_XZb_oi4&ab_channel=HienNguyen
  2. Image

GitHub

查看 Github