iOS Face Counter [CIDetector|Swift]

Nipun Ruwanpathirana
2 min readMar 31, 2020

In this short tutorial, you will learn how to get the number of faces from a UIImage using CIDetector and Swift. CIDetector supports on-device face detection.

If you want to learn how to get the number of faces in Android, Follow this

FaceCountManager

Write a Singleton class and a method to get the face count by passing UIImage as a parameter.

class FaceCountManager {    static let shared = FaceCountManager()    private init() {}    func detectFaceCount(image: UIImage) -> Int {
guard let personciImage = CIImage(image: image) else {
return 0
}
let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh] let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy) let faces = faceDetector!.features(in: personciImage) return faces.count }}

Usage

You can call above class with UIImage image as below:

val image: UIImage// Get your image.
let faceCount = self.detectFaces(image: image)

From this post, we’ve learned how to get the number of faces in given UIImage using CIDetector and Swift.

If you want to learn how to get the number of faces in Android, Follow this

Thank you for reading.

--

--