CIDetector在处理CMSampleBuffer时崩溃 [英] CIDetector crashing while processing CMSampleBuffer

查看:107
本文介绍了CIDetector在处理CMSampleBuffer时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我正在尝试从AVCaptureVideoDataOutput的CMSampleBuffer通过CIDetector获取面部特征.在执行程序时,十分之九的程序崩溃,并且只有在其正常运行后才会崩溃.

Problem: I am trying to get facial features through CIDetector from CMSampleBuffer from AVCaptureVideoDataOutput. On execution of the program, 9 out of 10 time the program crashes and only once it runs fine.

预期输出:运行时不会崩溃,并显示"Happy"(快乐)进行特征检测.

Expected Output: Run without crash and print "Happy" on feature detection.

代码:

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")
            }
        }
}

崩溃日志:在展开可选值时意外发现nil. detector 为nil

希望帮助和进一步的指点.

Would love help and further pointers.

推荐答案

返回值是可选的,并且 sampleBuffer 被称为过于繁琐,您只能这样做

The return is optional and sampleBuffer is called way too heavy you can only do this

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")
        }
    }
}

这篇关于CIDetector在处理CMSampleBuffer时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆