1

我正在尝试捕捉面部区域。

这是我在didOutputMetadataObjects 中所做的:获取 AVMetadataFaceObject 并在didOutputSampleBuffer中处理它

didOutputMetadataObjects正确显示标记,我考虑偏航,滚动轴

什么可能是最好的方法,我只得到面部区域,同时我看到一个面部标记?

-(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];


}
}
4

1 回答 1

0

你好一些建议如下:

1:添加一个 stillImageOutPut

lazy var stillImageOutPut: AVCaptureStillImageOutput = {
    let imageOutPut = AVCaptureStillImageOutput.init()
    return imageOutPut
}()

2添加到会话

if session.canAddOutput(stillImageOutPut){
            session.addOutput(stillImageOutPut)
}

3然后实现这个委托函数

// MARK: AVCaptureMetadataOutputObjectsDelegate

extension ZHFaceDetectorViewController: AVCaptureMetadataOutputObjectsDelegate {
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    printLog(Thread.current)
    let metadataObject = metadataObjects.first
    if let object = metadataObject {
        while !hasDetectorFace {
            if object.type == AVMetadataObject.ObjectType.face{
                hasDetectorFace = true
                DispatchQueue.global().async {
                    if let stillImageConnection = self.stillImageOutPut.connection(with: AVMediaType.video){
                        printLog(stillImageConnection)
                        printLog(connection)
                        stillImageConnection.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue)!
                        /// prepare settings 如果不设置 截取照片时屏幕会闪白
                        let settings = AVCaptureAutoExposureBracketedStillImageSettings.autoExposureSettings(exposureTargetBias: AVCaptureDevice.currentExposureTargetBias)
                        /// begin capture
                        self.stillImageOutPut.prepareToCaptureStillImageBracket(from: stillImageConnection, withSettingsArray: [settings], completionHandler: { (complete, error) in
                            if error == nil {
                                self.stillImageOutPut.captureStillImageAsynchronously(from: stillImageConnection, completionHandler: { (imageDataSampleBuffer, error) in
                                    printLog(imageDataSampleBuffer)
                                    printLog(error)
                                    if error == nil {
                                        if let sampleBuffer = imageDataSampleBuffer {
                                            if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer){
                                                if let image = UIImage(data: imageData) {
                                                    /// operater your image
                                                    printLog(image)
                                                }
                                            }
                                        }
                                    }else{
                                        printLog("something was wrong")
                                    }
                                })
                            }
                        })
                        
                    }
                }
            }
        }
    }
}

}

4 然后我得到我的图片 日志信息

于 2017-11-28T04:08:44.650 回答