慢人脸检测 Firebase MLKit

Slow face detection 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
        }

  }

我哪里错了?

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

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

  2. 创建人脸检测器时,请确保使用 VisionFaceDetectorOptions 并将其 isTrackingEnabled 设置为 true。

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

键:kCVPixelBufferPixelFormatTypeKey

值:kCVPixelFormatType_32BGRA

在我的案例中,我没有遵循正确的指南,这些指南是: 如果您想在 real-time 应用程序中使用人脸检测,请遵循以下准则以获得最佳帧率:

将面部检测器配置为使用面部轮廓检测或分类和地标检测,但不能同时使用:

你应该这样做以获得更好的帧率

  1. 轮廓检测
  2. 地标检测
  3. 分类
  4. 地标检测和分类

你不应该使用这些

轮廓检测和地标检测

轮廓检测与分类

轮廓检测、地标检测和分类

在此处查找所有指南: https://developers.google.com/ml-kit/vision/face-detection/android