Apple 最近向 CIDetector
类添加了一个名为 CIDetectorTracking
的新常量,它似乎能够跟踪视频帧之间的面部。如果我能弄清楚它是如何工作的,这对我来说将是非常有益的。
我已经尝试使用我能想到的远程相关的每个对象将此键添加到检测器选项字典中,包括我的 AVCaptureStillImageOutput 实例、我正在处理的 UIImage、YES、1 等。
NSDictionary *detectorOptions = [[NSDictionary alloc] initWithObjectsAndKeys:CIDetectorAccuracyHigh, CIDetectorAccuracy,myAVCaptureStillImageOutput,CIDetectorTracking, nil];
但无论我尝试传递什么参数,它要么崩溃(显然我在这里猜测)要么调试器输出:
Unknown CIDetectorTracking specified. Ignoring.
通常情况下,我不会对此进行猜测,但关于该主题的资源几乎不存在。 Apple's class reference状态:
A key used to enable or disable face tracking for the detector. Use this option when you want to track faces across frames in a video.
除了可用性是 iOS 6+ 和 OS X 10.8+ 之外就是这样。
CIDetector.h
中的注释:
/*The key in the options dictionary used to specify that feature tracking should be used. */
如果这还不够糟糕,Google搜索提供 7 个结果(当他们找到这篇文章时为 8 个)所有这些都是 Apple 类引用、API 差异、询问如何在 iOS 5 中实现此目的的 SO 帖子,或前者的第 3 方副本。
综上所述,我们将不胜感激有关正确使用 CIDetectorTracking
的任何提示或技巧!
最佳答案
你是对的,这个 key 没有很好的文档记录。除了 API 文档之外,它也不在以下内容中进行了解释:
- CIDetector.h 头文件
- 核心图像编程指南
- WWDC 2012 session "520 - What's New in Camera Capture"
- 此 session 的示例代码 (StacheCam 2)
我为 CIDetectorTracking
尝试了不同的值,唯一接受的值似乎是 @(YES)
和 @(NO)
。使用其他值,它会在控制台中打印此消息:
Unknown CIDetectorTracking specified. Ignoring.
当您将该值设置为 @(YES)
时,您应该获得具有检测到的面部特征的跟踪 ID。
但是,当您想要检测从相机捕获的内容中的人脸时,您应该更喜欢 AVFoundation 中的人脸检测 API。它内置面部跟踪,面部检测在 GPU 的后台进行,比 CoreImage 面部检测快得多 它需要 iOS 6 和至少 iPhone 4S 或 iPad 2。
面部作为元数据对象 (AVMetadataFaceObject
) 发送到 AVCaptureMetadataOutputObjectsDelegate
。
您可以使用此代码(取自 StacheCam 2 和上述 WWDC session 的幻灯片)设置人脸检测并获取人脸元数据对象:
- (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
}
}
如果要在预览图层中显示人脸框,需要获取转换后的人脸对象:
AVMetadataFaceObject * adjusted = (AVMetadataFaceObject*)[self.previewLayer transformedMetadataObjectForMetadataObject:face];
有关详细信息,请查看 sample code from WWDC 2012 .
关于objective-c - 正确使用 CIDetectorTracking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475387/