Hi.
Welcome back.
In this video, we will start coding our email webcam detection app.
I demonstrated you the app in the previous video.
So let's begin.
Creates a patch project and we will basically need to python files here.
Why we need to.
Let's call this emailing and main.
Please don't name it email because that may clash with a python library called email.
So use another name.
We need to python files because think about the program we want to build.
It has two parts.
It has the webcam part and it has the emailing part.
So the webcam is the front end part and the emailing part is the back end.
Therefore, in main that spy, I'll write the script which stars the computer webcam.
That script in Maine that we will also detect movement in the webcam and it will capture that as an
image.
Then this mean that's pure script will tell the emailing script to send an email using that image as
an attachment in the email.
And that's all.
So let's begin with the frontend.
The first thing I want to show you how to do is how to start the webcam of your computer.
So for this, obviously you need a webcam in your laptop or desktop computer.
So you either use the integrated computer of your laptop or use another USB camera or something similar.
There are also apps which connect your mobile camera to be used by your computer.
So you'll have to do some research on the Internet.
If that's the case, if you don't have a camera on your computer.
Right.
So considering you have a webcam, then what you want to do is import TV to CV to is actually called
open CV, So you shouldn't be installing that using this button, but go to the terminal instead and
make sure that the vent is activated and the PIP install open CV dash python.
So OpenCV minus Python that will install open CV for you and you import it with CV to.
Then we want to start the video, to start a video from the webcam using the webcam.
You write a variable and you say video is equal to CV to that video capture.
So this class.
And inside the parentheses you have to place a number, either zero or one or two.
So if you have only one camera in your computer, which could be the case if you have a laptop, for
example, you have integrated camera, then you should play zero to use that main camera.
But if you want to use your secondary camera, you should use one.
So perhaps you have a USB camera attached to your laptop and you want to use that USB camera.
Please use one here.
Otherwise use zero to use the main camera.
You can try either way and see what works for you.
And then write two variables.
Check and frame equal to video.
Video is this variable that read in parentheses.
So that's a method.
And then let's for now, let's print, check, and let's print the frame and see what these will be
run.
And if you notice it, what happened is that my computer camera, my webcam lights turned on.
So the camera started and then it turned off.
And I got through as part of the check variable and.
This matrix is part of the frame variable.
So this is a matrix.
It's a non PI matrix.
So CV two collaborates with a non PI library.
The non PI library provides these matrix matrices as it's specialized object type.
So we have lists which come directly from Python and we have matrices which come from non pi.
So basically these are lists of lists of lists.
And this thing here, which happens to be zeros, I I'll explain why.
This thing here is.
Representing the frame, the image that the camera captured.
So it's only one image, actually.
So the frame is the image and the image is represented as numbers.
Why is it zero?
Well, because my camera didn't have time to actually.
Do something.
So if you import time here and then you say after this video capture, after actually after video reads,
because this simply sets up sets up the camera and this actually reads the first frame of the video,
because a video What is a video?
A video is just a series of images.
When you when you hear, for example, 30 frames per second, that means the video has 30 frames per
second and.
These video players.
What they are doing is they are simply displaying these 30 images.
In one second and you see it, you perceive that as video.
So time does sleep.
One second here and then.
I'll get another check to frame too.
So then I'll check one, frame one.
We sleep for one second.
Another sleep here.
Sleep for another second and a third.
Check three and frame three here.
Then let's print out.
Frame three, for example.
Execute.
No, the camera should be staying on a bit longer.
And so what I've got here is the third frame.
All right.
Now we have some numbers here.
These actually, all these rows represent one single pixel in the image.
And each of the values represent the blue, the amount of blue, the amount of green, and the amount
of red.
So the combination of these three basic colors will construct the color for that pixel.
And then we have the other pixel again, BGR.
So blue, green, reds.
The same for the other pixel and so on.
So that's construct an entire.
Image an entire matrix.
Whatever you like to call it.
So this is quite long.
But you see, these dots means these will continue.
There will be many, many pixels, but they are simply not displayed here in the command line because
it's too many data, too many pixels.
All right.
So you are wondering.
All right, we have frames as numbers, but how do we see?
The frames.
Can you show the frames on the screen?
Yeah, you can do that using the I am show methods, but I am show should be used inside a wide loop.
So let's create a while.
True loop and video capture should be outside of the while loop.
But.
Reading the frames should be inside the wild loop.
So previously we were applying the read method multiple times.
But now, since we are using a wild loop, that means we don't need to manually add the read method
multiple times.
The while loop will execute, read multiple times.
And I would like to remove this once from the variable names.
So basically in each iteration we will have a frame.
So that will happen very fast.
And then you say CV to that I am show you give a name to this, let's say.
My video and then provide the frame variable.
This contains those matrices.
So the images.
So you want to display the images every fraction of a second or so.
Remove this frame three as well.
It's a good idea that before you get the first frame, you use time to sleep for one second, perhaps
because that will avoid these black frames.
So you give the camera some time to load.
Then you want to create a key variable which should be equal to CV to that weight.
Key one So CV two dot weight.
Key one as arguments.
And then you say if key.
Is equal to roads.
Q I'll explain you what this is for now.
Please follow me here as I'm typing.
Break and then go outside of the wild loop.
So in this level of indentation and you say video does release.
So you point to that video variable and you release the video.
Let's see now how this will work.
So let's execute.
Once you run the script, this should pop up a program.
So check among your programs.
You should see something with this icon.
This.
Python rockets.
So open that and you should see yourself there.
Now, the video is a bit slow, and that is because in the script I plays the time that sleep methods
in here.
So that means every loop will be one second apart from each other.
That means the frames are being shown.
With a lag of one second, basically.
So in other words, we are creating one frame per second here.
If you use that.
And of course, if you print out the frame here somewhere inside the loop, whenever you wherever you
like, like print frame.
That variable, you'll see that the numpy array, the matrix will be printed out every 1 seconds here.
So to fix this, you want to place that time, slip outside the loop.
So once you start the camera, you wait one second and then you capture the frames.
So you wait one second for the camera to load.
Then stop the script.
Wrong again.
And look for that program again so that it is so now the video is smooth and we can go back to the script
and I'll explain you what this does.
So these lines, well, this creates a keyboard key object, basically.
And then you say that if the user presses the Q key, we will break the.
Video.
So let's try this.
Go to The View, Focus with your cursor.
Make sure the focus is in the app and not in another app on your computer and press Q.
That's who will stop the program.
So it will release the video and yeah, the camera stops.
And to know if you want to process those frames, you should do stuff in here.
You should code lots of things in here if you want to save them and so on.
That is something we are going to do in the next videos.
So for now, that was how to start the computer webcam.
Thanks for following and I'll see you later.