WEBVTT

00:00.080 --> 00:04.760
In this lesson you will learn how to take a photo with Python using the Raspberry Pi camera.

00:04.760 --> 00:07.340
So how can we take a photo?

00:07.370 --> 00:10.370
Well, here again we are going to use a Python module.

00:10.370 --> 00:12.650
And which module will we use?

00:12.680 --> 00:16.730
Well the name is going to be pi cam zero.

00:16.730 --> 00:22.430
So we have the Gpio zero for the Gpio and we have the Pi cam zero for the camera okay.

00:22.430 --> 00:24.320
It's quite easy to use library.

00:24.350 --> 00:27.500
Now this one is not installed by default.

00:27.500 --> 00:29.030
So we will need to install it.

00:29.060 --> 00:31.100
Let's open a terminal.

00:31.100 --> 00:34.610
And we are going to try to install it with apt first.

00:34.610 --> 00:40.640
So the first thing I need to do I'm going to do a sudo apt and update to update all the sources.

00:40.640 --> 00:43.640
So I have the latest available versions.

00:45.500 --> 00:46.550
All right.

00:47.480 --> 00:51.950
And well you can see already I have 81 packages that can be upgraded.

00:51.950 --> 00:53.840
So what I'm going to install the package now.

00:53.840 --> 01:00.680
But as best practice if you see this you need to upgrade the packages with sudo apt upgrade.

01:00.710 --> 01:02.600
Okay I'm just going to do that later.

01:02.600 --> 01:04.580
If you have this, you can do that first.

01:05.240 --> 01:16.160
So let's do a sudo apt install and then python3 dash and let's type pi cam zero.

01:16.160 --> 01:17.870
Actually let's press tab here.

01:17.870 --> 01:19.730
And you can see it is found.

01:19.730 --> 01:22.400
So we can install this library with apt.

01:22.430 --> 01:23.630
Let's run.

01:23.660 --> 01:25.070
So let's press enter.

01:25.070 --> 01:27.920
And you can see that might be a lot of packages actually.

01:27.920 --> 01:35.600
So it's gonna download 50MB and add about 250MB of space.

01:35.630 --> 01:38.990
And here for me it's going to install 65 new packages.

01:38.990 --> 01:39.890
So it's quite a lot.

01:39.920 --> 01:41.060
Let's press enter.

01:41.060 --> 01:44.480
And let's wait for the installation to finish.

01:45.890 --> 01:48.230
And the package is successfully installed.

01:48.230 --> 01:52.160
So it took a few minutes for me but now it is done.

01:52.160 --> 01:56.870
So let's just test that we can use actually that we can import this library.

01:56.870 --> 02:01.130
So you open a Python shell from from here or even from the terminal.

02:01.160 --> 02:08.220
So Python three let's do import and then become zero.

02:09.660 --> 02:15.600
And you see, well, we don't get an error, which means that it is correctly installed and we can use

02:15.600 --> 02:15.720
it.

02:15.720 --> 02:18.270
So exit okay.

02:18.300 --> 02:20.640
And let's go to Tony.

02:20.640 --> 02:22.260
So how to use that library.

02:22.260 --> 02:31.380
Well we will do from Pi zero import and then camera with an uppercase camera like this.

02:31.470 --> 02:34.710
And then you can import a camera with.

02:34.740 --> 02:39.210
So let's create a camera variable camera is equal to camera.

02:39.210 --> 02:40.980
And then open and close parentheses.

02:41.010 --> 02:46.530
If you remember how we initialized an LED or a pushbutton it's very similar okay.

02:46.560 --> 02:53.670
You import the functionality from the library and then you just create a variable and you put the name

02:53.670 --> 02:55.410
with open close parentheses.

02:55.410 --> 02:57.240
Here we don't need to provide anything.

02:57.240 --> 03:02.940
And just a quick note is that here again we do from module import functionality.

03:02.970 --> 03:12.620
Let's say we just did Import pi can zero and let's comment that line.

03:13.220 --> 03:20.510
So if I did just that then I would need to do pi zero dot camera.

03:20.510 --> 03:21.770
So it's the same.

03:21.800 --> 03:26.960
Okay I import the whole module and then I do module dot functionality.

03:26.960 --> 03:28.610
So you can do it like this.

03:28.610 --> 03:31.520
Or you can just import the functionality that we want.

03:31.550 --> 03:33.050
And that's what I'm going to do now.

03:33.260 --> 03:36.290
So I do from Pi Zero import camera.

03:36.290 --> 03:38.450
And then I can use camera directly.

03:39.950 --> 03:40.340
All right.

03:40.340 --> 03:42.680
So this is going to initialize the camera.

03:42.710 --> 03:45.890
Now if you remember we did two things on the terminal.

03:45.890 --> 03:48.140
We changed the resolution.

03:48.140 --> 03:52.130
And we also did a vertical flip and a horizontal flip.

03:52.130 --> 03:54.110
We will need to do the same thing here.

03:54.320 --> 03:56.330
So let's do camera.

03:56.570 --> 03:58.130
And we have an option.

03:58.130 --> 04:01.640
It's called steal size okay.

04:01.670 --> 04:08.250
So steal for the photo like you had the steal comment on the terminal steal Still size and this one

04:08.250 --> 04:12.330
you need to do equal and give a what's called a tuple.

04:12.660 --> 04:18.750
I'm going to write 1536 and 864.

04:18.780 --> 04:20.400
That's the resolution we want.

04:20.430 --> 04:20.730
All right.

04:20.760 --> 04:23.700
So you write a tuple like this with parentheses.

04:23.700 --> 04:27.630
And then each item is separated by a comma.

04:27.660 --> 04:34.710
So we haven't seen tuples yet but very basically put a tuple is the same as a list.

04:34.740 --> 04:41.010
The only difference is that well you can modify elements in the list and you can add for example elements.

04:41.010 --> 04:43.380
But with a tuple you cannot do that.

04:43.410 --> 04:43.620
Okay.

04:43.650 --> 04:47.460
So tuple is created and after it's created you cannot modify it.

04:47.460 --> 04:48.690
That's the difference.

04:48.690 --> 04:50.880
And instead of brackets we have parentheses.

04:50.880 --> 04:51.360
That's it.

04:51.360 --> 04:52.950
And why we write a tuple.

04:52.950 --> 04:55.770
Well simply because this option is expecting a tuple.

04:55.770 --> 04:57.600
So we need to give a tuple.

04:57.600 --> 05:02.250
So we have camera dot still size is equal to tuple.

05:02.280 --> 05:07.860
Then we can do camera dot flip camera.

05:07.860 --> 05:10.100
And this one is actually not an option.

05:10.100 --> 05:12.140
This one is a function.

05:12.170 --> 05:12.410
Okay.

05:12.440 --> 05:19.250
So we actually are calling a function and you need to do so we can have h flip.

05:19.250 --> 05:23.210
We're going to provide h flip argument and put it to true.

05:23.210 --> 05:24.200
So is equal true.

05:24.200 --> 05:29.840
And then comma and then v flip is equal to true.

05:29.870 --> 05:31.580
And close the parentheses.

05:31.610 --> 05:31.970
All right.

05:32.000 --> 05:38.900
So we call the function flip camera with those two arguments h flip and v flip.

05:38.900 --> 05:41.000
You can put only one of those if you want.

05:41.030 --> 05:42.470
Here we need the two.

05:42.500 --> 05:43.040
Great.

05:43.040 --> 05:48.560
And then what we're going to do is that after the camera is initialized I'm going to add.

05:48.560 --> 05:50.390
So I'm going to import time.

05:52.190 --> 05:58.370
And I'm going to add a time dot sleep for two seconds.

05:58.370 --> 06:05.210
So just once after we initialize the camera I'm just giving two seconds for the camera to be fully initialized.

06:05.210 --> 06:12.610
And maybe the autofocus to work correctly just so that we have a good quality image, and after that,

06:12.610 --> 06:14.920
then we can take a photo.

06:14.950 --> 06:15.340
All right.

06:15.370 --> 06:17.440
So that's the initialization of the camera.

06:17.440 --> 06:19.420
So it's not really too complicated.

06:19.450 --> 06:23.170
You create a camera here and then you set for example the resolution.

06:23.170 --> 06:28.360
You flip the camera, you wait for a few seconds and then you can do whatever you want.

06:28.390 --> 06:35.530
So to take a photo you have camera dot and you have the take photo function.

06:35.560 --> 06:35.770
Okay.

06:35.770 --> 06:36.850
So quite easy.

06:36.850 --> 06:42.580
And the parameter we need to give here is the path for the file to save the photo.

06:42.580 --> 06:44.590
So we want to go in this.

06:44.620 --> 06:46.810
Well still in this camera folder here.

06:46.810 --> 06:48.850
And you can see the path here.

06:48.850 --> 06:53.380
We're going to put the absolute path is slash home slash pi slash camera.

06:53.860 --> 07:03.670
Let's write this slash home slash API slash camera slash and let's call it my photo dot jpg.

07:03.820 --> 07:06.490
So I close everything all right.

07:06.490 --> 07:10.030
And well let's just add a print.

07:10.690 --> 07:11.680
Um, done.

07:11.730 --> 07:17.970
Just so we know that the photo has been taken, because at least we have something printed on the terminal.

07:18.000 --> 07:18.390
Great.

07:18.390 --> 07:23.430
So the only thing left to do is to run this program, actually, so we can do it from here or from the

07:23.430 --> 07:24.210
terminal.

07:24.240 --> 07:25.740
Let's run it here.

07:27.300 --> 07:27.600
Okay.

07:27.630 --> 07:28.380
So we wait a bit.

07:28.410 --> 07:30.570
It's going to initialize the camera and wait for two seconds.

07:30.570 --> 07:31.800
And then you see done.

07:31.830 --> 07:35.310
And after it's done we can see our photo here.

07:35.310 --> 07:36.300
My photo.

07:36.300 --> 07:37.680
And that's what.

07:37.890 --> 07:40.980
That's a photo that's been taken from the Python script.

07:40.980 --> 07:43.320
As you can see here, we didn't have a preview.

07:43.320 --> 07:45.120
So that's something you could enable if you want.

07:45.150 --> 07:47.760
But we don't really need that from a Python script.

07:47.790 --> 07:48.360
Okay.

07:48.390 --> 07:53.340
The preview was available by default on the terminal, so it's quite nice.

07:53.340 --> 07:58.830
Also, you can correctly position the camera and make some tests, but if you are working from a Python

07:58.830 --> 08:04.260
script and you're going to integrate the camera functionality in a bigger project, you don't necessarily

08:04.260 --> 08:09.870
want to have a preview every time, okay, you know that the camera is set up, so you just initialize

08:09.870 --> 08:14.400
it once and then you just take a photo like that, it's going to be much more efficient.
