This is now step two for this final project.
And in this step we are going to focus on the camera.
So we're going to set up the camera and take a photo when the movement has been detected here.
So we already have the print.
So after this print in this if we're just going to take a photo because we know this logic is already
working correctly.
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.
Okay.
And I'm going to start from this.
So we already have here I've already organized it with the global variables.
And then we set up the gpios I'm going to put s here.
And we are also going to set up the camera.
So for example we can do it here.
Let's do setup camera.
You could do it before or after the Gpio.
It doesn't really matter.
And let's do this.
So let's import from PyCharm zero import camera.
And then we can do here.
Camera.
Let's create a camera.
Variable is equal to camera.
Then I'm going to give the settings camera dot.
Still size is equal to and tuple with 1036 and 864.
So this will depend on the version you have.
Okay so that's for the version three of the camera.
But if you have another one maybe you will need another resolution.
So you can go back to the section on cameras for more information.
And I'm also going to do camera dot flip camera because my camera is upside down with v flip through
and H flip.
So vertical flip and horizontal flip.
And then just in case I'm going to do time.
So I already imported time here.
So I will do time dot sleep two seconds.
Okay.
Make sure that everything is correctly set up and then print camera setup.
Okay.
So you can see I do camera setup.
Okay.
And then Gpio setup okay.
So then when I start the Python program I can see the camera.
Okay.
Maybe if I get an error after this means that the error is on the Gpio.
Okay.
So I can see better if the initialization is correct and if not where the error is.
Okay.
So we have our camera which is ready to take some pictures.
But now what I'm going to do is I'm going to create a folder.
Okay.
We're going to save those pictures inside a folder.
I'm going to add a new variable here.
I'm going to name camera.
Camera.
Folder.
Path.
And once again I use all uppercase because this is not something I want to modify later.
It's just something I create here.
Okay.
And we just use it as it is.
So by using uppercase I kind of make it a constant variable.
It's not a constant variable.
Technically you could modify it, but at least I know that I should try not to modify it later on.
Okay, so it's a good way to not mess your program later on.
So Camera Folder path is going to be home slash pie.
Let's use our home directory.
Okay.
And let's create a folder named Photos Final Project.
You can create any folder you want.
It doesn't really matter here.
It's the one I have chosen.
And I'm going to create this folder just well just when I initialize the camera, okay.
At the same time.
And to do this I will first need to check if this folder doesn't exist.
Then I'm going to create it.
Okay.
So I will need to import.
OS.
And with the OS module I can do if not OS dot path dot exists and with the camera folder path, then
I do OS dot mkdir to create the folder with camera folder path.
So I check if it doesn't exist.
Then I create it as simple as that.
And that's going to be it for the initialization of the camera.
I can already test that.
So let's do Ctrl s and let's go back here.
I'm going to check also with my file manager in my home directory.
You see I don't have any folder named what was the name photos final project.
So I don't have any folder here.
I'm going to start this actually step one.
So make sure that also when you save as a new file you also run the new one.
So python3 project step two and you see camera setup okay Gpio setup.
Okay I'm going to stop.
And you see now we should have this photos final project.
So we have the folder.
Great.
Now we can save inside this folder.
So I'm going to go back here.
And here what I print I'm going to leave this print okay.
Taking a photo and sending it by email.
And I'm gonna take a photo here.
But instead of taking a photo right here, what I can do is create a function, okay.
And I'm going to create a function here at the beginning of the program def take photo.
And what arguments do we need here.
So to take a photo we will create a file name.
And to create the file name we will need to have the well the base the folder path here.
So we're going to start with this and then add something for the file name.
And what we've done before is that for example you create this variable.
Then you create the function afterwards.
And you use the global variable inside the function.
But what I could also do is to pass the folder path directly inside the function as an argument, so
that I can put this function wherever I can put it at the top of the program.
Okay, it's going to be more modular then I don't need to create a global variable before that.
I can pass it as a parameter.
And the same thing for the camera here, I can just pass the camera as an argument.
So let me show you how to do this.
You will see it quite easy.
So I do camera and then folder path okay.
So that's my function.
And then to call this function I can do here take photo okay.
That's the function I'm calling.
And I need to send it the camera and the folder path.
So the camera is this one okay.
The camera variable and the camera folder path is that one.
So I go back here and I do camera and camera.
folder path.
Let's remove those spaces here.
Okay.
So you can see that we have created our variables here and all the stuff.
So here we still use global variables inside those two functions.
But for the take photo function I put it on the side.
So at the top.
And I just get the camera here as an argument.
Okay.
It's going to work the same.
But this way we can put all our other functions at the top.
And we don't need to worry about having a global variable before that, etc..
So I will do camera dot take photo and I need to create a file name.
So let's create a file name.
That's going to be what that's going to be the folder path here.
Which is whatever we pass to this function.
So in this case it's going to be that one because that's the one we pass Yeah, but you could also you
see you could also use this function with another path name.
Okay.
So the function is quite modular.
What do we do then.
I'm going to add a slash because here I didn't provide a slash.
And I'm going to name it image underscore.
And also I'm going to add the timestamp so the current time to the image.
So an easy way to create an image that has a unique name every time is just to add the current time
to it, because the current time is always going to increase.
Okay.
So you can see time to time is going to give us something like that.
So that's what we're going to write inside the file name.
So plus time dot time.
And note that this is going to be a number.
So to cast it as a string we do str okay.
Because you cannot concatenate numbers and strings you need to concatenate strings with strings.
Great.
So the image is going to be img.
Underscore The time stamp and we need to do plus dot jpg for the extension.
Then we do.
Camera take photo with file name.
All right.
That's going to take the photo in this folder with this name.
And finally I'm just going to do return file name because that's something we're going to use later.
So here actually take photo is going to return.
So I can save it here as photo file name is equal to take photo.
And I'm going to use that in the next step.
All right.
And that should be it for this step.
So before we test the program let's just do a quick recap here.
I have added this block of code to set up the camera and to create a folder to save the photos.
Then I have also added a function here that takes as an argument the camera and the folder path, so
that it will save a photo using the timestamp here in this folder path and also returns the file name.
And where do we call this?
Well, we call this from here this motion finished function with the logic that we have created in the
last step.
When we are sure that we have spent at least five seconds doing a movement, and that the last photo
was taken at least 30s ago.
Then we can take a new one.
So let's say I'm going to open the terminal.
I'm going to run the program.
So step number two.
Okay.
Let's run.
All right.
Camera setup okay Gpio okay.
And all right let's do some movement.
So it's two seconds but let's do five second one.
Okay.
So seven seconds something taking a photo.
So now let's go back here.
Let's see photos final project.
And we have a new photo okay.
You see the timestamp here.
So we have image and the timestamp stamp, then dot jpg.
So this way we are sure that we have a new file every time.
And the photo.
Okay, well you can see it's quite blurred because I was doing movement just in front of the camera.
But we have the latest photo.
So here I don't really care about the quality of the of the photo.
It's going to be blur every time.
But of course, if you want it to do a real application to use in real life, you would need to find
a nice spot for the camera.
Okay?
And fix the camera somewhere.
Make sure that the quality of the image is good.
Okay, but here we're just going to keep it like this with this small camera cable.
That's going to be enough for this project.
Great.
And if I go back here and what you see we have lots of timers.
I'm going to do another one here.
So five seconds at least okay.
Taking a photo.
So that was at least 30s okay.
Since the last one.
And you see now I have a second photo okay.
So I'm going to have a new photo every time.
Great.
And one thing I could do, because now you see, I have every time this starting timer plus the time,
etc. so what I could do is remove.
I'm not going to remove those, I'm going to comment those.
Okay.
So I'm going to comment the print start timer and the print motion duration here because we know it's
currently working.
I could remove them altogether, but I'm just going to put them as a comment in case you want to test
them again later.
Okay, so I save and now instead of all that if I start again.
Okay.
So you will still have the LED that's powered on.
And then if I do movement for five seconds.
So I'm just going to do movement for five seconds in front of the pier.
Okay.
You see taking a photo and sending it by email.
So we only have this log right.
So having logs like that is good as an intermediate steps.
And then when you no longer need them you can remove them or comment them.
And well that's going to be it for this.
Step number two.