0

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!

J. Doe
  • 1
  • 1
  • If `image` is a `UIImage` then no, you can't blindly cast it to any other class. – rmaddy Jan 19 '19 at 04:12
  • if that's the case how would I go about turning the UIImage into a CVPixelBuffer? – J. Doe Jan 19 '19 at 04:19
  • 1
    See [Convert Image to CVPixelBuffer for Machine Learning Swift](https://stackoverflow.com/questions/44400741/convert-image-to-cvpixelbuffer-for-machine-learning-swift) – rmaddy Jan 19 '19 at 04:21

0 Answers0