1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Leveraging Machine Learning for Your iOS Apps with MLKit

Creating Functions



The course is part of this learning path

Start course

This course focuses on machine learning. We're going to discuss what machine learning is and how we can leverage it to create intelligent and engaging apps. You'll also follow along as we create an image recognition app.


Hi, within this lecture, we're going to create the functions that we are going to use when we try to recognize the image. So, in order to do that, I'm going to download the model that we're going to use. So, we have different options in here. As you can see, this is 32 bit, this is 16 bit and these all belongs to MobileNetV2 model. So, as we increase the bits, the precision increases as well. So, if we want a full precision, If we want clearer results, we have to go with the 32 bit which is 24MB. So, if we want something smaller, of course, we can try to go with other models as well, but I'm going to go for the big one because I want full precision. Okay, so I want better assumptions on the images that I'm going to try and recognize. And if you click on this view code, it will display a documentation in which you will find the details of how to use this image classification models. And don't worry, I'm going to explain all of those and in fact we're not even going to follow this documentation. I'm going to simplify it. I'm going to rewrite all the codes from scratch in our app, but know that this documentation exists here so that you can try to take a look when you try to work with another model or another class model, if you're working with machine learning examples, okay? But if we're going to complete two steps first, we're going to complete the request and we're going to create an handler. We're going to see what are those later on. Don't worry, but we're going to see a lot of new codes and a lot of new techniques in this section. So, in order to memorize what's going on, just know that we have two main steps here: creating the request and creating a handler to handle this request to fire up this request, okay? And we're going to work with a class called VNCoreMLModel, okay? And again, if you want you can just try to read the details of this documentation, but I'm going to explain all of those in our code as well. So, let me open my Downloads folder and bring in this model into my project and make sure this copied items, if needed, and the targets are selected. Once you confirm, you can just hit on 'Finish', and it will bring in the model so that we can use it in our code, okay? So, first I have to import the Core ML framework that we have talked about. And also furthermore, we need something more and it's called vision. So, this is a model. This is kind of a module that we use when we try to work with image recognition models, okay? So, that's why I'm importing this I'm going to need these classes in my app. So, after you import the Core ML and the vision in your app, you're good to go actually. And we're going to have to adjust this UIImagePickerController and we're going to have to create our recognize function as well. I'm going to call my recognize function recognizeImage. So, this is going to be basic. And I'm going to call this function under didFinishPickingMediaWithinfo, okay? And once user finishes picking media, it will get called, it will get executed. And in order to do that, these methods, this function actually will ask me some image type called CIimage, okay? So, I'm going to have to adjust my code according to that. So, if you come over your documentation, you can't see the CIImage because it's better to do this now because it will ask us for later on and I don't want to waste any more time. I just want to get in to my recognizeImage functions so that I can tell you all about this Core ML functions, Core ML classes and objects. And in order to complete this, I'm going to convert my UIImage to be a CIImage. And as you can see this is a representation of an image to be processed by core image filters, so that's why we are bringing this into the game. And in order to do that, I'm going to create another variable called if CIImage. And I'm going to use if let because my UIImage is casted optionally in here. And if you say CIImage, it will ask you for some data. And the best way to do that is through a UIImage because we already have a UIImage at this point, right? We can try to convert this UIImage into a CIImage. So, I can get this easily from imageView.image, okay? And in here under if let, you can try to come up with a variable so that we can use it under our function. So, let me save our chosen image and this is going to be a CIImage, okay? And in order to comply with the scope, I'm going to say chosen image is a CIImage so that I can come over here and use my chosen image later on. So, make sure you select the right one. And under the recognizeImage, now we can actually use this chosen image. And In fact, I don't even have to do that; I can just take this as an input in my function, right? I can come over here and say this function will ask for an image and this is going to be a CIImage. And in this recognizeImage, I can just give this as a parameter, right? I can come over here and say, this is chosen image. So far so good, now I have my function ready in order to leverage all these Core ML objects and classes. So, again we're going to create the request. We're going to create an handler to handle this request and try to get the assumption; try to get the classification from our image model. So, let me try to find the CIImage. As you can see in the handler, it asks us for a CIImage. And again, this is the reason why we have created our CIImage again. So, here we go. I believe we can stop here because in the next lecture, we're going to create our request.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics