Google 转换为图像并尝试检测人脸时人脸检测崩溃

Google Face Detection crashing when converting to image and trying to detect face

我正在创建一个带有滤镜的自定义相机。当我添加以下行时,它崩溃了,没有显示任何异常。

//设置视频输出

func setupBuffer() {
    videoBuffer = AVCaptureVideoDataOutput()
    videoBuffer?.alwaysDiscardsLateVideoFrames = true
    videoBuffer?.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value: kCVPixelFormatType_32RGBA)]
    videoBuffer?.setSampleBufferDelegate(self, queue: DispatchQueue.main)
    captureSession?.addOutput(videoBuffer)
}


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

    if connection.videoOrientation != .portrait {
        connection.videoOrientation = .portrait
    }
        guard let image = GMVUtility.sampleBufferTo32RGBA(sampleBuffer) else {
            print("No Image ")
            return
        }

    pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    ciImage = CIImage(cvImageBuffer: pixelBuffer!, options: CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate)as! [String : Any]?)

    CameraView.filter = CIFilter(name: "CIPhotoEffectProcess")
    CameraView.filter?.setValue(ciImage, forKey: kCIInputImageKey)
    let cgimg = CameraView.context.createCGImage(CameraView.filter!.outputImage!, from: ciImage.extent)

    DispatchQueue.main.async  {
        self.preview.image = UIImage(cgImage: cgimg!)
    }
}

但它正在崩溃 -

  guard let image = GMVUtility.sampleBufferTo32RGBA(sampleBuffer) else {
                print("No Image ")
                return
            }

当我传递从 CIImage 创建的图像时,它无法识别图像中的人脸。 完整的代码文件是https://www.dropbox.com/s/y1ewd1sh18h3ezj/CameraView.swift.zip?dl=0

1) 为缓冲区创建单独的队列。

 fileprivate var videoDataOutputQueue = DispatchQueue(label: "VideoDataOutputQueue")

2) 使用此设置缓冲区

        let videoBuffer = AVCaptureVideoDataOutput()
        videoBuffer?.alwaysDiscardsLateVideoFrames = true
        videoBuffer?.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString): NSNumber(value: kCVPixelFormatType_32BGRA)]
        videoBuffer?.setSampleBufferDelegate(self, queue: videoDataOutputQueue ) //
        captureSession?.addOutput(videoBuffer)