CIDetector和UIImagePickerController

时间:2012-05-24 22:32:26

标签: ios orientation face-detection core-image cifacefeature

我正在尝试实现内置的iOS 5人脸检测API。我正在使用UIImagePickerController的实例来允许用户拍照,然后我尝试使用CIDetector来检测面部特征。不幸的是,featuresInImage总是返回一个大小为0的数组。

以下是代码:

- (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时,但它可能是任何东西。

4 个答案:

答案 0 :(得分:19)

@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]}];

答案 1 :(得分:2)

果然,在花了一天时间研究这个问题并且感到难过之后,我发布了一个小时后找到了解决方案。

我最终注意到人脸检测确实在横向上有效,但不是在纵向上。

原来我需要这些选项:

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

答案 2 :(得分:1)

Swift 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])

答案 3 :(得分:-1)

我遇到了类似的问题 - 面部检测器的工作方向与图像方向属性不匹配。 iOS face detector orientation and setting of CIImage orientation

快速解决一下我发现帮助的方法就是一起忽略图像方向并“平整”图像(这样就没有图像方向数据)

我使用了这段代码:

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

肯定看起来它对于前置摄像头图片效果更好。我没有在风景上做足够的尝试来判断它是否有帮助,但看起来很有希望