2016-10-01 92 views
0

我想使用此代碼通過CIFaceDetector檢測圖像中的人臉。由於圖像方向無法檢測圖像中的人臉

let Orientation: Int! 

    switch ((info[UIImagePickerControllerOriginalImage] as? UIImage)?.imageOrientation)! { 
    case UIImageOrientation.up: 
     Orientation = 1 
    case UIImageOrientation.down: 
     Orientation = 3 
    case UIImageOrientation.left: 
     Orientation = 8 
    case UIImageOrientation.right: 
     Orientation = 6 
    case UIImageOrientation.upMirrored: 
     Orientation = 2 
    case UIImageOrientation.downMirrored: 
     Orientation = 4 
    case UIImageOrientation.leftMirrored: 
     Orientation = 5 
    case UIImageOrientation.rightMirrored: 
     Orientation = 7 
    } 

    let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorImageOrientation: Orientation] as [String : Any] 
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options) 
    let faces = faceDetector?.features(in: personciImage) 

但是,這隻適用於風景權。我不知道爲什麼它不適用於所有其他方向。我的意思是應該設置正確的方向。我也將一個UIImage投射到CIImage上。

guard let personciImage = CIImage(image: (info[UIImagePickerControllerOriginalImage] as? UIImage)!) else { 
     return 
    } 

問題在那裏?

回答

0

CIDetector配置文檔here它似乎是方向鍵實際上不是這個類的選項。很有可能人臉檢測類實際上更喜歡讓你在通過它之前旋轉圖像。

旋轉圖像相當簡單,並且可以在SO上搜索,我找到了一個快速示例,但是我相信還有更多! quick rotation search example