【问题标题】:Why is Face detection only working with CGImagePropertyOrientation.right?为什么人脸检测仅适用于 CGImagePropertyOrientation.right?
【发布时间】:2018-03-16 07:55:32
【问题描述】:

我在下面的链接中创建了 Xcode 项目,以探索 Apple 使用 Xcode9 发布的新视觉框架。它基于对我在 GitHub 和 Youtube 上找到的代码所做的调整,但我不明白为什么 CGImagePropertyOrientation.right 在 VNImageRequestHandler 的方向参数上。有人能解释一下吗?

GitHub Xcode project

【问题讨论】:

    标签: ios swift4 apple-vision


    【解决方案1】:

    我通过 Apple 的 UIImage.imageOrientationCGImagePropertyOrientation 文档找到了我自己问题的答案。在这里发布它以防其他人遇到同样的问题: 底线是虽然这两个属性都与图像方向有关,但它们的枚举具有不同的实际值。例如,UIImage.imageOrientation.up 将映射到值 0,CGImagePropertyOrientation.up 将映射到 1。我处理它的方法是构建这个自定义“翻译”函数,该函数返回从UIImage.imageOrientation 输入到相应CGImagePropertyOrientation 值的映射:

    func getCGOrientationFromUIImage(_ image: UIImage) -> CGImagePropertyOrientation {
        // returns que equivalent CGImagePropertyOrientation given an UIImage.
        // This is required because UIImage.imageOrientation values don't match to CGImagePropertyOrientation values
        switch image.imageOrientation {
        case .down:
            return .down
        case .left:
            return .left
        case .right:
            return .right
        case .up:
            return .up
        case .downMirrored:
            return .downMirrored
        case .leftMirrored:
            return .leftMirrored
        case .rightMirrored:
            return .rightMirrored
        case .upMirrored:
            return .upMirrored
        }
    }
    

    GitHub 示例已更新。现在无论使用哪个方向拍照都可以识别人脸。

    【讨论】:

    • 这不起作用,因为您正在尝试使用一个 Int(来自 imageOrientation),而您应该提供一个 UInt32(CGImagePropertyOrientation)。在这种情况下编译器会抛出错误。
    • Apple 的示例代码具有允许相同转换的扩展。 developer.apple.com/documentation/vision/…
    • extension UIImageOrientation { var cgImagePropertyOrientation: CGImagePropertyOrientation { switch self { case .down: return .down case .left: return .left case .right: return .right case .up: return .up case .downMirrored: return .downMirrored case .leftMirrored: return .leftMirrored case .rightMirrored: return .rightMirrored case .upMirrored: return .upMirrored } } }
    • 使用image.imageOrientation.cgImagePropertyOrientation
    • 或扩展图片extension UIImage { var cgImagePropertyOrientation: CGImagePropertyOrientation { switch imageOrientation { case .down: return .down case .left: return .left case .right: return .right case .up: return .up case .downMirrored: return .downMirrored case .leftMirrored: return .leftMirrored case .rightMirrored: return .rightMirrored case .upMirrored: return .upMirrored } } }
    【解决方案2】:

    人脸检测需要了解所需的人脸方向,这通常与图像的预期显示方向相匹配。 (也就是说,大多数人,大多数时候,都在人脸朝上的地方拍照。)大多数人脸检测软件,包括视觉,都经过优化,可以寻找正面朝上的人脸,因为它更容易获得寻找任意方向的面孔时会出现误报。

    Vision 的面部检测功能更喜欢您指定方向。 AFAICT 如果您根本不指定,他们使用默认的.up 方向。如果您的图像仅在 .right 是指定方向时才能正确识别,则这些图像可能具有 EXIF 元数据,表明它们的首选显示方向不一定与捕获它们的本机传感器方向相同。

    如果您的图像以UIImage 从存储/URL 加载开始,您可以使用UIImageOrientation 询问图像的预期显示方向,并将其转换为CGImagePropertyOrientation 以传递给Vision 方法。在 Apple 的Vision sample code 中有一种方法可以做到这一点。

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 2020-05-14
      • 2017-08-29
      • 2020-06-24
      • 2019-02-20
      • 2019-11-18
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多