In this lesson we will be taking a snapshot from the drone camera.
First go to the project tab on the left side of the screen under drone app we created a static folder
under the static folder.
We have the image folder right click on the image folder to create a new directory where we can save
the snapshots enter snapshots as the name of the directory and press.
Okay now go to draw manager P Y and save the folder as a global directory.
Lets call this snapshot image folder and set the path to slash drone app.
STATIC I am G snapshots next.
Go to the edit of the drone manager P Y in the face detect.
Example file we set to raise an error.
If there were no face detected we will be creating code similar to this.
Let's scroll up again.
We need to create an exception class to raise an error.
Copy this error and paste it below the error will come up when there is no image directory.
Also rewrite the description as well.
Now we can receive an error if the image directory doesn't exist under the face detect example file.
We will add in the if statement if not OS path exists.
Snapshot.
Image folder we set to raise the error with F string snapshot.
Image folder does not exist
then set itself is snapshot to false so the snapshot will be disabled.
Now let's scroll down to where we set the face detect method so we can add on codes to enable the snapshots.
OK here we are.
We've set these codes to track the face from the top.
Once the face detectors enable the drone will detect the face and track here we'll be adding the codes
for the snapshot.
If self is snapshot which means the snapshot is enabled a snapshot file will be created.
We can set the name to snapshot JPEG.
One thing we need to be careful.
If there were a snapshot J big file already existing.
This code can rewrite the file so you want to create a backup file.
We can set the file name using time as TR f time with Year Month date hour minute second following by
JPEG.
Next we create a for loop file name with backup file and snapshot file set the file path to OS path.
Join and snapshot image folder and file name.
Then we set a width statement to open the file path and write binary as F and write j peg binary and
the F to save the image.
What we are doing here is we are creating two files one with the time and the other one name snapshot
both files are set in a loop so we can write in both binaries.
The snapshot JPEG is set for the front end side so we can create a more simple code when invoking the
file.
We are able to use the time base file name although it makes the front end code more complicated so
we will be using the snapshot Japan once we finished taking the snapshot we want to disable the snapshot.
So we need to set self is snapshot to false.
Next we go to the create the snapshot method to take the snapshots.
We define the method we enable snapshot by setting is snapshot to true here the j peg file will be generated
and the loop goes on as we enable the snapshot a snapshot file will be created and saved in the folder
to check to see if the snapshot was successfully taken.
We can set retry to zero start a while loop to retry less than three next set.
If not self is snapshot.
If we receive a false.
That means the snapshot was taken so we return true.
If the snapshot isn't taken we will wait for point one second and retry.
If we don't get a snapshot during this while loop we need to return false.
I just realized that I said J pick binary in this line.
This means that the first loop won't work how we want it to.
We should move these two lines before the snapshot loop starts so we can enable snapshot.
Even in the first loop
we finish the snapshot in the back end side.
Now we have to set the front inside.
I want you to go to server P why here we need to add on the snapshot.
Command if CND snapshot the drone snapshot will be invoked let's set an if statement as well so we can
check to see if the snapshot was successfully created or not.
If the snapshot was a success we want to return success.
But if the snapshot doesn't work we want to return the message fail using the error code four hundred
next.
Let's go onto controller HDMI all under the template folder to add the snapshot.
But.
We will be creating similar to the advanced mode.
So let's copy the controller box and paste below.
Here we will be creating the controller for the drone camera.
We can copy the H3 tag and paste below the controller box.
The name can be anything but let's use camera next.
Copy one of the codes to create a button this time for unlock.
We were using send commands but with snapshot we will be customizing a bit.
So set the on click to snapshot.
And name the button snapshot as well place a blank line next.
We set the I.D. to div snapshot and the style will be display.
And this will be set to none so that the display doesn't show up in the beginning.
Then we set the image I.D. to snapshot followed by the source which will be static image snapshot snapshot
JPEG.
Now we can go on and create the snapshot function.
We will be adding the codes to the script set function as snapshot.
Copy the post command and pasted under the snapshot function.
The API command parameter will be command and snapshot first case will be if the snapshot was successfully
done we set div snapshot followed by a show.
So the snapshot image will be set to be shown in the designated area.
When we display the snapshot if it's the first image to be taken it will simply be displayed although
there is a snapshot taken before and you wish to take more snapshots.
We need to add some codes to refresh the displayed image set snapshot 80 are and set source snapshot
at tribute as are C plus a question mark plus math random to add a random number.
Let's see what we just did here scroll back down to the end of this code where we set the snapshot.
Button this J big file will be the first image that the back end receives.
If there were to be multiple snapshots taken the JPEG file will be renewed to the latest image.
What we did with the code earlier was to add a question mark after the file name followed by a random
number like this.
By adding this random number the front end side will understand that this image is a different snapshot
and we'll get the newer image to display.
The question mark will be ignored when it is displayed so there won't be any problems.
This is a javascript based technique that comes in handy.
Finally we are done writing the codes so let's run the script and take some snapshots
OK.
Let's start the program.
You know the routine.
So we go to the controller page first let's hit the takeoff button once the drone is in the air.
You can see that the webcam started streaming the image let's press the snapshot button under the button.
You can see that the snapshot images display.
Now let's fly up the drone so we can get a different angle.
I'm going to enable the face detect and track once a face toward the drone camera the face detect comes
on.
Let's take a couple of snapshots.
You can see that the image goes on updating each time I take a snapshot.
Now let's finish this flight.
So how did your drone do.
I hope you got to take some great snapshots in this chapter.
We created a controller site with a lot of commands such as the initial takeoff and land you can control
your drone in different directions change speed and even flip the drones.
Also we went to some advanced modes such as patrol flights and faced detect.
I think we created quite an app already but you can go on updating this app so your drunk can do all
sorts of new things.
For example a security system you can set your drone to go on a patrol through your home enabled a face
detect and track.
So if the drone detects a face while it is on patrol you can take a snapshot of the intruder.
There is so much more to customize your app so keep on exploring.
In our last chapter we will be making a simple drone game that you can play on your smartphone.
So see you in our next lesson.