【问题标题】:Real time face tracking with camera in swift 4在swift 4中使用相机进行实时面部跟踪
【发布时间】:2018-06-15 10:08:33
【问题描述】:

我希望能够从摄像头源中跟踪用户的面部。我看过this SO 帖子。我使用了答案中给出的代码,但它似乎没有做任何事情。听说过

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)

在 swift 4 中已更改为其他内容。这可能是代码的问题吗?

在面部跟踪时,我还想使用 CIFaceFeature 监控面部标志。我该怎么做?

【问题讨论】:

标签: ios swift avfoundation apple-vision


【解决方案1】:

我在这里找到了一个起点:https://github.com/jeffreybergier/Blog-Getting-Started-with-Vision

基本上你可以像这样声明一个惰性变量的视频捕获会话:

private lazy var captureSession: AVCaptureSession = {
    let session = AVCaptureSession()
    session.sessionPreset = AVCaptureSession.Preset.photo
    guard
        let frontCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front),
        let input = try? AVCaptureDeviceInput(device: frontCamera)
        else { return session }
    session.addInput(input)
    return session
}()

然后在viewDidLoad 内开始会话

self.captureSession.startRunning()

最后你可以在里面执行你的请求

func captureOutput(_ output: AVCaptureOutput, 
    didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
}

例如:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: 
    CMSampleBuffer, from connection: AVCaptureConnection) {
    guard
        // make sure the pixel buffer can be converted
        let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
        else { return }

    let faceRequest = VNDetectFaceRectanglesRequest(completionHandler: self.faceDetectedRequestUpdate)

    // perform the request
    do {
        try self.visionSequenceHandler.perform([faceRequest], on: pixelBuffer)
    } catch {
        print("Throws: \(error)")
    }
}

然后你定义你的faceDetectedRequestUpdate 函数。

无论如何,我不得不说我无法从这里弄清楚如何创建一个工作示例。我发现的最佳工作示例在 Apple 的文档中:https://developer.apple.com/documentation/vision/tracking_the_user_s_face_in_real_time

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2018-04-26
    • 2020-02-17
    相关资源
    最近更新 更多