WEBVTT

00:00.170 --> 00:02.990
This is the solution for the activity number ten.

00:02.990 --> 00:08.090
And in this activity we are going to take a series of pictures with the Pi camera.

00:08.120 --> 00:10.460
So I'm going to remove all of that.

00:10.460 --> 00:11.600
And let's first.

00:11.600 --> 00:13.880
So I told you we are going to create a folder.

00:13.880 --> 00:17.030
So let's import the OS module.

00:17.090 --> 00:22.280
And let's create a new folder in our home directory and that folder name.

00:22.280 --> 00:23.810
So I'm going to create a variable.

00:23.810 --> 00:29.630
Actually folder name is going to be slash home pi.

00:29.630 --> 00:33.020
And then let's name it activity ten.

00:33.020 --> 00:37.310
And why not put it actually inside the camera folder.

00:37.310 --> 00:42.500
And then activity activity ten okay.

00:42.530 --> 00:45.380
As I told you you can create whatever folder you want.

00:45.410 --> 00:46.880
And so this is what I do.

00:46.880 --> 00:49.160
If you have a different folder it's completely fine.

00:49.160 --> 00:54.560
Now you see I create a variable here because we're going to use that several times.

00:54.560 --> 01:00.260
And also that's going to be helpful for the well in a few minutes when we will need to create a file

01:00.290 --> 01:05.120
name for the photos we will be able to use this folder name as a base.

01:05.120 --> 01:07.640
So first let's create this folder.

01:07.640 --> 01:10.160
And actually we're not going to just create this folder every time.

01:10.160 --> 01:13.370
We're going to make sure that it exists or not.

01:13.370 --> 01:16.370
And if it doesn't exist then we create it okay.

01:16.370 --> 01:18.770
We don't create the folder if it already exists.

01:18.770 --> 01:31.040
So I'm going to do if and then not OS dot path dot exists with the folder name okay.

01:31.070 --> 01:33.050
And note here I'm using the not.

01:33.080 --> 01:36.230
So this is going to return true or false.

01:36.230 --> 01:41.090
And if it doesn't exist it's going to return false which is what we want.

01:41.120 --> 01:44.600
So I put not and then that becomes true.

01:44.600 --> 01:53.450
So we enter the if and we can do OS dot mkdir with the folder name.

01:53.480 --> 01:53.960
All right.

01:53.990 --> 01:55.190
We create the folder.

01:55.190 --> 01:58.070
If it doesn't exist let's actually test that.

01:58.070 --> 02:05.780
So I'm going to open my file manager in the camera folder And let's run this.

02:05.810 --> 02:06.230
Okay.

02:06.260 --> 02:10.460
And in my camera folder I have a new activity ten folder.

02:10.490 --> 02:11.030
Okay.

02:11.030 --> 02:12.590
So it worked.

02:12.620 --> 02:14.150
Now let's use the camera.

02:14.150 --> 02:16.640
So we will import the camera.

02:16.910 --> 02:23.510
I'm going to do from Pi Cam zero import camera.

02:23.510 --> 02:25.970
And also we're going to import time.

02:26.600 --> 02:26.960
Okay.

02:26.990 --> 02:29.300
And what I'm going to do that here.

02:29.300 --> 02:35.810
So camera is equal to camera I'm going to set the resolution.

02:35.810 --> 02:41.360
So camera dot still size is equal to.

02:41.390 --> 02:49.100
So here I need to give a tuple I'm going to use 1536 and 864.

02:49.100 --> 02:51.260
So that's the same one I used previously.

02:51.290 --> 02:58.580
If you have a different module than the V3 module you might also want to use a different resolution.

02:58.610 --> 02:58.790
Okay.

02:58.820 --> 02:59.990
So this one is up to you.

02:59.990 --> 03:03.830
And then because in my setup the camera was flipped.

03:03.830 --> 03:08.270
I'm going to do camera dot flip camera.

03:08.270 --> 03:10.970
So this one is a function and I need to provide.

03:10.970 --> 03:17.390
So for example h flip is equal true true.

03:17.390 --> 03:21.590
And then v flip is equal to true.

03:21.830 --> 03:22.310
All right.

03:22.310 --> 03:27.590
And to finish I'm going to do time dot sleep two seconds.

03:27.590 --> 03:35.060
And maybe just a print camera has been initialized.

03:37.490 --> 03:38.000
Great.

03:38.000 --> 03:41.690
So this is what we have done previously in this section okay.

03:41.720 --> 03:46.790
We just initialize the camera with a resolution and we flip the image.

03:46.790 --> 03:50.300
Then here we're going to take some photos not just one.

03:50.300 --> 03:53.210
We're going to take a photo every five seconds.

03:53.210 --> 03:56.060
And we want to do that as long as the program runs.

03:56.060 --> 03:58.190
So I'm going to use a while.

03:58.340 --> 03:59.390
True.

03:59.420 --> 03:59.630
Okay.

03:59.660 --> 04:01.250
I'm going to use an infinite loop here.

04:01.250 --> 04:04.190
And what do we do inside.

04:04.250 --> 04:11.240
We're going to take a photo with camera dot take photo.

04:11.240 --> 04:14.150
And here I need to provide a file name.

04:14.150 --> 04:16.070
So that's kind of the challenging part.

04:16.100 --> 04:18.980
Here is what file name will we give.

04:19.010 --> 04:21.050
We already have the beginning.

04:21.080 --> 04:22.850
Here we have the folder name.

04:23.120 --> 04:26.120
So the file name is going to start with this.

04:26.120 --> 04:31.790
And then we need to do slash image plus number and then dot jpg.

04:32.210 --> 04:37.160
So what I'm going to do is I'm going to do file name I'm going to create a variable here.

04:38.120 --> 04:39.770
File name is equal to.

04:39.800 --> 04:42.350
And I'm going to concatenate some strings.

04:42.350 --> 04:44.780
So I'm going to start with folder name.

04:45.500 --> 04:49.340
You see that's quite useful to have created a variable here because I can use it here.

04:49.340 --> 04:52.190
So I don't need to write this a second time.

04:52.190 --> 04:55.760
And then plus and then there's going to be slash.

04:55.760 --> 04:57.470
So I need to add a slash here.

04:57.590 --> 05:00.980
And let's start for example with image one.

05:01.010 --> 05:05.780
Or we could even start with image zero okay dot JPG.

05:05.930 --> 05:06.350
All right.

05:06.350 --> 05:10.580
That's going to be a valid file name for the photo.

05:10.580 --> 05:13.970
But then the problem is we need to change that file name.

05:13.970 --> 05:19.940
So the next time we don't want image zero we want image1 and image2 etc..

05:19.940 --> 05:21.530
So how to change that.

05:21.560 --> 05:23.810
Well I could create a counter actually.

05:23.810 --> 05:26.690
So let's initialize a counter here.

05:27.230 --> 05:29.990
Counter is equal to zero.

05:30.020 --> 05:30.530
Okay.

05:30.560 --> 05:34.790
And how to actually put the value here inside the name.

05:34.790 --> 05:37.850
Well I would need to split this.

05:37.850 --> 05:41.090
So I will add a quote here.

05:41.570 --> 05:46.880
And then plus and then let's say we have the zero and then plus.

05:48.170 --> 05:48.380
Okay.

05:48.410 --> 05:50.720
So this is going to do the same thing.

05:50.720 --> 05:55.130
We have slash image and then the zero and then dot jpg.

05:55.430 --> 05:59.150
But instead of hard coding zero I'm just going to use the counter here.

05:59.150 --> 06:04.040
And because this is an integer we cannot concatenate integer with strings.

06:04.040 --> 06:06.140
So I'm going to need to cast it.

06:06.170 --> 06:12.020
So I'm going to do instead of that I'm going to do str counter.

06:12.500 --> 06:12.920
All right.

06:12.920 --> 06:20.120
And you see now we have created a file name which is slash home slash pi slash camera slash activity

06:20.150 --> 06:20.660
ten.

06:20.690 --> 06:25.250
Then slash image with a number plus an extension.

06:25.250 --> 06:27.560
I'm going to put this file name inside.

06:27.590 --> 06:32.450
So file name inside the camera take photo.

06:32.450 --> 06:35.960
And then very important we need to increment that counter.

06:35.960 --> 06:43.730
So counter plus equal one because the next time we enter the while loop we don't want to use zero.

06:43.760 --> 06:45.410
We want to use one.

06:45.530 --> 06:46.070
All right.

06:46.070 --> 06:50.900
So by doing this we create a different file name every time.

06:50.900 --> 06:55.310
What I will do as well is just use a.

06:55.340 --> 06:56.870
Well it could be anywhere here.

06:56.870 --> 07:02.870
For example print new photo has been taken.

07:02.900 --> 07:03.170
Okay.

07:03.200 --> 07:08.320
Just so that we kind of know what's happening when we run the program, we increment the counter and

07:08.320 --> 07:11.560
then very important we do a time dot sleep.

07:11.890 --> 07:16.180
And I told you it's going to be about well it's going to be five seconds because if.

07:16.210 --> 07:20.560
You don't do a time.sleep here, it's going to take photos as fast as possible.

07:20.560 --> 07:24.220
So you will end up with a lot of files.

07:24.250 --> 07:24.430
Okay.

07:24.460 --> 07:25.750
And that may be a problem.

07:25.780 --> 07:26.290
All right.

07:26.290 --> 07:29.170
And the program here should work.

07:29.200 --> 07:34.270
There's only one thing I'm going to do is because we are using a while loop here.

07:34.270 --> 07:40.360
And when we do control C well, wherever we are here, we're going to exit from the program with a keyboard

07:40.360 --> 07:41.170
interrupt.

07:41.170 --> 07:44.080
So in this case it should be fine.

07:44.110 --> 07:44.350
Okay.

07:44.380 --> 07:45.640
Nothing is going to happen.

07:45.640 --> 07:52.030
But to make the program a bit cleaner I'm going to use also a try except block.

07:52.030 --> 07:53.290
So I'm going to put the while.

07:53.290 --> 07:54.130
So it's quite simple.

07:54.130 --> 07:56.800
You put the try you put this block here.

07:56.800 --> 07:57.670
So I press tab.

07:57.670 --> 07:59.440
You see I just add a tab.

07:59.440 --> 08:07.870
So I just add an indentation for everything and then I go back I do accept keyboard.

08:09.340 --> 08:14.320
interrupt and then I can write pass if I want or I can print something.

08:14.350 --> 08:16.030
Well, it's up to you.

08:16.420 --> 08:18.760
So here we don't really have anything to do.

08:18.790 --> 08:21.580
It's just that we exit in a cleaner way.

08:21.610 --> 08:21.970
All right.

08:22.000 --> 08:23.890
Now we will be able to run that.

08:23.890 --> 08:29.380
So before you run, just also once again make sure you have a time.sleep here in the while loop.

08:29.380 --> 08:31.270
And let's run that.

08:31.270 --> 08:34.120
From what, from Tony or the terminal.

08:34.780 --> 08:37.060
And you can see we have.

08:37.390 --> 08:42.640
So after a few seconds camera has been initialized and new photo has been taken.

08:42.670 --> 08:50.110
Let's go to the camera folder in the activity ten we have image zero and then five seconds later we

08:50.110 --> 08:51.880
have image one.

08:51.880 --> 08:53.470
And then let's wait a bit more.

08:53.560 --> 08:54.940
We have image two.

08:55.330 --> 08:56.980
Let's wait five more seconds.

08:56.980 --> 09:01.690
We have image three, etc. etc..

09:01.690 --> 09:05.740
So it's going to continue to take photos as long as the program is running.

09:05.770 --> 09:07.180
Now we have image four.

09:07.210 --> 09:14.590
I'm going to click here and run Ctrl C and you can see we have a clean exit with end.

09:14.620 --> 09:14.830
Okay.

09:14.830 --> 09:17.830
And I can run it from the terminal as well.

09:18.460 --> 09:27.430
So that was in document Python programs and that was Python three activity ten.

09:29.290 --> 09:30.910
Okay I run it.

09:30.910 --> 09:32.950
And as you can see we have an error here.

09:32.950 --> 09:38.890
So that's the kind of same thing I told you with the Gpio zero is that maybe if you run a script here

09:38.890 --> 09:44.740
first on Sony and then on the terminal, it might not work, especially for stuff that is connected

09:44.740 --> 09:45.580
with hardware.

09:45.580 --> 09:53.830
So in this case, if I close Sony and then I run again, you can see now it is working okay because

09:53.830 --> 09:58.420
somehow Sony was kind of holding resources and now it's working.

09:58.450 --> 10:01.870
I can press Ctrl C and I can start Sony again.

10:01.870 --> 10:04.180
It's not a problem okay.

10:04.210 --> 10:06.940
I can start Sony again and I can run the script again.

10:06.940 --> 10:13.960
It's just that what you can do is for example, you only write the you only write the code here and

10:13.960 --> 10:16.120
then you execute it from the terminal.

10:16.120 --> 10:20.980
But if you execute it from Tony, then you might have an issue in the terminal.

10:21.010 --> 10:21.190
Okay.

10:21.220 --> 10:24.100
But you see here the code is on Tony.

10:24.130 --> 10:30.280
I haven't executed anything here and I can run the code on the terminal.

10:32.590 --> 10:33.010
All right.

10:33.010 --> 10:34.690
And it's going to take a new photo.

10:34.690 --> 10:36.280
So here is going to replace.

10:36.310 --> 10:40.600
It's going to start to replace the image zero and then replace the image one etc..

10:40.600 --> 10:44.680
And maybe add a new file after image five.

10:45.160 --> 10:45.490
All right.

10:45.520 --> 10:47.410
And I'm going to press Ctrl C here.

10:47.410 --> 10:49.960
And that's it for this activity.

10:50.080 --> 10:57.490
Just for completeness I'm going to show you that in the camera module in the Pi zero, there is actually

10:57.490 --> 11:02.830
a functionality that you could use to create a sequence of photos directly.

11:02.830 --> 11:09.130
So here I gave you this challenge to make you actually practice with Python with strings with loops

11:09.160 --> 11:09.340
okay.

11:09.370 --> 11:13.060
So that's a very good exercise, but I'm just going to write it here.

11:13.150 --> 11:14.170
I'm not going to run it.

11:14.170 --> 11:24.640
But for example you could do camera and then dot capture sequence like that and provide the path.

11:24.640 --> 11:25.930
So that's going to be.

11:25.960 --> 11:39.130
So here I'm going to write the full path for example home by camera activity ten slash image dot jpg.

11:39.130 --> 11:41.290
So you don't provide any number here.

11:41.290 --> 11:44.860
And then you will have num images.

11:44.860 --> 11:54.460
So number of images you want for example ten and then interval for example five sequence okay.

11:54.460 --> 12:00.340
If you run that it's going to take ten images separated by five second interval.

12:00.340 --> 12:06.670
And it's going to take this file name and add I think it's going to add A-1 and then two, dash three

12:06.700 --> 12:09.940
etc. it's going to do that automatically for you okay.

12:09.970 --> 12:16.600
So if you know already how many images you want to take, that can be a good thing to do.

12:16.600 --> 12:21.070
But then there are two problems with this function is that, first of all, you need to know how many

12:21.100 --> 12:22.240
images you want.

12:22.270 --> 12:25.240
Here we can just take as many as we want.

12:25.270 --> 12:27.700
It could be one, it could be five, it could be ten.

12:27.730 --> 12:29.470
As long as we run the program.

12:29.590 --> 12:30.040
Okay.

12:30.040 --> 12:34.210
So here you need to know how many images you want in advance.

12:34.210 --> 12:37.420
And then if you run that well it's going to block the program.

12:37.420 --> 12:39.670
And that's the only thing you can do okay.

12:39.670 --> 12:44.440
With a loop like this you can take a photo, but after you take a photo you can do something else.

12:44.440 --> 12:49.000
For example, you could do something with the push button with the LEDs, with the Pearson.

12:49.000 --> 12:50.680
So you could send an email okay.

12:50.710 --> 12:53.740
You could continue to do stuff on the side.

12:53.740 --> 12:56.950
It's not only just taking a sequence of photos, okay.

12:56.980 --> 12:58.960
You can do something else on the side.

12:58.990 --> 12:59.350
All right.

12:59.350 --> 13:00.940
So you have just seen this.

13:00.940 --> 13:03.400
I'm going to comment it and leave it in the solution.

13:03.400 --> 13:06.550
Just so you know that there is also a function like that.

13:06.580 --> 13:07.000
Great.

13:07.000 --> 13:08.590
And that's the end of this activity.

13:08.620 --> 13:13.090
And that's also the end of this section on the Raspberry Pi camera.
