Search code examples
swiftxcodeswift-playgroundcoreml

How To Get CoreML In Pure Playground Files


I am using a .playground file and I can't seem to add my CoreML model to it. I drag it into the Resources folder and this is my code:

func predict(image: CGImage) {
    let model = try! VNCoreMLModel(for: Inceptionv3().model)
    let request = VNCoreMLRequest(model: model, completionHandler: results)
    let handler = VNSequenceRequestHandler()
    try! handler.perform([request], on: image)
}

However, I get the error saying:

Use of Undeclared Type Inceptionv3

Can someone please help me out?


Solution

  • The compiler raises this error, because it cannot find a declaration of the class Inceptionv3, that you try to instantiate an object of.

    This class is automatically created for you as long as you have a regular Xcode project. If you want to work with it inside a Swift playground, you will need to add this file manually:

    First, create a regular Xcode project (an iOS app for example) and integrate your mlmodel there. Xcode will then generate an interface for that model automatically. These are exactly the files that are missing in your project and contain the definition of the Inceptionv3 class.

    The same problem has been asked and answered here. There you can also find an image showing how to find the automatically generated classes.