带AVCaptureOutput的CIDetector

时间:2015-01-17 12:16:22

标签: ios objective-c avfoundation

我试图通过AVFoundation和CIDetector的视频输入逐帧检测微笑。在captureOutput中使用时,CIDetector在准确性方面非常不一致。根据照明,相机距离和照片中的其他面部,相同的面部表情可以从微笑反弹到不快速微笑。有没有办法让CIDetector不那么紧张而不会让它慢得多?

@interface CameraViewController () {
    BOOL smiling;
}

...

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
    CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary *)attachments];

    if (attachments)
        CFRelease(attachments);

    //CIImage translates all the pixels -90 deg; set orientation taking this into account

    NSNumber *orientation;
    switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationPortrait:
            orientation = @6;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            orientation = @8;
            break;
        case UIDeviceOrientationLandscapeLeft:
            orientation = @3;
            break;
        case UIDeviceOrientationLandscapeRight:
            orientation = @1;
            break;
        default:
            orientation = @6;
            break;
    }

    NSArray *faces = [[[SmileDetector sharedDetector] detector] featuresInImage:ciImage
                                                                        options:@{CIDetectorSmile: @(YES), CIDetectorImageOrientation:orientation}];
    smiling = NO;
    for (CIFaceFeature* face in faces)
        if (face.hasSmile) {
            smiling = YES;
            break;
        }
}

SmileDetector是在其他ViewControllers中使用相同CIDetector实例的单例。我将探测器精度设置为低,因为高速太慢。

- (id)init {
    if (self = [super init]) {
        detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                      context:[CIContext contextWithOptions:nil]
                                      options:@{CIDetectorAccuracy:CIDetectorAccuracyLow}];
    }
    return self;
}

1 个答案:

答案 0 :(得分:0)

我目前正在处理同样的问题,我发现在CIDetector完成工作之前预处理帧以稳定对比度和亮度效果非常好。我的项目是静止帧,我不知道它对视频帧有多好......