Face Detection#

  • On-device AI Face Detection App with ZETIC.MLange

Github Repository#

  • We provide Face Detection demo application source code for both Android and iOS. repository

What is Face Detection#

  • The Face Detection model in Google’s MediaPipe is a high-performance machine learning model designed for real-time face detection in images and video streams.

  • Face Detection Google AI Document : link

Step-by-step implementation#

0. Prerequisites#

Prepare the model Face Detection from github.

  • Face Detection: Convert the Tensorflow model to the TorchScript model.

    $ pip install tf2onnx
    $ python -m tf2onnx.convert --tflite face_detection_short_range.tflite --output face_detection_short_range.onnx --opset 13
    

1. Generate ZETIC.MLange model#

  • Get your own MLange model key from the model

    • If you want to get your own model key, please get your own model key as below.

        # (1) Get mlange_gen
        $ wget https://github.com/zetic-ai/ZETIC_MLange_document/raw/main/bin/mlange_gen && chmod 755 mlange_gen
    
        # (2) Run mlange_gen for two models
        #    - Face detection model
        $ ./mlange_gen -m face_detection_short_range.onnx -i input.npy
    
    • Expected output

        ...
        MLange Model Key : {YOUR_FACE_DETECTION_MODEL_KEY}
        ...
    

2. Implement ZeticMLangeModel with your model key#

  • We prepared a model key for the demo app: face_detection. You can use the model key to try the Zetic.MLange Application.

  • Anroid app

      val faceDetectionModel = ZeticMLangeModel(this, 'face_detection')
    
      faceDetectionModel.run(inputs)
    
      val outputs = faceDetectionModel.outputBuffers
    
  • iOS app

    • For the detailed application setup, please follow deploy to XCode page

    • ZETIC.MLange usage in Swift

      let faceDetectionModel = ZeticMLangeModel('face_detection')
    
      faceDetectionModel.run(inputs)
    
      let outputs = faceDetectionModel.getOutputDataArray()
    

3. Prepare Face Detection image feature extractor for Android and iOS#

  • We provide a Face Detection feature extractor as an Android and iOS module.

    • (The Face Detection feature extractor extension will be exposed as an open-source repository soon.)

    • You can use your own feature extractor if you have one for Face Detection usage

  • For Android

    // (0) Initialize ZeticMLangeFeatureFaceDetection
    val feature = ZeticMLangeFeatureFaceDetection()
    
    // (1) Preprocess bitmap and get processed float array
    val inputs = feature.preprocess(bitmap)
    
    ...
    
    // (2) Postprocess to bitmap
    val resultBitmap = feature.postprocess(outputs)
    
  • For iOS

    import ZeticMLange
    
    // (0) Initialize ZeticMLangeFeatureFaceDetection
    let feature = ZeticMLangeFeatureFaceDetection()
    
    // (1) Preprocess UIImage and get processed float array
    let inputs = feature.preprocess(image)
    
    ...
    
    // (2) Postprocess to UIImage
    let resultBitmap = feature.postprocess(&outputs)
    

Total Face Detection Process implementation#

Pipelining two models.

  • For Android

    • Kotlin

      • Face Detection Model

      // (0) Initialization Models
      val faceDetectionModel = ZeticMLangeModel(this, 'face_detection')
      
      // (1) Initialization Feature
      val faceDetectionFeature = ZeticMLangeFeatureFaceDetection()
      
      // (2) Preprocess Image
      val faceDetectionInputs = faceDetectionFeature.preprocess(bitmap)
      
      // (3) Process Model
      faceDetectionModel.run(faceDetectionInputs)
      val faceDetectionOutputs = faceDetectionModel.getOutputDataArray()
      
      // (4) Postprocess model run result
      val faceDetectionPostprocessed = faceDetectionFeature.postprocess(faceDetectionOutputs)
      
  • For iOS

    • Swift

      • Face Detection Model

      // (0) Initialization Models
      let faceDetectionModel = ZeticMLangeModel('face_detection')
      
      // (1) Initialization Feature
      let faceDetectionFeature = ZeticMLangeFeatureFaceDetection()
      
      // (2) Preprocess Image
      let faceDetectionInputs = faceDetectionFeature.preprocess(bitmap)
      
      // (3) Process Model
      faceDetectionModel.run(faceDetectionInputs)
      let faceDetectionOutputs = faceDetectionModel.getOutputDataArray()
      
      // (4) Postprocess model run result
      let faceDetectionPostprocessed = faceDetectionFeature.postprocess(&faceDetectionOutputs)
      

Conclusion#

With ZETIC.MLange, building your own on-device AI applications with NPU utilization is incredibly easy. We’ve developed a custom OpenCV module and an ML application pipeline, making the implementation of models like face detection remarkably simple and efficient. This streamlined approach allows you to integrate advanced features with minimal effort. We’re continually uploading new models to our examples and HuggingFace page. Stay tuned, and contact us for collaborations!