简体   繁体   中英

AVFoundation Face Detection Frame

I am working on a project with AVFoundation where I can detect faces and add something to the picture (before taking the picture). I have implemented the preview layer and image capture.

My question, how do I introduce the face detection and get the frame/location of the face object? Is it possible to then add something on top of the preview layer so that it is also captured in the picture (think of the new snapchat filters)?

TIA

You can use AvCaptureMetadataOutput to get metadata found in frame :

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
    for metadataObject in metadataObjects as [AVMetadataObject] {
      if metadataObject.type == AVMetadataObjectTypeFace {
        var transformedMetadataObject = previewLayer.transformedMetadataObjectForMetadataObject(metadataObject)
      }
    }
}

Then you can get face rectangle from transformedMetadataObject.bounds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM