How to add a machine learning model to your iOS project using CoreML
CoreML is the framework that lets you build and compile a Core ML model to add machine learning features to your app. It’s available on all Apple platforms. In this tutorial, you will see how you can use it in an iOS app.
Step 1. Create Xcode project
Open Xcode-> Select ‘Create New Xcode Project’-> Select ‘Single View App’-> Enter ‘Product Name’-> Select the location where it should be saved-> Create. Now you have a newly created Xcode project.
Step 2. Get the model
For this tutorial, I am going to use the SqueezeNet model from Apple’s website.
Step 3. Add the model to your project
All you have to do after you have your model is to drag and drop it into your project. If you select the model you will see some details about it and after a few seconds, a Swift model class will be generated.
Step 4. Create screens
You can create your own screens the way you like it or need it in your app or you can download my demo project from here. For the next steps, we will need an image provided by the user.
Step 5. Instantiate the model
As I’ve said earlier, after you drop the model into your project, a swift class will be generated for it. Now we will use that class. In the view controller where you want to use the model, you will need to instantiate it.
Step 6. Use Vision to create the prediction request
Because we are dealing with images, we are going to use Vision, but if you want to process text or speech input you can use Natural language framework.
First, you will have to import Vision. After that, in the method where you have the photo provided by the user, you need to add the following code:
That’s it! :) Now you can run the project and see how it works.
Thanks for reading! Now you can implement your own machine learning feature and create a really cool app. If you have any questions leave me a comment below 🙏 .
Zipper Studios is a group of passionate engineers helping startups and well-established companies build their mobile products. Our clients are leaders in the fields of health and fitness, AI, and Machine Learning. We love to talk to likeminded people who want to innovate in the world of mobile so drop us a line here.