All right. 1
So in the last lesson, you saw how we were able to use the image that the user picked in the 2
imagePickerController and convert that image into a ciimage, and then pass that ciimage into our detect image 3
method. 4
Now, once the image goes into this detect image method, then we do a couple of other things here. 5
Firstly, we load up our model using the imported Inceptionv3 model, and then we create a request that 6
asked the model to classify whatever data that we pass in. And the data that we pass in is defined over 7
here using a handler. 8
And then we use that image handler to perform the request of classifying the image. 9
And once that process completes, then this callback gets triggered and we get back a request or an error 10
and we print out the results that we got from that classification. 11
And as you can see the results were pretty accurate. But now we have to dig into those results because 12
we want to see if the top result, the result with the highest confidence, was classified as a hotdog 13
or as not a hotdog, 14
making it more like the Seafood app from Silicon Valley. 15
So instead of printing out all of the results into the console, what we want is to change the title bar 16
text up here to say, "Hotdog" 17
if the image contains a hotdog, or "Not hotdog" if the image is classified to not contain a hotdog. 18
So how can we do that? 19
Well, the first thing we can do is delete this print statement. And we're going to check in the results 20
that we get back for the first item. 21
So that's usually the one with the highest confidence interval. In this case, it was 82 percent confident 22
that the image contained a computer keyboard or keypad. 23
So in order to tap into that first result, we can write if let's firstResult = results.first. 24
That was easy, right? 25
So, now inside here, we can tap into first results and we can check if firstResult.identifier 26
contains a string called "hotdog," 27
then the classification we got back was certain that it contained a hotdog, 28
right? 29
Then the classification we got back is pretty confident that there's a hotdog in the image. 30
In that case, we're going to change navigation bar's title to say hotdog. 31
So we'll write 32
self.navigationItem.title = "Hotdog!" Now, if the first result doesn't contain the word 33
"hotdog," then we're going to set the navigationItem's title to "Not Hotdog!" instead. 34
All right. 35
So that's pretty simple, right? 36
So we're, basically, using optional chaining to check to make sure that the results that we get back definitely 37
have a firstResult value, and then we're using that value to check that its identifier contains the 38
word "hotdog." 39
So what you see in the console at the moment is an array of all of the VNClassificationObservations. 40
And if you have a look at the first VNClassificationObservation up here, you can see that it contains 41
a number of properties. And one of those properties is this number here. And that's the percentage confidence 42
the model has in its own prediction. 43
So this prediction for that image we saw earlier on, the model has is 82 percent confident that it is 44
a computer keyboard. 45
Now, this string which says "Computer keyboard, keypad" is the identifier property of the firstResult. 46
And in this line of code, we're checking to make sure that in that string, there contains the word "hotdog" 47
in which case, we're looking for the most confident prediction from the model and checking to see 48
if it includes a hotdog prediction. And if it does, then we're pretty certain that the image contains 49
this hotdog and we're going to show the user the prediction. 50
If not, then we'll show them the "Not Hotdog!" variant. So let's give it a spin, shall we? 51
All right. 52
So I've got the app loaded up on my phone and I'm going to first take a picture of the keyboard and 53
hit Use Photo. 54
Let's see what we get back. 55
Not Hotdog! 56
All right. 57
Let's try it with the real deal now. Let's try it with a hotdog, Use Photo. 58
Hotdog! 59
Brilliant. 60
So our app is working. 61
I don't know how many hotdogs they've trained this Inceptionv3 model on, but from my testing, it's been 62
able to pick up most of the hotdog images I can find on Google. 63
Now, it can be a bit more variable when you try it on a live food item. 64
I've tried a few times. But depending on the orientation, sometimes it identifies it as "Not a Hotdog." 65
So I'm sure there's still more work for Google to be done on inception, but it's already performing pretty 66
well. 67
So there it is. 68
There is our remarkably simple app where we managed to tap into the camera, take photos, and classify those 69
images all on the device. So I can show you if I switch into airplane mode, you can see that the classification 70
still works even though I have no internet, whatsoever. 71
So there you go. 72
That's how incredibly simple it is to implement image recognition using Vision and CoreML. 73
So I hope you've learned something quite useful and you can start implementing these image recognition 74
machine learning models in your own iOS apps 75
from now on. 76
So my recommendation is to use Inceptionv3 because I find from testing that it's most accurate. 77
But, of course, feel free to play around with the other models that Apple has provided and you'll see 78
that some of them have better applications in certain situations and scenarios. 79
So I'm looking forward to hearing about all of your awesome apps that you've created using CoreML. And 80
if you made anything really cool, then please post it in the discussion and we would love to congratulate 81
you and have a play with it. 82
Now, in the next module, we have a bonus tutorial for you. 83
Now, it's an optional tutorial so you don't have to do it. In the next tutorial, 84
we show you how you can recreate the Hotdog app using IBM Bluemix API. 85
So we're basically using a different technology stack in order to achieve the same purpose, i.e., Visual 86
Recognition. 87
So as I mentioned, this is completely optional. 88
You're going to be building the same Hotdog or Not Hotdog app, although with some neat add-on features. 89
But if you are not interested in recreating the same app again, then you can go ahead and skip the next module, 90
and go straight ahead to our Intermediate CoreML Module where we're going to be building a really 91
awesome plant identification app. 92
So I'll see you on one of those modules. 93
See you soon.