Swift 4-如何检查眼睛是否闭合

时间:2018-10-23 06:10:06

标签: swift

我使用Core Image来检测人脸,它也能成功

但是我无法检测到眼睛是闭合的。 :(

我想检测面部并检查眼睛是否闭合, 非常感谢你!

代码:

    func detect(ciimage:CIImage) {
    let imageOptions =  NSDictionary(object: NSNumber(value: 5) as NSNumber, forKey: CIDetectorImageOrientation as NSString)
    let personciImage = ciimage
    let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy)
    let faces = faceDetector?.features(in: personciImage, options: imageOptions as? [String : AnyObject])

    //success
    if let face = faces?.first as? CIFaceFeature {


        hasFace = true

        // face.leftEyeClosed not work,It always outputs false
        if face.leftEyeClosed {
            lefteyeclosed = true
        }else{
            lefteyeclosed = false
        }

        // face.leftEyeClosed not work
        if face.rightEyeClosed{
            lefteyeclosed = true
        }else{
            lefteyeclosed = false
        }
    } else {
        hasFace = false
    }
}

我修改了以下代码:

    let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy)
    let faces = faceDetector?.features(in: ciimage, options: [CIDetectorEyeBlink : true, CIDetectorImageOrientation: 5])

1 个答案:

答案 0 :(得分:1)

来自leftEyeClosed documentation

  

要检测到闭合的眼睛,必须在传递给检测器的features(in:options :)方法的字典中以键值为true的CIDetectorEyeBlink出现。

因此,您的选择应为:

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