Search code examples
swiftxcodecoremlapple-visioncreateml

Enum for .mlmodels?


I am currently working on a project that includes image recognition with CreateML, CoreML, and Vision. I am still trying datasets and improving the models with CreateML, but if I change from a model to another, I have to manually change a variable let model = example() that Xcode creates for me when I import the .mlmodel file into the project. So I wanted to do a tableView with the name of those models files so if I tap on one of them, it takes me to the "RecognitionVC", passing the name as a variable so I can instantiate the class with an enum from a rawValue(string) and then access its "model variable" which I cannot achieve.

This is what the enum would look like:

enum MLModels: String {
    case example
    case letters
    case ab
    case numbers
    case asl
}

And this is what I would like to achieve:

   func getModel() -> AnyClass {
        switch self {
        case .example:
            return example()
...
        }
    }

    var model: MLModel {
        switch self {
        case .example:
            return example()
...
        }
    }

I am new to these machine learning built in frameworks, if someone could let me know what am I doing wrong or how can I achieve this I would be very grateful.I am including some screenshots. And in case you want to test anything I am including the link to apple's public models:

link

folder example1 example2


Solution

  • When you write example() it creates an instance of a wrapper class. This is not an MLModel. However, it does have the MLModel as a property, so you could write return example().model to do what you want.