问题
I'm working on some face detection code on OSX Mavericks and I'm trying to take advantage of the newish (as of 10.8) face tracking across multiple stills functionality that CIDetector offers.
I have basic face detection working fine, like so:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *image = [CIImage imageWithCVImageBuffer:imageBuffer];
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh,
CIDetectorTracking : @YES
}];
NSArray *features = [faceDetector featuresInImage:image];
for ( CIFaceFeature *feature in features ) {
if (feature.hasTrackingID) {
NSLog(@"tracking id: %@", @(feature.trackingID));
}
}
}
The features list does get populated correctly but that trackingID never seems to be present. Has anyone gotten this working on Mavericks? It fails the same way on Mountain Lion.
I have seen a similar question here (CIFaceFeature trackingID is always coming same for multiple faces) but I didn't learn anything new there.
For what it's worth it does seem to function correctly on iOS.
回答1:
I looked at this code again and the answer turned out to be pretty obvious: I was constantly re-initializing the CIDetector, which was bad for performance and also had the consequence of resetting its internal tracking data each frame. So the first time a face was detected was always the first time a face was detected for that particular CIDetector instance.
Also, CIDetector warns about this in the docs:
"This class can maintain many state variables that can impact performance. So for best performance, reuse CIDetector instances instead of creating new ones.", from https://developer.apple.com/library/mac/documentation/CoreImage/Reference/CIDetector_Ref/Reference/Reference.html.
来源:https://stackoverflow.com/questions/19983741/cidetector-trackingid-never-present