In our previous lesson we made the drone detect faces in this lesson we will make the drone tracked
the detected faces before we go into the codes.
I want to explain you what we will be creating.
Look at this image in the screen the outer screw will be the frame the x and y coordinates will be zero
and zero.
The width will be a maximum of 320 and height will be 240 the small square will be the area where the
face is detected once the face is detected the x and y coordinates width and height can be calculated.
Also the center point of the face can be calculated by dividing the width and height of the rectangle.
Here I marked it with a red dot.
The center point of the frame can be calculated as well.
When the drone detects the face and determines the center of the face we will set the system to calculate
the gap between the face Center and the frame center.
Based on this gap the drone can fly to the point where the face is set to the center of the frame for
example in this case the face is at a higher point.
So the drone should fly up also the is on the left side so the drone should be flying to the left.
This can make the drone constantly track the dejected face by calculating the area of the frame and
the face.
We can also determine the occupancy rate if the occupancy rate is too high.
It means that the drone is too close to the face.
So the drone needs to fly back.
If it's too low it means that the drone needs to fly forward and get closer to the face.
Based on this idea we will be creating our codes go to the drone manager and scroll down to the video
JPEG generator where we set the for loop to detect the face.
First we need to calculate the face center X..
This will be X plus half of the width.
We will do the same for calculating the face center Y which will be Y plus half the height.
Let's see in the image the face center will be determined by adding half the with 2 x and half the height
to Y.
Next we want to get the gap between the frame Center and the space center.
Let's set this using div as indifference.
Def X will be set with frame center X which we created previously minus face center X..
This will be the same for height div y will be set to frame center Y minus face center Y then we calculate
the face area which will be with times height.
Next we calculate the face area percentage.
Let's call this percentage.
Percent face which will be face area divided by frame.
Area the drone will be flying based on these numbers to attract the face.
Let's check on the API that we will be using.
We will be using this go x y z speed.
This command is used when you want to fly the drone to determine point x y z under the velocity of a
centimeter per second.
And the description it shows that each value for X Y and Z should be from negative 500 to 500 remembers.
Check the note that says the value for x y and z can not set to my negative 20 to twenty simultaneously.
In this lesson we will be using 30 centimetres.
Now I want to take a moment to explain the x y and z.
Here is an image.
This middle point will be where the drone is that this arrow which is where the X is will mean forward.
The arrow Z means up minus Y means right.
So if you want to fly the drone forward you need to add value to X to fly up.
You will be adding value to Z to fly right.
You will increase the value at minus Y.
Some of you may feel awkward having y on the left side.
There may be changes in this area so you may want to check when there is an update to the SDK or if
there is a far more update on your drone.
If you feel that the drone is not flying correctly you might want to check on the changes.
At this moment which is April 20 19 the value for right will be minus Y for flying down is minus Z.
Flying back will be minus X and flying left is why I think it will be easier to understand once we run
the code.
So let's go back to the script and start making the codes.
Scroll down a bit and set the initial values.
Drone X drone y drone Z and speed x y z will be set to zero and the speed will be self speed.
Now we set and if statement if diff X is smaller than negative 30 the drone y needs to be negative 30
to set the face center in the middle.
We need to fly the drone to the right by 30.
That is the reason we set drone y to negative 30.
You might think why we set this at 30.
If we set it to a smaller number the drone will be constantly moving around every time the face makes
a slight move to have the drone fly smoothly.
30 will be a good number if you prefer a different value.
You can change interior preferred.
No.
Let's go on.
If the diff X is bigger than 30 we need to fly the drone to the left.
So drone y will be set to 30.
Next is to fly down the drone.
If the diff Y is smaller than negative 15 we need to fly the drone down.
So drone Z is set to negative 30.
If diff Y is bigger than 15 the drone Z is set to 30.
Next is the face percentage if percent face is larger than 30 percent.
This means that the drone is too close to the face.
Here we need to set drone X to negative 30.
So the drone can backup if percent face is smaller than 2 percent.
This means that the drone is too far from the face.
Here we need to set drone X to 30 so the drone can come forward.
Lastly we need to send this command self command a string go with drone X drone y drone Z and speed.
If we run this code as it is there will be some problems.
Once we run this code the drone will fly based on the command go.
Let's check the send command method.
Here we have a while loop that is waiting for a response.
The drone may take a few seconds to start flying.
Meanwhile the face detect loop will be processing every millisecond and producing video images while
the send command is waiting for a response.
The video image will be stored and when it goes to the video binary generator the video will show a
delayed video to solve this problem.
We will be setting the CENTCOM in to a thread.
First we need to add an attribute to the initial settings.
So scroll up to the minute it.
Let's set the thread above the send command.
Here we will be using command semaphore.
This will limit the number of command that will process if there is a good man sent while one command
is processing the later command will be on hold or will try the process again later.
We can set the semaphore to one next.
We set the command thread.
Since we haven't set this yet we will set it to none.
Now let's go to the send command.
We don't want to change the name of the method so let's play some underscore in front of the sudden
command then defined said command.
By doing this we can use the original send command as how it is in the news send command.
We set self command and blocking to true next is self command thread and set threading thread.
The target will be the original send command with the underscore arcs will be command and blocking.
Next we want to run this thread.
So let's set self command thread to start
we've added the blocking option.
So we need to add it to the original send command and set this to true.
Let me explain what this blocking option does to the commands.
Once the blocking is set to true while the first command is processing the rest of the following commands
will be on hold.
Once the first command finishes the next command waiting in line will be processed.
This may cause some delays which we don't want.
Rumors streaming videos.
If there is a consecutive command like right left right left we want to ignore the commands which comes
in while the drone is flying right.
This is where we set the is acquired to self command semaphore acquire the blocking will be set to blocking
once the process is unlocked.
We want to set a width statement to context lib exit stack as stack and release the lock.
So we set stack call back with self command semaphore release the following process needs to go under
this width statement.
So let's set it back.
In case the lock doesn't come off we need to set the program to log a warning the status will be not
acquired so we know what happens.
Let's go back and check on what we've made.
We receive a command with Sen command if the commands are a consecutive command such as right.
Right.
If the blocking option is true this blocking option will also become true.
While the first command right is in process the thread will be locked and the second right command will
be on hold once the first lock is released.
The next command right will process this will go on until all the commands are processed.
If the blocking option is false the first command right will be processed although the second and third
command right will not be acquired and will leave a warning log and won't be processed.
Now go back to the go drone.
X y z speed command.
Here we add in the blocking option and set this to false.
Now we've set the process to ignore commands while the drone is processing the first command.
We can avoid the delayed videos once you've finished creating all these codes.
Try it out and see how your drone works.
I'll be running this program as well.
Let's go to the controller page and take off.
I hit the up button so that the drone doesn't go down.
Landing mode.
Now I hit.
Turn the face detect and track mode to on.
I'm going to go in front of the drone so my face can be detected on the left bottom of the screen you
can see the webcam.
The blue rectangle is around my face which means the drone has found my face as I stand up straight.
You can see the drone is tracking my face and flew up.
Let me go to the right.
So the drone can track me.
Well I think the light was too bright so the color balance changed and the drone can interject my face.
Let me move to the left.
You can see that the drone found my face again and is starting to track if I squat down.
You can see that the drone also comes down.
But if my face is out of the frame you'll see that the face detect drops again.
You might want to try this in a bigger room so that you have plenty of space for the drone can track
you.
I hope your drone tracked you down as well.
I'll see you in the next lesson.