首页 > 解决方案 > 慢速人脸检测 Firebase MLKit

问题描述

开始使用带有前置摄像头的 MLKit 人脸检测器,但处理人脸的速度真的很慢

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

    print("Picture at ", Date())


    let visionImage = VisionImage(buffer: sampleBuffer)
    visionImage.metadata = metadata


    faceDetector?.detect(in: visionImage) { (faces, error) in
        guard error == nil, let faces = faces, !faces.isEmpty else {
            // Error. You should also check the console for error messages.
            let errorString = error?.localizedDescription
            print("Face detection failed with error: \(errorString)")
            return
        }

  }

我哪里错了?

标签: swiftcamerafirebase-mlkit

解决方案


您可以尝试一些方法来加快检测速度:

  1. 以发布模式(优化)构建您的应用程序,而不是调试模式。

  2. 创建 faceDetector 时,请确保使用 VisionFaceDetectorOptions 并将其 isTrackingEnabled 设置为 true。

  3. 设置 AVCaptureVideoDataOutput 时,请将以下键值对添加到其 videoSettings 中:

键:kCVPixelBufferPixelFormatTypeKey

值:kCVPixelFormatType_32BGRA


推荐阅读