iOS面部识别问题

10

我正在尝试在iOS 5中使用CoreImage的人脸检测功能,但它没有检测到任何内容。我正在尝试使用以下代码在刚刚捕获的相机图像中检测人脸:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSDictionary *detectorOptions = [[NSDictionary alloc] initWithObjectsAndKeys:CIDetectorAccuracyHigh, CIDetectorAccuracy, nil];     
    CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions];
    NSArray *features = [faceDetector featuresInImage:image.CIImage];
    NSLog(@"Features = %@", features);
    [self dismissModalViewControllerAnimated:YES];
}

这段代码可以编译并运行,但是无论图像里有什么内容,features数组总是为空... 有什么想法吗?

4个回答

22

我无法直接回复您在@14:52发表的评论,Vic320。但是我一直在尝试使用前置摄像头进行人脸检测,但我一直处于困境之中,因为前置摄像头根本无法捕捉到我的面部信息...

事实证明,它对旋转非常敏感-当我竖直地握着iPad2(使用前置摄像头时应该这样做)时,识别准确率不到10%。后来我随手将其横过来,结果前置摄像头的识别准确率达到了100%。

如果您总是在纵向模式下使用前置摄像头,则可以通过添加以下代码片段轻松解决此问题:

NSDictionary* imageOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:6] forKey:CIDetectorImageOrientation];
NSArray* features = [detector featuresInImage:image options:imageOptions];
那个 6 强制探测器在纵向模式下运行。若需要动态地判断设备方向,Apple 的 SquareCam 示例有很多实用方法可供使用。

That 6 in there forces the detector to operate in portrait mode. Apple's SquareCam Sample has a whole bunch of utility methods to figure out what orientation you're in if you need it to dynamically figure out your orientation.


今天仍然相关!绝对精彩。 - Youssef Moawad

5

好的,仔细阅读文档总是很有帮助的。在UIImage的文档中,在CIImage属性下面写着:“如果UIImage对象是使用CGImageRef初始化的,则该属性的值为nil。”显然,UIImagePickerController确实会使用CGImageRef初始化图像,因此该属性的确为nil。为了使上述代码正常工作,您需要添加:

CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];

并更改这一行:
NSArray *features = [faceDetector featuresInImage:ciImage];

我注意到的另一个重要问题是,从前置摄像头拍摄的低分辨率图像上进行人脸检测并不真正起作用!但使用后置高分辨率相机时,每次都能正常工作。也许算法是针对高分辨率进行调整的...


4
请尝试以下方法。假设您将照片加载到image变量中:
NSDictionary *options = [NSDictionary dictionaryWithObject: CIDetectorAccuracyLow forKey: CIDetectorAccuracy];
            CIDetector  *detector = [CIDetector detectorOfType: CIDetectorTypeFace context: nil options: options];

        CIImage *ciImage = [CIImage imageWithCGImage: [image CGImage]];
        NSNumber *orientation = [NSNumber numberWithInt:[image imageOrientation]+1];
        NSDictionary *fOptions = [NSDictionary dictionaryWithObject:orientation forKey: CIDetectorImageOrientation];
            NSArray *features = [detector featuresInImage:ciImage options:fOptions];
            for (CIFaceFeature *f in features) {

                NSLog(@"left eye found: %@", (f. hasLeftEyePosition ? @"YES" : @"NO"));

                NSLog(@"right eye found: %@", (f. hasRightEyePosition ? @"YES" : @"NO"));

                NSLog(@"mouth found: %@", (f. hasMouthPosition ? @"YES" : @"NO"));

                if(f.hasLeftEyePosition)

                    NSLog(@"left eye position x = %f , y = %f", f.leftEyePosition.x, f.leftEyePosition.y);

                if(f.hasRightEyePosition)

                    NSLog(@"right eye position x = %f , y = %f", f.rightEyePosition.x, f.rightEyePosition.y);

                if(f.hasMouthPosition)

                    NSLog(@"mouth position x = %f , y = %f", f.mouthPosition.x, f.mouthPosition.y);

            }

1

以上答案都对我没用(iOS 8.4)iPad mini和iPad Air 2

我和robwormald有同样的观察。当iPad旋转时,人脸检测可以工作,所以我旋转了ciImage :)

let ciImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments)
let angle = CGFloat(-M_PI/2)
let rotatedImage = ciImage.imageByApplyingTransform(CGAffineTransformMakeRotation(angle))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接