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

标签 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)

但是这仅适用于横向。我不知道为什么它在所有其他方向上不起作用。我的意思是方向应该设置正确。我还将 UIImage 转换为 CIImage。

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

问题出在哪里吗?

最佳答案

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

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

关于ios - 由于图像方向而无法检测图像中的面部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39809513/

相关文章:

ios - 在裁剪之前调整 UIImage 或 CIImage 的大小会拉伸(stretch)图像吗?

iphone - UIView animateWithDuration 不考虑延迟

ios - 如何在 Settings.bundle 中的 xCode 中添加额外的 plists 以便与 InAppSettingsKit iPhone 库一起使用?

ios - UIViewController 与 NSViewController

Swift:动画 uilayoutconstraints 和更改 alpha 会导致崩溃

Swift TableView Cell - 显示事件微调器、执行函数、隐藏事件微调器

ios - 从 .m4a 数据中过滤频率

ios - Core Image 颜色内核的 Metal 着色语言,如何传递 float3 数组

ios - UIButton 向左拐角时消失

ios - 通过 Core Image 的 HDR 图像?