首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CIDetector和UIImagePickerController

CIDetector和UIImagePickerController
EN

Stack Overflow用户
提问于 2012-05-25 06:32:26
回答 4查看 7.4K关注 0票数 5

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

代码如下:

代码语言:javascript
复制
- (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时,但它可以是任何东西。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-06-10 15:47:51

@tonyc您的解决方案仅适用于特定情况,即"UIImageOrientationRight“。问题来自于UIImage和CIDetectorImageOrientation中的方向不同。来自iOS文档:

CIDetectorImageOrientation

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

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

在CIDetector.h中声明。

所以现在的问题是这两个方向之间的转换,这是我在代码中做的,我测试了它,它对所有方向都有效:

代码语言:javascript
复制
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]}];
票数 19
EN

Stack Overflow用户

发布于 2012-05-25 08:28:08

果然,在花了一天的时间研究这个问题并被难住之后,我在发布这篇文章一个小时后找到了解决方案。

我最终注意到人脸检测在风景中起作用,但在肖像中不起作用。

事实证明我需要这些选项:

代码语言:javascript
复制
NSDictionary *imageOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:6]
                                                         forKey:CIDetectorImageOrientation];
NSArray *features = [detector featuresInImage:ciimage options:imageOptions];
票数 2
EN

Stack Overflow用户

发布于 2017-03-29 06:28:06

Swift 3版本

代码语言:javascript
复制
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])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10746212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档