Skip to content

keithito/SwiftMaskDetection

Repository files navigation

SwiftMaskDetection

Version License Platform

SwiftMaskDetection is a face mask detection library with a Swift interface.

It is a port of AIZOO's FaceMaskDetection model to CoreML. The model runs at over 30fps on recent iPhones and iPads. For more information on the model and training data, please see https://github.com/AIZOOTech/FaceMaskDetection (AIZOO did all the hard work).

Demo

Demo video Demo video

To run the demo:

  1. Make sure you have Xcode and CocoaPods.

  2. Clone this repo and open the example project:

    git clone https://github.com/keithito/SwiftMaskDetection.git
    cd SwiftMaskDetection/Example 
    pod install
    open SwiftMaskDetection.xcworkspace
    
  3. Run the project from XCode on a device (it needs the camera)

    • If you see an error that signing needs a development team, open the "SwiftMaskDetection" project, click on the "Signing & Capabilities" tab, and select an option from the "Team" menu.

Installation

SwiftMaskDetection is available through CocoaPods. To install it, add the following line to your Podfile:

pod 'SwiftMaskDetection'

If you don't use CocoaPods, you can simply copy the files in SwiftMaskDetection/Classes into your Xcode project.

Usage

Images

To recognize an image:

import SwiftMaskDetection

let detector = MaskDetector()
let image = UIImage(named: "my_photo")!
if let results = try? detector.detectMasks(cgImage: image.cgImage!) {
  // Do something with the results.
}

The image must be 260x260 pixels. detectMasks supports CGImage, CIImage, and CVPixelBuffer inputs and returns an array of Results, one for each detected face:

public struct Result {
  /// The status of the detection (.mask or .noMask)
  public let status: Status

  /// The bounding box of the face in normalized coordinates (the top-left corner of the image
  /// is [0, 0], and the bottom-right corner is [1, 1]).
  public let bound: CGRect

  /// Value between 0 and 1 representing the confidence in the result
  public let confidence: Float
}

Video

MaskDetectionVideoHelper may come in handy for running on live video. First, create the helper:

let helper = MaskDetectionVideoHelper(maskDetector: MaskDetector())

Then call detectInFrame on each video frame:

if let results = try? helper.detectInFrame(cmSampleBuffer) {
  // Do something with the results.
}

You don't need to resize the image to 260x260; the helper does that for you. See the example app's ViewController for a complete example.

Requirements

  • iOS 13 or later

License

SwiftMaskDetection is available under the MIT license. See the LICENSE file for more info.

The face mask detection model is (c) 2020 AIZOOTech and is also available under the MIT License.