使用didOutputSampleBuffer捕获图像AVMetadataFaceObject:

时间:2016-10-18 01:30:11

标签: uiimage avfoundation cvpixelbuffer

我想抓住脸部区域。

以下是我的工作,在 didOutputMetadataObjects:中获取AVMetadataFaceObject并在 didOutputSampleBuffer

中处理它

didOutputMetadataObjects 正确显示标记,我认为是Yaw,roll axis

什么是最好的方式,我只获得面部区域,同时看到面部标记?

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{

   for(AVMetadataObject *metaObject in metadataObjects){
        if([metaObject isKindOfClass:[AVMetadataFaceObject class ]] && metaObject.type == AVMetadataObjectTypeFace){
           AVMetadataFaceObject * adjustedMeta = (AVMetadataFaceObject*)[self.videoLayer transformedMetadataObjectForMetadataObject:metaObject];
           self.metaFaceObject= adjustedMeta;
           //Draw the face marker here
            }
    }
}

AVCaptureVideoDataOutputSampleBufferDelegate

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        if(pixelBuffer ){
            CFDictionaryRef attachments = CMCopyDictionaryOfAttachments( kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate );
           CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary<NSString *,id> * _Nullable)(attachments)];
            ciImage = [ciImage imageByCroppingToRect:self.metaFaceObject.bounds];
            //This Image is upside down. Second thing the it does not have the face.
            UIImage *image=[UIImage imageWithCIImage:ciImage];


}
}

0 个答案:

没有答案