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