2017-10-04 137 views

回答

0

我已经找到了答案,以我自己的问题去府通苹果对UIImage.imageOrientation和CGImagePropertyOrientation文档。在此处张贴以防其他人遇到相同问题: 底线是这两个属性与图像方向有关,但它们具有不同的实际值,分配给它们的枚举。例如,一个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的样品。无论使用何种方向拍摄照片,现在都可以识别人脸。

此致敬礼。

+0

这不起作用,因为您正尝试使用Int(来自imageOrientation),您希望提供一个UInt32(CGImagePropertyOrientation)。编译器在这种情况下会引发错误。 –

+0

Apple的示例代码具有允许进行相同转换的扩展名。 https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml –

+1

'扩展UIImageOrientation {VAR cgImagePropertyOrientation:CGImagePropertyOrientation { 开关自{ 情况下.down: 回报.down 情况。左: 回报。左 情况.right: 返回.right 情况.UP: 返回.UP 情况.downMirrored: 返回.downMirrored 情况.leftMirrored: 返回.leftMirrored 情况.rightMirrored: 返回.rightMirrored 的情况。upMirrored: 返回.upMirrored } } }' –

1

人脸检测需要知道人脸所需的方向,这通常与图像的预期显示方向相匹配。 (也就是说,大多数人在大多数情况下会在脸部正面朝上的地方拍照)。大多数人脸检测软件(包括Vision)都经过优化,可以查找正面朝上的脸,因为它更容易获得当你正在寻找任何方向的面孔时误报。

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

如果您的图片开始从存储/ URL加载UIImage,你可以问图像预期显示方向是什么UIImageOrientation,并且将其转换成CGImagePropertyOrientation传递到视力的方法。在苹果的Vision sample code中有这样做的方法。