Couscous or Not Couscous — Let CoreML Decide
Training a model in an iOS application
Yes, that’s right. I built an iOS app that looks at a picture and decides whether it’s couscous or not couscous.
Let’s Start With the App Layout
We need to create an iOS project with a single view app:
Now you have your project. I don’t like using Storyboard, so the app is made programmatically, which means no buttons to toggle or switches — just pure code.
You have to delete the main.storyboard and set your AppDelegate.swift file like this:
Make sure that you have removed the storyboard “Main” from the deployment info.
Main ViewController
Now let’s set our ViewController with the buttons and logo. I used some custom buttons. First, you need to inherit from UIButton and create your own custom button:
BtnPleinLarge
is our new button, we use it to create our main two buttons for ViewController.swift, our main view.
Set the layout and buttons with some logic as well:
Buttons: Upload a Picture and Take a Picture
We now need to set some logic. It’s important to change the Info.plist file and add a property so that an explanation of why we need access to the camera and the library is given to the user. Add some text to the “Privacy — Camera Usage Description”:
Of course, you need to set up the layout and add the subviews to the view too. I’ve added a logo on top of the view as well:
Secondary ViewController: Where We Show Our Results
We need three things:
- Our original image
2. A button to dismiss the view:
3. And a picture that either says “Couscous” or “Not Couscous”:
Don’t forget to add the subviews to the main view and set up the layout too.
Train the Model
There are two ways to train the model. The first is through Xcode and the second is through a custom Python script (use the turicreate package). The most important thing is to get a .mlmodel file that can be parsed by Xcode. We can take advantage of the way Xcode uses the .mlmodel file to obtain confidence and use it to update our secondary ViewController with the appropriate “Couscous” or “Not Couscous” image.
Since our model is binary, we need to train the model based on pictures that are clearly couscous (in this case I used pictures with Moroccan couscous) and pictures that are not couscous as well.
I used 51 pictures for “Couscous” and 68 pictures for “Not Couscous”. You can find the images I used in the repository.
Let’s Start
Open playground and write this code:
When you run it, you should see something similar to this picture:
Drag to the folder that contains the folders “couscous” and “NotCouscous”. Now let the builder classify the images.
You’ll have to save the .mlmodel file and drop it into the Xcode project, Xcode will do the rest (parsing the file, etc.)
Some Classifier Logic
Now we need to add the delegate for the ImagePickerController in our main ViewController:
Creating an app and training a model is that simple!
Final Result
This project is available to download from my Github account.