【问题标题】:CIDetector crashing while processing CMSampleBufferCIDetector 在处理 CMSampleBuffer 时崩溃
【发布时间】:2021-05-16 20:36:27
【问题描述】:

问题: 我正在尝试通过来自 AVCaptureVideoDataOutput 的 CMSampleBuffer 的 CIDetector 获取面部特征。程序执行时,10 次中有 9 次程序崩溃,只有一次运行正常。

预期输出: 运行时不会崩溃并在特征检测时打印“Happy”。

代码:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    
    let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
    let opaqueBuffer = Unmanaged<CVImageBuffer>.passUnretained(imageBuffer).toOpaque()
    let pixelBuffer = Unmanaged<CVPixelBuffer>.fromOpaque(opaqueBuffer).takeUnretainedValue()
    let sourceImage = CIImage(cvPixelBuffer: pixelBuffer, options: nil)
    let options = [CIDetectorSmile : true as AnyObject, CIDetectorEyeBlink: true as AnyObject, CIDetectorImageOrientation : 6 as AnyObject]
    
    // The detector is nil
    let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options) 
    let features = detector!.features(in: sourceImage, options: options)

        for feature in features as! [CIFaceFeature] {

            if (feature.hasSmile) {
                printLog(item: "HAPPY")
            }
        }
}

崩溃日志: Unexpectedly found nil while unwrapping an Optional value. detector 为零

希望得到帮助和进一步的指点。

【问题讨论】:

  • 你使用前置 faceID 摄像头吗?
  • @Aznix 是的。我正在使用前置摄像头。但它不仅仅是 faceID,它可以在所有设备上运行。

标签: ios swift image-processing avfoundation vision


【解决方案1】:

return 是可选的,sampleBuffer 被称为太重了你只能这样做

if let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options) {
    let features = detector.features(in: sourceImage, options: options) 
    for feature in features as! [CIFaceFeature] { 
        if (feature.hasSmile) {
            printLog(item: "HAPPY")
        }
    }
}

【讨论】:

  • 我明白了。这是最优化的方式还是可以优化
  • 正如你所说的那样,它有时会正确返回一些东西,这意味着它可以工作,但由于 Apple 将其设为可选,这意味着它可能会因某种原因而失败,这两个东西放在一起上面是 IMO 唯一可用的方式
猜你喜欢
  • 2010-12-29
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多