2012-05-24 77 views
4

我試圖實現內置的iOS 5人臉檢測API。我正在使用UIImagePickerController的實例來允許用戶拍照,然後我試圖使用CIDetector來檢測面部特徵。不幸的是,featuresInImage總是返回大小爲0CIDetector和UIImagePickerController

這裏的數組的代碼:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage]; 

NSNumber *orientation = [NSNumber numberWithInt: 
         [picture imageOrientation]]; 
NSDictionary *imageOptions = 
[NSDictionary dictionaryWithObject:orientation 
          forKey:CIDetectorImageOrientation]; 

CIImage *ciimage = [CIImage imageWithCGImage:[picture CGImage] 
            options:imageOptions]; 


NSDictionary *detectorOptions = 
[NSDictionary dictionaryWithObject:CIDetectorAccuracyLow 
          forKey:CIDetectorAccuracy]; 
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace 
              context:nil 
              options:detectorOptions]; 

NSArray *features = [detector featuresInImage:ciimage]; 
NSLog(@"Feature size: %d", features.count); 
} 

這總是返回0的功能。但是,如果我使用內置於應用程序中的文件使用UIImage,則臉部檢測效果很好。

我正在使用此代碼Pragmatic Bookshelf article

對於它的價值,我認爲錯誤是當我將UIImage從相機轉換爲CIImage時,但它可能是任何東西。

回答

17

@tonyc您的解決方案只適用於「UIImageOrientationRight」的特定情況。問題來自UIImage和CIDetectorImageOrientation之間的區別。從iOS的文檔:

CIDetectorImageOrientation

用於指定要檢測其特徵的圖像的顯示方向鍵。此密鑰 是一個NSNumber對象,具有與TIFF和 EXIF規範相同的值;值的範圍可以從1到8.值 指定圖像的原點(0,0)的位置。如果不存在 ,則默認值爲1,這表示圖像 的原點位於左上方。有關每個值指定的圖像原點的詳細信息,請參閱kCGImagePropertyOrientation。

適用於iOS 5.0及更高版本。

在CIDetector.h中聲明。

所以,現在的問題是這兩個方向之間的轉換,這裏是我在我的代碼做了,我測試和它的工作對所有方向:

int exifOrientation; 
switch (self.image.imageOrientation) { 
    case UIImageOrientationUp: 
     exifOrientation = 1; 
     break; 
    case UIImageOrientationDown: 
     exifOrientation = 3; 
     break; 
    case UIImageOrientationLeft: 
     exifOrientation = 8; 
     break; 
    case UIImageOrientationRight: 
     exifOrientation = 6; 
     break; 
    case UIImageOrientationUpMirrored: 
     exifOrientation = 2; 
     break; 
    case UIImageOrientationDownMirrored: 
     exifOrientation = 4; 
     break; 
    case UIImageOrientationLeftMirrored: 
     exifOrientation = 5; 
     break; 
    case UIImageOrientationRightMirrored: 
     exifOrientation = 7; 
     break; 
    default: 
     break; 
} 

NSDictionary *detectorOptions = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh }; // TODO: read doc for more tuneups 
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions]; 

NSArray *features = [faceDetector featuresInImage:[CIImage imageWithCGImage:self.image.CGImage] 
              options:@{CIDetectorImageOrientation:[NSNumber numberWithInt:exifOrientation]}]; 
+0

+1優秀的答案。 –

+0

什麼是self.image.imageOrientation你能解釋一下嗎? –

2

果然,在花了一天的時間研究這個並被難倒之後,我發現了一個小時後的解決方案。

我最終注意到臉部檢測確實在橫向上工作,但不是在肖像中工作。

原來我需要這些選項:

NSDictionary *imageOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:6] 
                 forKey:CIDetectorImageOrientation]; 
NSArray *features = [detector featuresInImage:ciimage options:imageOptions]; 
0

我也有類似的問題 - 人臉檢測器在未形象定位屬性相匹配的方向努力。 iOS face detector orientation and setting of CIImage orientation

快速解決辦法,我發現以幫助是不顧形象定位都在一起,「扁平化」的圖像(所以不存在形象定位數據)

我用這個代碼:

http://blog.logichigh.com/2008/06/05/uiimage-fix/

果然看起來是工作多的前置攝像頭圖片更好。我還沒有嘗試足夠的景觀,以告訴它是否有幫助,但看起來很有希望

0

斯威夫特3版

var exifOrientation : Int 
switch (inputImage.imageOrientation) 
{ 
case UIImageOrientation.up: 
    exifOrientation = 1; 
case UIImageOrientation.down: 
    exifOrientation = 3; 
case UIImageOrientation.left: 
    exifOrientation = 8; 
case UIImageOrientation.right: 
    exifOrientation = 6; 
case UIImageOrientation.upMirrored: 
    exifOrientation = 2; 
case UIImageOrientation.downMirrored: 
    exifOrientation = 4; 
case UIImageOrientation.leftMirrored: 
    exifOrientation = 5; 
case UIImageOrientation.rightMirrored: 
    exifOrientation = 7; 
} 

let ciImage = CIImage(cgImage: inputImage.cgImage!) 
let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh] 
let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)! 
let faces = faceDetector.features(in: ciImage, options:["CIDetectorImageOrientation":exifOrientation]) 
相關問題