Swift:CoreImage人脸检测会将每个圆形物体检测为人脸

时间:2018-09-05 07:38:36

标签: ios swift4 core-image

在我的应用中,我想对UIImage使用人脸检测,因此我使用库中的 CoreImage 构建(我知道Vision Library也是最新的人脸检测库。但是它仅支持ios 11.0及更高版本)。该库运行良好,直到我发现某些表面呈圆形的图像都被检测为人脸为止。

这是我的代码:

func checkFaceDetection(image: UIImage) -> Bool {
        var isFace = false
        let personciImage = CIImage(image: image)

        let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
        let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy)
        let faces = faceDetector?.features(in: personciImage!)

        if let face = faces?.first as? CIFaceFeature {
            print("Found face at \(face.bounds)")
            if face.hasLeftEyePosition && face.hasRightEyePosition && face.hasMouthPosition && face.hasFaceAngle{
                isFace = true
            }else{
                isFace = false
            }
        }
        return isFace
    }

即使我使用左眼,右眼,嘴巴和faceAngle过滤了该图像,仍然无法解决我的问题。

2 个答案:

答案 0 :(得分:1)

您可以将CIDetectorAccuracyHigh更改为Low。会更有用

答案 1 :(得分:-1)

let imageOptions = NSDictionary(对象:NSNumber(值:5)作为NSNumber,forKey:CIDetectorImageOrientation作为NSString)