【问题标题】:CIDetector with AVCaptureOutput带有 AVCaptureOutput 的 CIDetector
【发布时间】:2015-01-17 12:16:21
【问题描述】:

我正在尝试使用 AVFoundation 和 CIDetector 从视频输入中逐帧检测微笑。 CIDetector 在 captureOutput 中使用时在准确性方面非常不一致。根据光线、与相机的距离以及照片中的其他面孔,相同的面部表情可以很快地从微笑转变为不微笑。有没有办法让 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 是一个单例,用于在其他 ViewController 中使用相同的 CIDetector 实例。我将检测器精度设置为低,因为高太慢。

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

【问题讨论】:

  • 也许只是改变准确性? @{CIDetectorAccuracy:CIDetectorAccuracyHigh}
  • 试过了,太慢了:在 CIDetector 分析最后一帧之前帧更新。

标签: ios objective-c avfoundation


【解决方案1】:

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

【讨论】:

  • 不,如果你添加自动调整调用(`let adjusts = sourceImg.autoAdjustmentFilters()`),它会进一步延迟执行
猜你喜欢
  • 2017-03-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 2018-08-20
  • 2023-03-27
  • 2017-04-14
相关资源
最近更新 更多