This is the solution for the activity number ten.
And in this activity we are going to take a series of pictures with the Pi camera.
So I'm going to remove all of that.
And let's first.
So I told you we are going to create a folder.
So let's import the OS module.
And let's create a new folder in our home directory and that folder name.
So I'm going to create a variable.
Actually folder name is going to be slash home pi.
And then let's name it activity ten.
And why not put it actually inside the camera folder.
And then activity activity ten okay.
As I told you you can create whatever folder you want.
And so this is what I do.
If you have a different folder it's completely fine.
Now you see I create a variable here because we're going to use that several times.
And also that's going to be helpful for the well in a few minutes when we will need to create a file
name for the photos we will be able to use this folder name as a base.
So first let's create this folder.
And actually we're not going to just create this folder every time.
We're going to make sure that it exists or not.
And if it doesn't exist then we create it okay.
We don't create the folder if it already exists.
So I'm going to do if and then not OS dot path dot exists with the folder name okay.
And note here I'm using the not.
So this is going to return true or false.
And if it doesn't exist it's going to return false which is what we want.
So I put not and then that becomes true.
So we enter the if and we can do OS dot mkdir with the folder name.
All right.
We create the folder.
If it doesn't exist let's actually test that.
So I'm going to open my file manager in the camera folder And let's run this.
Okay.
And in my camera folder I have a new activity ten folder.
Okay.
So it worked.
Now let's use the camera.
So we will import the camera.
I'm going to do from Pi Cam zero import camera.
And also we're going to import time.
Okay.
And what I'm going to do that here.
So camera is equal to camera I'm going to set the resolution.
So camera dot still size is equal to.
So here I need to give a tuple I'm going to use 1536 and 864.
So that's the same one I used previously.
If you have a different module than the V3 module you might also want to use a different resolution.
Okay.
So this one is up to you.
And then because in my setup the camera was flipped.
I'm going to do camera dot flip camera.
So this one is a function and I need to provide.
So for example h flip is equal true true.
And then v flip is equal to true.
All right.
And to finish I'm going to do time dot sleep two seconds.
And maybe just a print camera has been initialized.
Great.
So this is what we have done previously in this section okay.
We just initialize the camera with a resolution and we flip the image.
Then here we're going to take some photos not just one.
We're going to take a photo every five seconds.
And we want to do that as long as the program runs.
So I'm going to use a while.
True.
Okay.
I'm going to use an infinite loop here.
And what do we do inside.
We're going to take a photo with camera dot take photo.
And here I need to provide a file name.
So that's kind of the challenging part.
Here is what file name will we give.
We already have the beginning.
Here we have the folder name.
So the file name is going to start with this.
And then we need to do slash image plus number and then dot jpg.
So what I'm going to do is I'm going to do file name I'm going to create a variable here.
File name is equal to.
And I'm going to concatenate some strings.
So I'm going to start with folder name.
You see that's quite useful to have created a variable here because I can use it here.
So I don't need to write this a second time.
And then plus and then there's going to be slash.
So I need to add a slash here.
And let's start for example with image one.
Or we could even start with image zero okay dot JPG.
All right.
That's going to be a valid file name for the photo.
But then the problem is we need to change that file name.
So the next time we don't want image zero we want image1 and image2 etc..
So how to change that.
Well I could create a counter actually.
So let's initialize a counter here.
Counter is equal to zero.
Okay.
And how to actually put the value here inside the name.
Well I would need to split this.
So I will add a quote here.
And then plus and then let's say we have the zero and then plus.
Okay.
So this is going to do the same thing.
We have slash image and then the zero and then dot jpg.
But instead of hard coding zero I'm just going to use the counter here.
And because this is an integer we cannot concatenate integer with strings.
So I'm going to need to cast it.
So I'm going to do instead of that I'm going to do str counter.
All right.
And you see now we have created a file name which is slash home slash pi slash camera slash activity
ten.
Then slash image with a number plus an extension.
I'm going to put this file name inside.
So file name inside the camera take photo.
And then very important we need to increment that counter.
So counter plus equal one because the next time we enter the while loop we don't want to use zero.
We want to use one.
All right.
So by doing this we create a different file name every time.
What I will do as well is just use a.
Well it could be anywhere here.
For example print new photo has been taken.
Okay.
Just so that we kind of know what's happening when we run the program, we increment the counter and
then very important we do a time dot sleep.
And I told you it's going to be about well it's going to be five seconds because if.
You don't do a time.sleep here, it's going to take photos as fast as possible.
So you will end up with a lot of files.
Okay.
And that may be a problem.
All right.
And the program here should work.
There's only one thing I'm going to do is because we are using a while loop here.
And when we do control C well, wherever we are here, we're going to exit from the program with a keyboard
interrupt.
So in this case it should be fine.
Okay.
Nothing is going to happen.
But to make the program a bit cleaner I'm going to use also a try except block.
So I'm going to put the while.
So it's quite simple.
You put the try you put this block here.
So I press tab.
You see I just add a tab.
So I just add an indentation for everything and then I go back I do accept keyboard.
interrupt and then I can write pass if I want or I can print something.
Well, it's up to you.
So here we don't really have anything to do.
It's just that we exit in a cleaner way.
All right.
Now we will be able to run that.
So before you run, just also once again make sure you have a time.sleep here in the while loop.
And let's run that.
From what, from Tony or the terminal.
And you can see we have.
So after a few seconds camera has been initialized and new photo has been taken.
Let's go to the camera folder in the activity ten we have image zero and then five seconds later we
have image one.
And then let's wait a bit more.
We have image two.
Let's wait five more seconds.
We have image three, etc. etc..
So it's going to continue to take photos as long as the program is running.
Now we have image four.
I'm going to click here and run Ctrl C and you can see we have a clean exit with end.
Okay.
And I can run it from the terminal as well.
So that was in document Python programs and that was Python three activity ten.
Okay I run it.
And as you can see we have an error here.
So that's the kind of same thing I told you with the Gpio zero is that maybe if you run a script here
first on Sony and then on the terminal, it might not work, especially for stuff that is connected
with hardware.
So in this case, if I close Sony and then I run again, you can see now it is working okay because
somehow Sony was kind of holding resources and now it's working.
I can press Ctrl C and I can start Sony again.
It's not a problem okay.
I can start Sony again and I can run the script again.
It's just that what you can do is for example, you only write the you only write the code here and
then you execute it from the terminal.
But if you execute it from Tony, then you might have an issue in the terminal.
Okay.
But you see here the code is on Tony.
I haven't executed anything here and I can run the code on the terminal.
All right.
And it's going to take a new photo.
So here is going to replace.
It's going to start to replace the image zero and then replace the image one etc..
And maybe add a new file after image five.
All right.
And I'm going to press Ctrl C here.
And that's it for this activity.
Just for completeness I'm going to show you that in the camera module in the Pi zero, there is actually
a functionality that you could use to create a sequence of photos directly.
So here I gave you this challenge to make you actually practice with Python with strings with loops
okay.
So that's a very good exercise, but I'm just going to write it here.
I'm not going to run it.
But for example you could do camera and then dot capture sequence like that and provide the path.
So that's going to be.
So here I'm going to write the full path for example home by camera activity ten slash image dot jpg.
So you don't provide any number here.
And then you will have num images.
So number of images you want for example ten and then interval for example five sequence okay.
If you run that it's going to take ten images separated by five second interval.
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
etc. it's going to do that automatically for you okay.
So if you know already how many images you want to take, that can be a good thing to do.
But then there are two problems with this function is that, first of all, you need to know how many
images you want.
Here we can just take as many as we want.
It could be one, it could be five, it could be ten.
As long as we run the program.
Okay.
So here you need to know how many images you want in advance.
And then if you run that well it's going to block the program.
And that's the only thing you can do okay.
With a loop like this you can take a photo, but after you take a photo you can do something else.
For example, you could do something with the push button with the LEDs, with the Pearson.
So you could send an email okay.
You could continue to do stuff on the side.
It's not only just taking a sequence of photos, okay.
You can do something else on the side.
All right.
So you have just seen this.
I'm going to comment it and leave it in the solution.
Just so you know that there is also a function like that.
Great.
And that's the end of this activity.
And that's also the end of this section on the Raspberry Pi camera.