1
00:00:00,360 --> 00:00:04,680
In this lesson we will be taking a snapshot from the drone camera.

2
00:00:04,680 --> 00:00:11,100
First go to the project tab on the left side of the screen under drone app we created a static folder

3
00:00:11,910 --> 00:00:13,340
under the static folder.

4
00:00:13,350 --> 00:00:19,860
We have the image folder right click on the image folder to create a new directory where we can save

5
00:00:19,860 --> 00:00:25,180
the snapshots enter snapshots as the name of the directory and press.

6
00:00:25,190 --> 00:00:32,770
Okay now go to draw manager P Y and save the folder as a global directory.

7
00:00:33,090 --> 00:00:38,320
Lets call this snapshot image folder and set the path to slash drone app.

8
00:00:38,340 --> 00:00:43,430
STATIC I am G snapshots next.

9
00:00:43,490 --> 00:00:47,920
Go to the edit of the drone manager P Y in the face detect.

10
00:00:47,940 --> 00:00:51,060
Example file we set to raise an error.

11
00:00:51,110 --> 00:00:56,900
If there were no face detected we will be creating code similar to this.

12
00:00:56,990 --> 00:00:59,180
Let's scroll up again.

13
00:00:59,300 --> 00:01:02,750
We need to create an exception class to raise an error.

14
00:01:03,050 --> 00:01:10,560
Copy this error and paste it below the error will come up when there is no image directory.

15
00:01:10,980 --> 00:01:13,220
Also rewrite the description as well.

16
00:01:14,240 --> 00:01:21,590
Now we can receive an error if the image directory doesn't exist under the face detect example file.

17
00:01:21,720 --> 00:01:27,380
We will add in the if statement if not OS path exists.

18
00:01:27,450 --> 00:01:28,410
Snapshot.

19
00:01:28,410 --> 00:01:33,030
Image folder we set to raise the error with F string snapshot.

20
00:01:33,030 --> 00:01:34,800
Image folder does not exist

21
00:01:39,000 --> 00:01:45,330
then set itself is snapshot to false so the snapshot will be disabled.

22
00:01:45,330 --> 00:01:52,470
Now let's scroll down to where we set the face detect method so we can add on codes to enable the snapshots.

23
00:01:52,470 --> 00:01:53,360
OK here we are.

24
00:01:54,030 --> 00:01:58,080
We've set these codes to track the face from the top.

25
00:01:58,080 --> 00:02:05,220
Once the face detectors enable the drone will detect the face and track here we'll be adding the codes

26
00:02:05,220 --> 00:02:06,170
for the snapshot.

27
00:02:07,410 --> 00:02:15,280
If self is snapshot which means the snapshot is enabled a snapshot file will be created.

28
00:02:15,330 --> 00:02:18,340
We can set the name to snapshot JPEG.

29
00:02:18,390 --> 00:02:20,340
One thing we need to be careful.

30
00:02:20,550 --> 00:02:24,500
If there were a snapshot J big file already existing.

31
00:02:24,690 --> 00:02:29,630
This code can rewrite the file so you want to create a backup file.

32
00:02:29,700 --> 00:02:40,140
We can set the file name using time as TR f time with Year Month date hour minute second following by

33
00:02:40,180 --> 00:02:42,730
JPEG.

34
00:02:42,810 --> 00:02:52,440
Next we create a for loop file name with backup file and snapshot file set the file path to OS path.

35
00:02:52,440 --> 00:02:57,240
Join and snapshot image folder and file name.

36
00:02:57,240 --> 00:03:05,040
Then we set a width statement to open the file path and write binary as F and write j peg binary and

37
00:03:05,040 --> 00:03:07,620
the F to save the image.

38
00:03:08,010 --> 00:03:14,370
What we are doing here is we are creating two files one with the time and the other one name snapshot

39
00:03:15,370 --> 00:03:19,840
both files are set in a loop so we can write in both binaries.

40
00:03:20,410 --> 00:03:26,500
The snapshot JPEG is set for the front end side so we can create a more simple code when invoking the

41
00:03:26,500 --> 00:03:28,090
file.

42
00:03:28,090 --> 00:03:34,300
We are able to use the time base file name although it makes the front end code more complicated so

43
00:03:34,300 --> 00:03:41,440
we will be using the snapshot Japan once we finished taking the snapshot we want to disable the snapshot.

44
00:03:42,170 --> 00:03:46,310
So we need to set self is snapshot to false.

45
00:03:46,310 --> 00:03:51,190
Next we go to the create the snapshot method to take the snapshots.

46
00:03:51,320 --> 00:04:00,480
We define the method we enable snapshot by setting is snapshot to true here the j peg file will be generated

47
00:04:00,570 --> 00:04:07,530
and the loop goes on as we enable the snapshot a snapshot file will be created and saved in the folder

48
00:04:08,580 --> 00:04:11,790
to check to see if the snapshot was successfully taken.

49
00:04:11,790 --> 00:04:20,240
We can set retry to zero start a while loop to retry less than three next set.

50
00:04:20,240 --> 00:04:23,190
If not self is snapshot.

51
00:04:23,420 --> 00:04:24,800
If we receive a false.

52
00:04:24,800 --> 00:04:28,360
That means the snapshot was taken so we return true.

53
00:04:29,610 --> 00:04:35,130
If the snapshot isn't taken we will wait for point one second and retry.

54
00:04:38,040 --> 00:04:43,690
If we don't get a snapshot during this while loop we need to return false.

55
00:04:43,770 --> 00:04:47,520
I just realized that I said J pick binary in this line.

56
00:04:47,520 --> 00:04:52,180
This means that the first loop won't work how we want it to.

57
00:04:52,470 --> 00:04:59,130
We should move these two lines before the snapshot loop starts so we can enable snapshot.

58
00:04:59,130 --> 00:05:00,230
Even in the first loop

59
00:05:03,270 --> 00:05:06,620
we finish the snapshot in the back end side.

60
00:05:06,630 --> 00:05:09,210
Now we have to set the front inside.

61
00:05:09,390 --> 00:05:14,400
I want you to go to server P why here we need to add on the snapshot.

62
00:05:14,400 --> 00:05:24,340
Command if CND snapshot the drone snapshot will be invoked let's set an if statement as well so we can

63
00:05:24,340 --> 00:05:28,660
check to see if the snapshot was successfully created or not.

64
00:05:28,840 --> 00:05:33,430
If the snapshot was a success we want to return success.

65
00:05:33,430 --> 00:05:39,490
But if the snapshot doesn't work we want to return the message fail using the error code four hundred

66
00:05:41,320 --> 00:05:41,910
next.

67
00:05:41,950 --> 00:05:46,640
Let's go onto controller HDMI all under the template folder to add the snapshot.

68
00:05:46,660 --> 00:05:48,640
But.

69
00:05:49,040 --> 00:05:52,250
We will be creating similar to the advanced mode.

70
00:05:52,250 --> 00:05:56,060
So let's copy the controller box and paste below.

71
00:05:56,060 --> 00:06:00,080
Here we will be creating the controller for the drone camera.

72
00:06:00,170 --> 00:06:04,460
We can copy the H3 tag and paste below the controller box.

73
00:06:04,460 --> 00:06:09,710
The name can be anything but let's use camera next.

74
00:06:09,950 --> 00:06:14,640
Copy one of the codes to create a button this time for unlock.

75
00:06:14,660 --> 00:06:19,480
We were using send commands but with snapshot we will be customizing a bit.

76
00:06:19,640 --> 00:06:22,250
So set the on click to snapshot.

77
00:06:22,340 --> 00:06:28,200
And name the button snapshot as well place a blank line next.

78
00:06:28,220 --> 00:06:33,230
We set the I.D. to div snapshot and the style will be display.

79
00:06:33,230 --> 00:06:38,590
And this will be set to none so that the display doesn't show up in the beginning.

80
00:06:38,660 --> 00:06:46,280
Then we set the image I.D. to snapshot followed by the source which will be static image snapshot snapshot

81
00:06:46,340 --> 00:06:48,240
JPEG.

82
00:06:48,240 --> 00:06:51,780
Now we can go on and create the snapshot function.

83
00:06:51,780 --> 00:06:57,180
We will be adding the codes to the script set function as snapshot.

84
00:06:57,240 --> 00:07:01,360
Copy the post command and pasted under the snapshot function.

85
00:07:01,650 --> 00:07:08,730
The API command parameter will be command and snapshot first case will be if the snapshot was successfully

86
00:07:08,730 --> 00:07:13,510
done we set div snapshot followed by a show.

87
00:07:13,530 --> 00:07:18,230
So the snapshot image will be set to be shown in the designated area.

88
00:07:18,570 --> 00:07:25,500
When we display the snapshot if it's the first image to be taken it will simply be displayed although

89
00:07:25,740 --> 00:07:30,520
there is a snapshot taken before and you wish to take more snapshots.

90
00:07:30,540 --> 00:07:40,020
We need to add some codes to refresh the displayed image set snapshot 80 are and set source snapshot

91
00:07:40,210 --> 00:07:48,450
at tribute as are C plus a question mark plus math random to add a random number.

92
00:07:48,450 --> 00:07:54,780
Let's see what we just did here scroll back down to the end of this code where we set the snapshot.

93
00:07:54,780 --> 00:08:00,340
Button this J big file will be the first image that the back end receives.

94
00:08:00,690 --> 00:08:07,380
If there were to be multiple snapshots taken the JPEG file will be renewed to the latest image.

95
00:08:07,380 --> 00:08:13,260
What we did with the code earlier was to add a question mark after the file name followed by a random

96
00:08:13,260 --> 00:08:14,770
number like this.

97
00:08:14,940 --> 00:08:20,780
By adding this random number the front end side will understand that this image is a different snapshot

98
00:08:21,060 --> 00:08:24,150
and we'll get the newer image to display.

99
00:08:24,150 --> 00:08:29,690
The question mark will be ignored when it is displayed so there won't be any problems.

100
00:08:29,700 --> 00:08:34,000
This is a javascript based technique that comes in handy.

101
00:08:34,020 --> 00:08:38,850
Finally we are done writing the codes so let's run the script and take some snapshots

102
00:08:41,330 --> 00:08:41,950
OK.

103
00:08:41,970 --> 00:08:43,530
Let's start the program.

104
00:08:43,530 --> 00:08:44,450
You know the routine.

105
00:08:44,460 --> 00:08:50,870
So we go to the controller page first let's hit the takeoff button once the drone is in the air.

106
00:08:50,910 --> 00:08:57,970
You can see that the webcam started streaming the image let's press the snapshot button under the button.

107
00:08:57,990 --> 00:09:01,500
You can see that the snapshot images display.

108
00:09:01,530 --> 00:09:05,230
Now let's fly up the drone so we can get a different angle.

109
00:09:05,310 --> 00:09:13,230
I'm going to enable the face detect and track once a face toward the drone camera the face detect comes

110
00:09:13,230 --> 00:09:14,590
on.

111
00:09:14,690 --> 00:09:16,630
Let's take a couple of snapshots.

112
00:09:16,700 --> 00:09:21,650
You can see that the image goes on updating each time I take a snapshot.

113
00:09:21,740 --> 00:09:27,180
Now let's finish this flight.

114
00:09:27,230 --> 00:09:29,170
So how did your drone do.

115
00:09:29,180 --> 00:09:33,520
I hope you got to take some great snapshots in this chapter.

116
00:09:33,530 --> 00:09:40,700
We created a controller site with a lot of commands such as the initial takeoff and land you can control

117
00:09:40,700 --> 00:09:45,650
your drone in different directions change speed and even flip the drones.

118
00:09:45,650 --> 00:09:51,090
Also we went to some advanced modes such as patrol flights and faced detect.

119
00:09:51,140 --> 00:09:58,130
I think we created quite an app already but you can go on updating this app so your drunk can do all

120
00:09:58,130 --> 00:09:59,800
sorts of new things.

121
00:09:59,870 --> 00:10:06,710
For example a security system you can set your drone to go on a patrol through your home enabled a face

122
00:10:06,710 --> 00:10:07,780
detect and track.

123
00:10:07,790 --> 00:10:14,450
So if the drone detects a face while it is on patrol you can take a snapshot of the intruder.

124
00:10:14,490 --> 00:10:21,370
There is so much more to customize your app so keep on exploring.

125
00:10:21,380 --> 00:10:27,710
In our last chapter we will be making a simple drone game that you can play on your smartphone.

126
00:10:27,710 --> 00:10:29,700
So see you in our next lesson.
