CIDetector trackingID never present

夙愿已清 提交于 2019-12-12 15:08:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!