2017-10-04 139 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中有這樣做的方法。