WEBVTT

00:00.080 --> 00:03.260
This is now step two for this final project.

00:03.260 --> 00:06.290
And in this step we are going to focus on the camera.

00:06.290 --> 00:12.020
So we're going to set up the camera and take a photo when the movement has been detected here.

00:12.020 --> 00:13.340
So we already have the print.

00:13.340 --> 00:18.680
So after this print in this if we're just going to take a photo because we know this logic is already

00:18.680 --> 00:19.910
working correctly.

00:19.910 --> 00:26.690
So what I'm going to do is I'm going to do file save as and I'm going to name it Project step two.

00:27.350 --> 00:27.590
Okay.

00:27.620 --> 00:29.870
And I'm going to start from this.

00:29.870 --> 00:35.180
So we already have here I've already organized it with the global variables.

00:35.180 --> 00:38.330
And then we set up the gpios I'm going to put s here.

00:38.330 --> 00:41.120
And we are also going to set up the camera.

00:41.120 --> 00:43.010
So for example we can do it here.

00:43.550 --> 00:46.100
Let's do setup camera.

00:46.490 --> 00:48.410
You could do it before or after the Gpio.

00:48.500 --> 00:49.880
It doesn't really matter.

00:50.060 --> 00:52.220
And let's do this.

00:52.220 --> 00:59.210
So let's import from PyCharm zero import camera.

00:59.210 --> 01:00.670
And then we can do here.

01:00.760 --> 01:01.720
Camera.

01:02.560 --> 01:03.340
Let's create a camera.

01:03.340 --> 01:06.970
Variable is equal to camera.

01:07.660 --> 01:11.380
Then I'm going to give the settings camera dot.

01:12.460 --> 01:21.460
Still size is equal to and tuple with 1036 and 864.

01:21.460 --> 01:24.730
So this will depend on the version you have.

01:24.760 --> 01:28.030
Okay so that's for the version three of the camera.

01:28.030 --> 01:31.540
But if you have another one maybe you will need another resolution.

01:31.540 --> 01:34.870
So you can go back to the section on cameras for more information.

01:34.870 --> 01:45.850
And I'm also going to do camera dot flip camera because my camera is upside down with v flip through

01:45.850 --> 01:48.610
and H flip.

01:48.610 --> 01:51.100
So vertical flip and horizontal flip.

01:52.090 --> 01:54.610
And then just in case I'm going to do time.

01:54.610 --> 01:56.320
So I already imported time here.

01:56.320 --> 02:00.590
So I will do time dot sleep two seconds.

02:00.620 --> 02:00.950
Okay.

02:00.980 --> 02:09.470
Make sure that everything is correctly set up and then print camera setup.

02:10.430 --> 02:11.210
Okay.

02:11.210 --> 02:13.490
So you can see I do camera setup.

02:13.520 --> 02:13.910
Okay.

02:13.910 --> 02:15.620
And then Gpio setup okay.

02:15.650 --> 02:18.860
So then when I start the Python program I can see the camera.

02:18.890 --> 02:19.190
Okay.

02:19.220 --> 02:23.570
Maybe if I get an error after this means that the error is on the Gpio.

02:23.600 --> 02:23.960
Okay.

02:23.990 --> 02:30.620
So I can see better if the initialization is correct and if not where the error is.

02:30.650 --> 02:30.920
Okay.

02:30.950 --> 02:34.010
So we have our camera which is ready to take some pictures.

02:34.010 --> 02:37.910
But now what I'm going to do is I'm going to create a folder.

02:37.940 --> 02:38.180
Okay.

02:38.210 --> 02:41.180
We're going to save those pictures inside a folder.

02:41.210 --> 02:43.250
I'm going to add a new variable here.

02:43.670 --> 02:45.260
I'm going to name camera.

02:46.490 --> 02:47.420
Camera.

02:49.490 --> 02:50.270
Folder.

02:51.410 --> 02:52.250
Path.

02:52.820 --> 02:58.040
And once again I use all uppercase because this is not something I want to modify later.

02:58.070 --> 02:59.690
It's just something I create here.

02:59.720 --> 02:59.960
Okay.

03:00.010 --> 03:02.260
And we just use it as it is.

03:02.260 --> 03:06.550
So by using uppercase I kind of make it a constant variable.

03:06.580 --> 03:07.930
It's not a constant variable.

03:07.960 --> 03:13.450
Technically you could modify it, but at least I know that I should try not to modify it later on.

03:13.480 --> 03:17.200
Okay, so it's a good way to not mess your program later on.

03:17.200 --> 03:20.800
So Camera Folder path is going to be home slash pie.

03:20.830 --> 03:22.720
Let's use our home directory.

03:22.750 --> 03:23.290
Okay.

03:23.290 --> 03:31.330
And let's create a folder named Photos Final Project.

03:31.330 --> 03:33.580
You can create any folder you want.

03:33.910 --> 03:34.900
It doesn't really matter here.

03:34.900 --> 03:36.160
It's the one I have chosen.

03:36.160 --> 03:41.110
And I'm going to create this folder just well just when I initialize the camera, okay.

03:41.140 --> 03:42.040
At the same time.

03:42.040 --> 03:46.360
And to do this I will first need to check if this folder doesn't exist.

03:46.360 --> 03:48.130
Then I'm going to create it.

03:48.160 --> 03:48.670
Okay.

03:48.670 --> 03:51.070
So I will need to import.

03:53.290 --> 03:54.010
OS.

03:54.010 --> 04:08.430
And with the OS module I can do if not OS dot path dot exists and with the camera folder path, then

04:08.430 --> 04:16.050
I do OS dot mkdir to create the folder with camera folder path.

04:16.050 --> 04:18.750
So I check if it doesn't exist.

04:18.750 --> 04:21.000
Then I create it as simple as that.

04:21.000 --> 04:24.480
And that's going to be it for the initialization of the camera.

04:24.510 --> 04:25.800
I can already test that.

04:25.800 --> 04:29.820
So let's do Ctrl s and let's go back here.

04:30.120 --> 04:33.570
I'm going to check also with my file manager in my home directory.

04:33.570 --> 04:39.180
You see I don't have any folder named what was the name photos final project.

04:39.180 --> 04:40.770
So I don't have any folder here.

04:41.130 --> 04:44.700
I'm going to start this actually step one.

04:44.700 --> 04:49.320
So make sure that also when you save as a new file you also run the new one.

04:50.460 --> 04:59.890
So python3 project step two and you see camera setup okay Gpio setup.

04:59.920 --> 05:02.020
Okay I'm going to stop.

05:02.020 --> 05:06.310
And you see now we should have this photos final project.

05:06.310 --> 05:07.390
So we have the folder.

05:07.420 --> 05:07.840
Great.

05:07.870 --> 05:10.150
Now we can save inside this folder.

05:11.170 --> 05:13.270
So I'm going to go back here.

05:13.270 --> 05:17.470
And here what I print I'm going to leave this print okay.

05:17.500 --> 05:20.200
Taking a photo and sending it by email.

05:20.200 --> 05:21.970
And I'm gonna take a photo here.

05:21.970 --> 05:27.430
But instead of taking a photo right here, what I can do is create a function, okay.

05:27.430 --> 05:35.230
And I'm going to create a function here at the beginning of the program def take photo.

05:36.070 --> 05:38.320
And what arguments do we need here.

05:38.320 --> 05:41.710
So to take a photo we will create a file name.

05:41.710 --> 05:46.930
And to create the file name we will need to have the well the base the folder path here.

05:46.930 --> 05:51.160
So we're going to start with this and then add something for the file name.

05:51.160 --> 05:55.090
And what we've done before is that for example you create this variable.

05:55.090 --> 05:57.400
Then you create the function afterwards.

05:57.430 --> 06:00.520
And you use the global variable inside the function.

06:00.520 --> 06:06.100
But what I could also do is to pass the folder path directly inside the function as an argument, so

06:06.100 --> 06:10.330
that I can put this function wherever I can put it at the top of the program.

06:10.360 --> 06:14.590
Okay, it's going to be more modular then I don't need to create a global variable before that.

06:14.590 --> 06:16.600
I can pass it as a parameter.

06:16.600 --> 06:22.570
And the same thing for the camera here, I can just pass the camera as an argument.

06:22.570 --> 06:24.430
So let me show you how to do this.

06:24.430 --> 06:25.900
You will see it quite easy.

06:26.050 --> 06:31.330
So I do camera and then folder path okay.

06:31.330 --> 06:32.470
So that's my function.

06:32.470 --> 06:39.700
And then to call this function I can do here take photo okay.

06:39.730 --> 06:42.040
That's the function I'm calling.

06:42.100 --> 06:46.630
And I need to send it the camera and the folder path.

06:46.630 --> 06:49.300
So the camera is this one okay.

06:49.330 --> 06:53.290
The camera variable and the camera folder path is that one.

06:54.430 --> 06:58.480
So I go back here and I do camera and camera.

06:59.420 --> 07:01.670
folder path.

07:02.630 --> 07:04.490
Let's remove those spaces here.

07:04.580 --> 07:05.000
Okay.

07:05.030 --> 07:11.510
So you can see that we have created our variables here and all the stuff.

07:11.510 --> 07:14.660
So here we still use global variables inside those two functions.

07:14.660 --> 07:18.650
But for the take photo function I put it on the side.

07:18.650 --> 07:19.490
So at the top.

07:19.490 --> 07:22.580
And I just get the camera here as an argument.

07:22.610 --> 07:23.060
Okay.

07:23.090 --> 07:24.200
It's going to work the same.

07:24.200 --> 07:27.140
But this way we can put all our other functions at the top.

07:27.140 --> 07:32.180
And we don't need to worry about having a global variable before that, etc..

07:32.180 --> 07:40.700
So I will do camera dot take photo and I need to create a file name.

07:40.700 --> 07:42.320
So let's create a file name.

07:43.820 --> 07:47.300
That's going to be what that's going to be the folder path here.

07:49.520 --> 07:51.980
Which is whatever we pass to this function.

07:51.980 --> 08:00.520
So in this case it's going to be that one because that's the one we pass Yeah, but you could also you

08:00.520 --> 08:04.510
see you could also use this function with another path name.

08:04.540 --> 08:04.840
Okay.

08:04.870 --> 08:06.400
So the function is quite modular.

08:06.400 --> 08:07.480
What do we do then.

08:07.510 --> 08:11.170
I'm going to add a slash because here I didn't provide a slash.

08:11.170 --> 08:14.710
And I'm going to name it image underscore.

08:14.710 --> 08:18.970
And also I'm going to add the timestamp so the current time to the image.

08:18.970 --> 08:25.780
So an easy way to create an image that has a unique name every time is just to add the current time

08:25.780 --> 08:29.410
to it, because the current time is always going to increase.

08:29.440 --> 08:29.590
Okay.

08:29.590 --> 08:32.530
So you can see time to time is going to give us something like that.

08:32.530 --> 08:36.910
So that's what we're going to write inside the file name.

08:37.540 --> 08:40.240
So plus time dot time.

08:41.020 --> 08:43.240
And note that this is going to be a number.

08:43.240 --> 08:46.570
So to cast it as a string we do str okay.

08:46.600 --> 08:53.350
Because you cannot concatenate numbers and strings you need to concatenate strings with strings.

08:53.380 --> 08:53.800
Great.

08:53.800 --> 08:56.290
So the image is going to be img.

08:56.500 --> 09:04.400
Underscore The time stamp and we need to do plus dot jpg for the extension.

09:04.400 --> 09:04.970
Then we do.

09:05.000 --> 09:08.300
Camera take photo with file name.

09:08.330 --> 09:08.690
All right.

09:08.720 --> 09:12.830
That's going to take the photo in this folder with this name.

09:13.340 --> 09:19.940
And finally I'm just going to do return file name because that's something we're going to use later.

09:20.210 --> 09:24.200
So here actually take photo is going to return.

09:24.200 --> 09:30.830
So I can save it here as photo file name is equal to take photo.

09:30.830 --> 09:33.110
And I'm going to use that in the next step.

09:33.140 --> 09:33.650
All right.

09:33.650 --> 09:35.210
And that should be it for this step.

09:35.210 --> 09:39.380
So before we test the program let's just do a quick recap here.

09:39.380 --> 09:45.470
I have added this block of code to set up the camera and to create a folder to save the photos.

09:45.500 --> 09:53.090
Then I have also added a function here that takes as an argument the camera and the folder path, so

09:53.090 --> 09:59.680
that it will save a photo using the timestamp here in this folder path and also returns the file name.

09:59.680 --> 10:01.540
And where do we call this?

10:01.570 --> 10:08.290
Well, we call this from here this motion finished function with the logic that we have created in the

10:08.290 --> 10:09.340
last step.

10:09.340 --> 10:14.470
When we are sure that we have spent at least five seconds doing a movement, and that the last photo

10:14.500 --> 10:16.600
was taken at least 30s ago.

10:16.630 --> 10:17.980
Then we can take a new one.

10:17.980 --> 10:21.970
So let's say I'm going to open the terminal.

10:21.970 --> 10:23.560
I'm going to run the program.

10:23.560 --> 10:25.120
So step number two.

10:25.300 --> 10:25.960
Okay.

10:26.110 --> 10:27.340
Let's run.

10:29.560 --> 10:30.010
All right.

10:30.040 --> 10:32.020
Camera setup okay Gpio okay.

10:32.020 --> 10:35.380
And all right let's do some movement.

10:35.380 --> 10:39.370
So it's two seconds but let's do five second one.

10:41.680 --> 10:42.340
Okay.

10:42.340 --> 10:45.820
So seven seconds something taking a photo.

10:46.150 --> 10:47.950
So now let's go back here.

10:47.980 --> 10:50.440
Let's see photos final project.

10:50.440 --> 10:52.660
And we have a new photo okay.

10:52.780 --> 10:54.670
You see the timestamp here.

10:54.670 --> 10:58.440
So we have image and the timestamp stamp, then dot jpg.

10:58.470 --> 11:01.680
So this way we are sure that we have a new file every time.

11:02.460 --> 11:03.720
And the photo.

11:04.500 --> 11:09.090
Okay, well you can see it's quite blurred because I was doing movement just in front of the camera.

11:09.090 --> 11:11.160
But we have the latest photo.

11:11.310 --> 11:14.730
So here I don't really care about the quality of the of the photo.

11:14.730 --> 11:16.320
It's going to be blur every time.

11:16.320 --> 11:21.570
But of course, if you want it to do a real application to use in real life, you would need to find

11:21.570 --> 11:22.920
a nice spot for the camera.

11:22.950 --> 11:23.160
Okay?

11:23.190 --> 11:24.750
And fix the camera somewhere.

11:24.780 --> 11:27.420
Make sure that the quality of the image is good.

11:27.450 --> 11:31.500
Okay, but here we're just going to keep it like this with this small camera cable.

11:31.500 --> 11:33.300
That's going to be enough for this project.

11:33.330 --> 11:33.660
Great.

11:33.660 --> 11:37.140
And if I go back here and what you see we have lots of timers.

11:37.140 --> 11:39.060
I'm going to do another one here.

11:40.170 --> 11:44.820
So five seconds at least okay.

11:44.850 --> 11:46.170
Taking a photo.

11:46.380 --> 11:48.540
So that was at least 30s okay.

11:48.570 --> 11:49.770
Since the last one.

11:50.310 --> 11:53.550
And you see now I have a second photo okay.

11:53.580 --> 11:55.560
So I'm going to have a new photo every time.

11:55.590 --> 11:56.010
Great.

11:56.010 --> 12:02.110
And one thing I could do, because now you see, I have every time this starting timer plus the time,

12:02.110 --> 12:05.230
etc. so what I could do is remove.

12:05.230 --> 12:08.320
I'm not going to remove those, I'm going to comment those.

12:08.350 --> 12:08.980
Okay.

12:09.190 --> 12:15.100
So I'm going to comment the print start timer and the print motion duration here because we know it's

12:15.100 --> 12:16.090
currently working.

12:16.090 --> 12:21.640
I could remove them altogether, but I'm just going to put them as a comment in case you want to test

12:21.640 --> 12:22.810
them again later.

12:22.960 --> 12:27.700
Okay, so I save and now instead of all that if I start again.

12:30.580 --> 12:31.240
Okay.

12:31.240 --> 12:34.660
So you will still have the LED that's powered on.

12:34.660 --> 12:37.180
And then if I do movement for five seconds.

12:37.180 --> 12:41.020
So I'm just going to do movement for five seconds in front of the pier.

12:42.880 --> 12:43.150
Okay.

12:43.150 --> 12:45.190
You see taking a photo and sending it by email.

12:45.190 --> 12:47.320
So we only have this log right.

12:47.320 --> 12:50.350
So having logs like that is good as an intermediate steps.

12:50.350 --> 12:54.310
And then when you no longer need them you can remove them or comment them.

12:54.310 --> 12:56.170
And well that's going to be it for this.

12:56.170 --> 12:57.280
Step number two.
