我正在尝试学习 Apple 的 CoreML 框架,为此我创建了一个非常简单的 CoreML 模型,该模型将判断图像显示的是苹果还是香蕉。为此,我在 Assets.xcassets 目录中有一个苹果的图像,当我按下一个按钮时,我希望这个图像传递到我的模型中,我希望得到关于它的正确信息。
@IBAction func applePressed(_ sender: Any){
let image = cropImage(imageToCrop: UIImage(named: "apple")!)
guard let fruitName = try? model.prediction(image: image as! CVPixelBuffer) else{
fatalError("Unexpected runtime error.")
}
print(fruitName.classLabel)
}
现在我收到一个错误。我得到的错误是
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
错误在该行旁边
guard let fruitName = try? model.prediction(image: image as! CVPixelBuffer) else{
fatalError("Unexpected runtime error.")
}
我不确定是什么导致了错误。我能想到的唯一原因是我没有正确格式化图像。该模型需要一个 CVPixelBuffer,我不确定我从 UIImage 到 CVPixelBuffer 的转换是否正确。
我究竟做错了什么?
感谢所有帮助!