由于图像方向而无法检测图像中的面部

时间:2016-10-01 17:26:18

标签: ios swift core-image uiimageorientation cidetector

我想使用此代码通过CIFaceDetector检测图像中的面部。

    let Orientation: Int!

    switch ((info[UIImagePickerControllerOriginalImage] as? UIImage)?.imageOrientation)! {
    case UIImageOrientation.up:
        Orientation = 1
    case UIImageOrientation.down:
        Orientation = 3
    case UIImageOrientation.left:
        Orientation = 8
    case UIImageOrientation.right:
        Orientation = 6
    case UIImageOrientation.upMirrored:
        Orientation = 2
    case UIImageOrientation.downMirrored:
        Orientation = 4
    case UIImageOrientation.leftMirrored:
        Orientation = 5
    case UIImageOrientation.rightMirrored:
        Orientation = 7
    }

    let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorImageOrientation: Orientation] as [String : Any]
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)
    let faces = faceDetector?.features(in: personciImage)

然而,这仅适用于景观。我不知道为什么它不适用于所有其他方向。我的意思是方向应该正确。我也在向CIImage施放UIImage。

 guard let personciImage = CIImage(image: (info[UIImagePickerControllerOriginalImage] as? UIImage)!) else {
        return
    }

那里有问题吗?

1 个答案:

答案 0 :(得分:0)

CIDetector配置文档here看来,方向键实际上并不是此类的选项。面部检测类实际上可能更喜欢您在传入图像之前旋转图像。

旋转图像非常简单,也可以在SO上搜索,我找到了一个简单的例子,但我确信还有更多! quick rotation search example