I am trying to learn Apple's CoreML framework, and to do so I have created a very simple CoreML model that will tell whether an image is showing an apple or a banana. To do so, I have an image of an apple in the Assets.xcassets directory and when I press a button I would like this image to get passed into my model and I am hoping to get the correct information about it.
@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)
}
Right now I am getting an error. The error I am getting is
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
The error is next to the line
guard let fruitName = try? model.prediction(image: image as! CVPixelBuffer) else{
fatalError("Unexpected runtime error.")
}
I am not sure what is causing the error. The only reason I can think of is that I am not correctly formatting the image. The model requires a CVPixelBuffer and I am not sure if my casting from UIImage to CVPixelBuffer is correct.
What am I doing wrong?
All help is appreciated!