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

And*_*rra 1 ios swift4 apple-vision

我已经创建了以下链接中提供的 Xcode 项目,以便探索 Apple 使用 Xcode9 发布的新 Vision Framework。它基于我在 GitHub 和 Youtube 上找到的代码所做的调整,但我无法理解为什么 VNImageRequestHandler 上的方向参数上的 CGImagePropertyOrientation.right 。有人可以阐明这一点吗?

GitHub Xcode 项目

And*_*rra 6

我通过浏览 Apple 的文档找到了自己问题的答案UIImage.imageOrientationCGImagePropertyOrientation。将其发布在这里,以防其他人遇到同样的问题:底线是,虽然这两个属性都与图像方向相关,但它们分配给枚举的实际值不同。例如,一个UIImage.imageOrientation.upwill 映射到值 0,一个CGImagePropertyOrientation.upwill 映射到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
    }
}
Run Code Online (Sandbox Code Playgroud)

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