CIDetector和UIImagePickerController

我试图实现内置的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个function。 但是,如果我从应用程序内置的文件中使用UIImage,则脸部检测效果很好。

我正在使用这个实用书架文章的代码。

对于它的价值,我认为错误是当我从相机的UIImage转换为CIImage,但它可以是任何东西。

@tonyc您的解决scheme仅适用于“UIImageOrientationRight”的特定情况。 问题来自UIImage和CIDetectorImageOrientation之间的区别。 从iOS文档:

CIDetectorImageOrientation

用于指定要检测其function的图像的显示方向的键。 这个键是一个NSNumber对象,其值与TIFF和EXIF规定的值相同; 值的范围可以从1到8.该值指定图像的原点(0,0)所在的位置。 如果不存在,则默认值为1,这意味着图像的原点是左上angular。 有关每个值指定的图像原点的详细信息,请参阅kCGImagePropertyOrientation。

在iOS 5.0及更高版本中可用。

在CIDetector.h中声明。

所以现在的问题是这两个方向之间的转换,这是我在我的代码中做的,我testing过了,它适用于所有的方向:

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

果然,花了一天的时间看着这个并被难住,我发现一个小时后发布这个解决scheme。

我最终注意到,人脸检测确实在横向上工作,而不是在肖像上。

原来我需要这些选项:

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

我有一个类似的问题 – 面部检测器的工作方向与图像方向属性不匹配。 iOS人脸检测器的方向和CIImage方向的设置

一个快速的工作,我发现帮助,这是无视图像方向所有在一起,并“图像”(所以没有图像的方向数据)

我用这个代码:

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

看起来好像对于前置摄像头来说效果更好。 我还没有尝试足够的景观,看看它是否有帮助,但看起来很有希望

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