Hey everybody what's going on this is Caleb with Debb slopes.
And in this video we're going to take what we just built in the previous video and we're going to set
it up so that we can actually perform our facial recognition requests.
So let's go back into our view controller here and I'm going to go ahead and import the vision framework.
OK vision is the framework in question.
That is the one we are going to use which is very very powerful and robust there's a lot it can do.
We're barely scratching the surface with this tutorial but in order to actually begin this we need to
start by creating a function K and that function is going to be called perform vision request.
OK.
There we go.
Lovely.
And so what we're going to need is we're going to need to create what is called a V.N. detect face rectangles
request here.
That is a lot of words but it is basically a class that can analyze an image identify faces and return
parameters for where it thinks those faces are and then we can use those parameters to generate shapes
to place over the faces.
So let's go ahead and create that request.
Let face detection request equals V and detect.
And like I said it's the end detect face rectangles request just like that.
But if I push enter on this you can see that we get returned what is called a request and an optional
error.
Should there be a problem.
And we're going to actually begin by handling that error if there is one.
So if left error equals error.
OK.
If there happens to be an error and it is not nil we're going to set it to a constant called error and
we'll print it out.
So failed to detect face and then we're going to go ahead and print out that error.
After that we're going to go ahead and return because if there's an error there's no need for this request
to continue.
Now assuming there is no air and that we are good to go we're going to go ahead and actually pull out
the results of our request and for every result we're going to go ahead and cycle through and pull out
a parameter called a V.N. face observation.
So go ahead and type request dot results dot for each.
We're going to use a For Each loop.
All righty.
And for every results result sorry singular for every result we're going to go ahead and create a face
observation object.
And it is optional so let's use guard light to be safe guard let's face observation equals results and
we're going to cast it as V.N. face observation.
Otherwise we will return.
So now essentially I should back up essentially what we're going to do is eventually we're going to
pass in this request into what's called a V.N. image request handler.
OK.
And we're basically going to pass in the image.
We're going to pass in this request and it's going to process the image and if there are any faces that
it finds based on how it's been trained it's going to return an observation of that face.
And we're going to see what's actually inside this object.
So we know what to do with it.
So we're going to go ahead and print face observation.
OK.
And there's a parameter in here called bounding box and what it is is it's essentially the coordinates
and size of a box of the detected object or the face.
So I'm going to go ahead and print that out.
However we have not yet used this parameter so it's not going to do anything yet but we are going to
get there right now.
So let's go ahead and create the handler where we're going to actually pass in this request.
Let's go ahead and type let image requests handler equals V.N. image request handler like so.
And we'll put a constructor here at the very end and we're going to choose the one that requires a CEG
image and options.
Now the options we don't actually have any for this so we're just going to use an empty dictionary.
But we do need HEG image and we're going to have to pass that in at the very beginning so perform vision
quest for image.
And let's make that of type Siggi image.
Now the issue is that we need to actually have a C.G. image but the image in our app is of tight UI
image.
So we're going to go ahead and create a CGI image like so and we can actually go ahead and just type
image and we can go ahead and just pull out the C.G. image from within that.
But it is optional because not every image is going to have an underlying C-g C-g image.
It's hard to say.
So what we're going to do is we're going to be safe and we're going to say else and we're going to print
out an error in the instance that this doesn't work we'll say you could not find CGI image just like
that.
And then we will return.
That'll tell us whether or not our CGI image is legit and in order to actually put this CGI image somewhere
important.
We're going to go ahead and pass it in right here.
We'll just call our function perform vision request for C.G. image and we'll pass it in boom just like
that.
So now that we have sent in this C.G. image we can pass it in through our function here.
It comes in as the property image.
And what this request handler is going to do is we're going to be able to call a function called perform
and we can pass in our request it'll do all of the work for the image and then return results to us
and actually print out you know this bounding box it'll it'll do all of the work that we've asked it
to do.
Now if we go ahead and we try to call this image request handler does perform you'll see that it throws
errors.
Kate I'm sure you saw that.
So we're going to need to actually put this inside of a do catch block to be safe.
So go ahead and type do.
And then at the bottom type catch and that's where errors are going to be thrown which is exactly what
we want.
And go ahead and cut this and paste it in.
However we still need to call try at the beginning we need to tell it to try and if it can't then it
will throw errors here in that instance.
We're going to print something like a message telling what happened.
Failed to perform image request and then we're going to actually print out the error itself but we'll
get its localized description so that it's a little easier to deal with.
So there is that and then we can go ahead and return.
No need to go further if there is an error like so.
And now assuming there's not an error and that we can in fact perform our request we can pass in our
face detection request.
However it is expecting an array.
And what that means is that you could pass in 500 requests and what it would do is it would parse through
each one in order and perform all of the requests that you ask whether it's scanning faces identifying
barcodes or rectangles you know tracking things like that.
Vision is so cool.
OK.
So we have now passed in our image.
We have now performed our request.
And with that in mind we should get an observation back and we should get a bounding box that will print
out.
So I don't know about you but I think it would be great to try this and just see what we get printing
out.
And let's let's go ahead and print a little tag here at the beginning bounding box and maybe a line
break and that will help us to see where our stuff is so let's build and run this and our function should
get called right away.
When our app loads and then we should get a nice little print out.
So it's loading doing its thing.
Oh and we already got one that is really cool stuff.
Check this out.
We get what is called a bounding box.
And just so you know if we go into the bounding box parameter you'll see that it's a C-g recked but
we need to know what that actually means.
So if we dive deeper into V.N. face observation and go find bounding box we can get a nice little description
of what that actually means.
And you know what.
Maybe easier just to.
Yes to do that.
OK.
It is the bounding box of the detected object.
The coordinates are normalized to the dimensions of the processed image.
Meaning they're not exactly you know the right coordinates they're in reference to the actual size of
the image so we need to do some math to upgrade them to fit the image in the view.
And the origin of those points is the image is lower left corner.
So down here this is interesting because typically C-g wrecked rectangles or images or anything.
The top left corner is usually the origin point so it's a little interesting we have to do some math
to kind of flip things.
But what you need to know is this is the x coordinate.
This is the y coordinate for the origin point of the box.
And then this is the length or I guess the width and the height.
So it's showing you all four parameters.
This is really great that's what we need.
So let's go ahead and with these parameters head over to the next video where we're going to actually
put this into something valuable.
You know what this means though vision is doing the work for us it found a face and it's telling us
exactly where it is in the next video we're going to do some math and drop a huge eye view on the screen
to outline the face based on the values that we've received from vision so cool super easy actually.
And let's head over there now.
Let's do it.