Writing the iOS mobile application

In this section, we are going to create an app to make use of the image recognition model that we've created to predict images using your iOS mobile camera.

To start, you need a Mac PC running Xcode version 9+. Download the source code (x-code project) from the Git repository and navigate to the project folder. Open the recognition.xcodeproj image in Xcode. The following screenshot shows the folder structure of the project:

The main file we are going to view is controller.swift. It contains the following code: 

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var pictureImageView :UIImageView!
@IBOutlet weak var titleLabel :UILabel!

These are the outlets for the image-view control and title-label control in the main storyboard:

private var model : converted = converted()

This is the instance of the model that was generated when we added the core-ml file we created in the previous section:

    var content : [ String : String ] = [
"cheeseburger" : "A cheeseburger is a hamburger topped with cheese. Traditionally, the slice of cheese is placed on top of the meat patty, but the burger can include many variations in structure, ingredients, and composition. It has 303 calories per 100 grams.",
"carbonara" : "Carbonara is an Italian pasta dish from Rome made with egg, hard cheese, guanciale, and pepper. The recipe is not fixed by a specific type of hard cheese or pasta. The cheese is usually Pecorino Romano.",
"meat loaf" : "Meatloaf is a dish of ground meat mixed with other ingredients and formed into a loaf shape, then baked or smoked. The shape is created by either cooking it in a loaf pan, or forming it by hand on a flat pan. It has 149 calories / 100 grams",
"pizza" : "Pizza is a traditional Italian dish consisting of a yeasted flatbread typically topped with tomato sauce and cheese and baked in an oven. It can also be topped with additional vegetables, meats, and condiments, and can be made without cheese. It has 285 calories / 100 grams"
]

We hardcoded the contents to display in the title label for the corresponding class label we trained:

    let images = ["burger.jpg","pizza.png", "pasta.jpg","meatloaf.png"]

These are the images we have added to the project; they'll serve as input for our prediction app:

    var index = 0
override func viewDidLoad() {
super.viewDidLoad()
nextImage()
}
@IBAction func nextButtonPressed() {
nextImage()
}
func nextImage() {
defer { index = index < images.count - 1 ? index + 1 : 0 }
let filename = images[index]
guard let img = UIImage(named: filename) else {
self.titleLabel.text = "Failed to load image (filename)"
return
}
self.pictureImageView.image = img
let resizedImage = img.resizeTo(size: CGSize(width: 224, height: 224))
guard let buffer = resizedImage.toBuffer() else {
self.titleLabel.text = "Failed to make buffer from image (filename)"
return
}

As we trained our model with 224 px images, we are also resizing the images of the input and converting it into an image buffer, which we want to give to the prediction method:

        do {
let prediction = try self.model.prediction(input: MymodelInput(input__0: buffer))

Here, we are inputting the image and getting the prediction results:

            if content.keys.contains(prediction.classLabel) {
self.titleLabel.text = content[prediction.classLabel]
}
else
{
self.titleLabel.text = prediction.classLabel;
}

In the preceding code, depending on the class label, we are displaying the content to the user:

        } catch let error {
self.titleLabel.text = error.localizedDescription
}
}
}

This completes the application's creation. Now, we will execute the application to find the following images as output:

 

Click on Next to find the our next image:

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset