Creating an iOS mobile application using the Core ML model
In this section, we will be creating an iOS project to use Core ML, for which you will require Xcode (it must be version 9+).
Let's get started by opening Xcode and creating an empty swift application with a storyboard. In the main storyboard design, the screen will appear as follows. Then, add the generated model file to your project. This should give you the following structure:
Now, create the UI in your main storyboard file, as shown here:
Create outlets for each text field. And add event listener to each and every text field. Now, your view controller will look like this:
import UIKit
import Core ML
class ViewController: UIViewController {
let model = cancermodel()
@IBOutlet weak var meanradius: UITextField!
@IBOutlet weak var cancertype: UILabel!
@IBOutlet weak var meanperimeter: UITextField!
@IBOutlet weak var meanarea: UITextField!
@IBOutlet weak var meanconcavity: UITextField!
@IBOutlet weak var meanconcavepoints: UITextField!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad();
updated(meanconcavepoints);
//This line is to fire the initial update of the cancer type.
}
/*
This method will send the input data to your generated model class and display the returned result to the label.
*/
@IBAction func updated(_ sender: Any) {
guard let modeloutput = try? model.prediction(mean_radius:
Double(meanradius.text!)!, mean_perimeter:
Double(meanperimeter.text!)!, mean_area: Double(meanarea.text!)!,
mean_concavity: Double(meanconcavity.text!)!, mean_concave_points:
Double(meanconcavepoints.text!)!) else {
fatalError("unexpected runtime error")
}
cancertype.text = modeloutput.typeofcancer;
}
}
You can find the same code in the GitHub repository for this book.
Once you set up the project in Xcode, you can run it in the simulator. The result will look like this: