【问题标题】:CIDetector and UIImagePickerControllerCIDetector 和 UIImagePickerController
【发布时间】:2012-05-31 13:14:49
【问题描述】:

我正在尝试实现内置的 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 时,但它可能是任何东西。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

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

    原来我需要这些选项:

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

    【讨论】:

    • Swift4.2 var imageOptions = [CIDetectorImageOrientation : NSNumber(value: 6)] var features = faceDetector!.features(in: myImage, options: imageOptions as? [String : 任何])
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      我发现有一个快速解决方法是忽略图像方向并“压平”图像(这样就没有图像方向数据)

      我使用了这个代码:

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

      果然看起来它对于前置摄像头照片的效果要好得多。我在景观方面的尝试还不够多,无法判断它是否对那里有所帮助,但看起来很有希望

      【讨论】:

      • 您的链接已损坏
      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 2017-04-14
      • 2013-11-27
      • 2012-01-15
      相关资源
      最近更新 更多