【发布时间】: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