首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理CMSampleBuffer时CIDetector崩溃

处理CMSampleBuffer时CIDetector崩溃
EN

Stack Overflow用户
提问于 2021-02-13 15:51:41
回答 1查看 45关注 0票数 0

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

预期输出:运行时不会崩溃,并在检测到功能时打印"Happy“。

代码:

代码语言:javascript
复制
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. The detector is nil

希望得到帮助和更多的指点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-13 16:01:18

返回值是可选的,而且调用sampleBuffer的方法太重了,您只能这样做

代码语言:javascript
复制
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")
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66182937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档