Core Image Face Detector No Result

265 Views Asked by At

I am trying to detect faces in an image using CIDetector

However nothing seems to work.

Here is the code:

if let inputImage = UIImage(named: "taylor-swift") {
    let ciImage = CIImage(cgImage: inputImage.cgImage!)

    let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)!

    let faces = faceDetector.features(in: ciImage)

    if let face = faces.first as? CIFaceFeature {
        print("Found face at \(face.bounds)")

        if face.hasLeftEyePosition {
            print("Found left eye at \(face.leftEyePosition)")
        }

        if face.hasRightEyePosition {
            print("Found right eye at \(face.rightEyePosition)")
        }

        if face.hasMouthPosition {
            print("Found mouth at \(face.mouthPosition)")
        }
    }
}

I have tested these images:

enter image description here

enter image description here

enter image description here

1

There are 1 best solutions below

1
On

This happened because I was running it on a simulator. It works correctly on device.

Note, this also applies to the Vision framework