正确使用CIDetectorTracking

Mic*_*lum 16 objective-c face-detection ios

Apple最近在CIDetector类中添加了一个新的常量,CIDetectorTracking它似乎能够跟踪视频中帧之间的面.如果我能弄清楚它是如何工作的,这对我来说非常有益.

我已经尝试将这个键添加到探测器选项字典中,使用我能想到的远程相关的每个对象,包括我的AVCaptureStillImageOutput实例,我正在处理的UIImage,YES,1等.

NSDictionary *detectorOptions = [[NSDictionary alloc] initWithObjectsAndKeys:CIDetectorAccuracyHigh, CIDetectorAccuracy,myAVCaptureStillImageOutput,CIDetectorTracking, nil];
Run Code Online (Sandbox Code Playgroud)

但无论我尝试传递什么参数,它都会崩溃(显然我在这里猜测它)或调试器输出:

指定了未知的CIDetectorTracking.忽略.

通常情况下,我不会猜测,但这个主题的资源实际上是不存在的.Apple的课程参考说明:

用于启用或禁用检测器的面部跟踪的键.如果要跟踪视频中帧的面,请使用此选项.

除了可用性是iOS 6+和OS X 10.8+之外.

里面的评论CIDetector.h:

/*用于指定要素跟踪的选项字典中的键应该被使用.*/

如果这还不够糟糕,谷歌搜索提供了7个结果(当他们发现这篇文章时有8个)所有这些都是Apple类引用,API差异,SO帖子询问如何在iOS 5中实现这一点,或者第三方副本前者

所有这一切,任何正确使用的提示或技巧CIDetectorTracking将不胜感激!

Fel*_*lix 19

你是对的,这个关键字没有很好的记录.除了API文档之外,它也没有在下面解释:

我尝试了不同的值,CIDetectorTracking唯一可接受的值似乎是@(YES)@(NO).使用其他值,它会在控制台中打印此消息:

指定了未知的CIDetectorTracking.忽略.

将值设置为时,@(YES)应使用检测到的面部特征获取跟踪ID.


但是,当您想要检测从相机捕获的内容中的面部时,您应该更喜欢AVFoundation中的面部检测API.它具有内置的面部跟踪功能,并且在GPU的背景中进行人脸检测,并且比CoreImage人脸检测要快得多它需要iOS 6,至少需要iPhone 4S或iPad 2.

面部作为元数据对象(AVMetadataFaceObject)发送到AVCaptureMetadataOutputObjectsDelegate.

您可以使用此代码(取自StacheCam 2和上面提到的WWDC会话的幻灯片)来设置面部检测并获取面部元数据对象:

- (void) setupAVFoundationFaceDetection
{       
    self.metadataOutput = [AVCaptureMetadataOutput new];
    if ( ! [self.session canAddOutput:self.metadataOutput] ) {
        return;
    }

    // Metadata processing will be fast, and mostly updating UI which should be done on the main thread
    // So just use the main dispatch queue instead of creating a separate one
    // (compare this to the expensive CoreImage face detection, done on a separate queue)
    [self.metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [self.session addOutput:self.metadataOutput];

    if ( ! [self.metadataOutput.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeFace] ) {
        // face detection isn't supported (via AV Foundation), fall back to CoreImage
        return;
    }

    // We only want faces, if we don't set this we would detect everything available
    // (some objects may be expensive to detect, so best form is to select only what you need)
    self.metadataOutput.metadataObjectTypes = @[ AVMetadataObjectTypeFace ];

}

// AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
         didOutputMetadataObjects:(NSArray *)metadataObjects
         fromConnection:(AVCaptureConnection *)c
{
   for ( AVMetadataObject *object in metadataObjects ) {
     if ( [[object type] isEqual:AVMetadataObjectTypeFace] ) {
      AVMetadataFaceObject* face = (AVMetadataFaceObject*)object;
      CMTime timestamp = [face time];
      CGRect faceRectangle = [face bounds];
      NSInteger faceID = [face faceID];
      CGFloat rollAngle = [face rollAngle];
      CGFloat yawAngle = [face yawAngle];
      NSNumber* faceID = @(face.faceID); // use this id for tracking
      // Do interesting things with this face
     }
}
Run Code Online (Sandbox Code Playgroud)

如果要在预览图层中显示面部框架,则需要获取转换后的面部对象:

AVMetadataFaceObject * adjusted = (AVMetadataFaceObject*)[self.previewLayer transformedMetadataObjectForMetadataObject:face];
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看WWDC 2012中示例代码.