ios - 为什么人脸检测只能与 CGImagePropertyOrientation.right 一起使用?

标签 ios swift4 apple-vision

为了探索 Apple 随 Xcode9 发布的新 Vision Framework,我创建了可在下方链接中获取的 Xcode 项目。它基于对我在 GitHub 和 Youtube 上找到的代码所做的调整,但我不明白为什么 CGImagePropertyOrientation.right 在 VNImageRequestHandler 的方向参数上。有人可以阐明这一点吗?

GitHub Xcode project

最佳答案

我通过 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 示例已更新。现在,无论使用哪个方向拍照,都可以识别人脸。

关于ios - 为什么人脸检测只能与 CGImagePropertyOrientation.right 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573044/

相关文章:

machine-learning - Apple Vision 创建自定义 VNImageBasedRequest

ios - 使用 UIPanGuesture 将 UIView 拖放到另一个 UIView

ios - 第一次使用后 ViewController 没有改变

ios - 如何安排哪部分代码先工作

ios - SwiftUI:@State 已修改但在阅读之后的行时看似未更改

IOS:UISearchBar 部分覆盖带有提示的 UINavigationBar

ios - 滚动时 UITableViewCell contenview 上的阴影率增加

ios - 使用 Vision 框架时在 Xcode 中获取控制台垃圾邮件并且脸部移出屏幕

swift - 从随机排列的数组中设置按钮的标题 - Swift

ios - 获取在 VNDetectTextRectanglesRequest 完成处理程序上的 VNImageRequestHandler 中使用的 cvPixelBuffer