Hey welcome to this new lecture and in this lecture we will be using open CV to
capture video from the computer webcam. So you'll need OpenCV and you will
import it as cv2. As you already know OpenCV is what we use for image
processing, and of course video processing because video is made of
images, so we have also of images and they are referred to as frames and these
frames they, these images are showing one after the other in a high speed and then
we can see those images as a video. And you'll either hate this lecture
or love it. What we'll be doing is that we'll go in
a really low level of image processing here, of video processing. We will be reading
frames one by one, so images one by one and you'll see that how we use loops
in Python to show those images and to actually build a window where images are
showing fast and you can see it as a video, so if you have a laptop you
probably have a built in camera. If you have a desktop without a camera you'll
need an external camera to follow me in this lecture. And now the first thing
you may want to do is to read a video and then once you read the video, so you
go read a video either from the webcam or from a video file, and once you read
the video then you can apply other operations such as graying out the video,
or adding texture to the video, or detecting objects in the video etc.
So the first thing you want to do is to create an object, let's say video and
that would be equal to cv2.VideoCapture. So that's the method that
triggers a video capture object and that gets an argument here which can be
either a number such as 0, 1, 2, 3, or the video file path, so when you put a number
that implies that you're capturing a video from the camera. So let's say I have
a built in camera in my computer, but also I have an external camera. In that case one
of my cameras will have an index of 0 and the other camera will have an index of 1,
and if I have a third camera it will have an index of 2 and so on. So that's it.
If you had a video file you could just say movie.mp4 or something like that.
It depends on your video file name, and you'd have to have this file
somewhere in your computer, but in this lecture we are talking about capturing
video from the webcam so I have only one webcam in my computer so I'll pass 0 here.
That's it. Once you do that you want to make sure that you will release
the camera, so you want to access your object video dot release is the method, and then
you don't need to pass any argument there. So let's see what we have if we
execute this Python file, so Python capture.py is
the name of my file. And obviously nothing happens, but on my
side I notice that my camera was turned on for a second or for a fraction
of a second, so the light of the camera was turned on and then it was
closed, it was turned off immediately. So what happens is that we actually triggered
the camera here, the webcam but then we immediately release the camera.
So what Python will do is it will open the camera and in a millisecond or so it
will release the camera. Let me try it again.
Sometimes it might happen that you don't even see a light at all, so you don't see
it because the process happens very fast. So probably your computer needs some
seconds to actually turn on the light of your camera, and we can give it some
seconds so. To do that you want to import the built-in time library. The time
library provides some operations related to time.
For example in our case we will use it to hold the script for three seconds.
So what this will do is that Python will try to execute the script line by line, so it
will import cv2 and time, and then it will trigger the camera, and then before
releasing the camera we will have the script wait for three seconds. So this
will be executed, wait for three seconds and then execute the other method. That's it.
Now let me execute this again. Okay. The light is on one two three off, yep.
And the video is released. Now maybe you're thinking why don't we see your
face there showing in the video camera, in the window? Well the reason is
that do you see any line in here that I'm showing a window? No. So why do you expect
my face to show there? And the way we do that is by first creating a frame object
which will read the images of this video capture object, and let me do that first
before going into more explanations. So let me say check frame. That will be
equal to video dot read. Just like that. So what we are reading here is we have a
boolean data type here and a Numpy array, so let me print this out so that
you understand better, so check first and then print frame. Okay, execute the script.
Wait for 3 seconds and close it. So what we got here is, I'll expand this.
So we got true4.check variable and we got the Numpy array which represent
the image. This image is actually the first image that the video captures.
This here, the boolean we can use it for various operations such as checking
whether the video is running or not. So you may have to check that while doing
your programs, and the frame is the most important one because we will use these
frame objects, so we'll use this Numpy array which is a three-dimensional
array because it's a color image, so it has three bands, and you know this from
a previous lecture where we did image processing. Okay, we will loop
through this frame and show it using the image show method, so imshow,
image show method of the cv2 library. So we will recursively show each frame of
video being captured, so I hope that makes sense.
So before we do that recursively I would like to show a window down here so
cv2.imshow, and let's put a name for the window capturing, pass the frame object
in there, and also remember that when you pass an image show method, so when
you're showing a window you want to make sure that the window is closed and you
do that using the cv2.waitKey method and you can pass zero to allow
you to press any button and close that window, and what you want to do is cv2
destroy all windows, so when this button is pressed you destroy all
windows. I actually want to put this up here so the video is not released before
you press this key. So what happens is that Python executes these lines and then
it waits for you to press a key, and then it releases the video, and then it
it destroys the actual window that you're seeing on the screen. Okay, let's
see what will happen now. Surprise I've got only two teeth, you do know that. Okay
I'm joking, I have more but so what Python had did here is that it triggered
the camera so it triggered the camera here and then it read the first frame of
the video, so the very first frame as soon as the camera triggers it reads the
first frame. Then we printed out that frame and it's here somewhere in the
command line. Then the script slept for three seconds, so it didn't do anything.
It waited for three seconds and then cv2 with the imshow method it created
a window where it showed the first frame of the video, so the picture that you
just saw is the first frame of the video. And then, the actual frame, the window is
still open so it's waiting for a key for us to press, so if I press a key now
this last two lines will be executed. So that means that the video will be
released and this window will destroy, so press key, and yep, that
happened. So python is processing the video frame just as simple images. That
means you can also apply methods such as cv2.cvtColor and you can pass
that frame in here, and then cv2COLOR BGR2GRAY, save it again in grey variable.
So what I'm doing is that I am converting the frame, so the color frame
into a gray image, and actually I have to do this up here, so and then I can show
the grayscale version of the image here. Okay, let me try it again.
So that's the grayscale version but it's showing using imshow method, so that the
idea. Now how about showing an actual video, not a single image there.
Well the answer is that we need to use a while loop here and the reason we need to
do that is that a while loop what it does is that it executes some statements
infinitely until we break the loop at a certain point using statements such as
break for example so the idea is that we need to put all this code up to here so
we need to put it in a while loop. I'll do just that, so you can either use a shortcut
from your keyboard depending on your keyboard, or go to
edit lines and then indent to indent these lines.
So here is a shortcut, but anyway then you want to write while
True and then everything goes inside the while loop, as easy as that. But we
haven't finished yet. We have some more things to do here, but first of all I
would like to actually make sure you understand the while loop, so let me
trigger a platform console here quickly. So what one loop does is that let's say
while True print let's say 1. If you execute that now, 1 will be printed
infinitely, so my concert is getting very busy here and I can interrupt this using
a keyboard interruption operation so ctrl C or control Z if that doesn't work.
So the while loop is, while True which will be always true
execute this lines, but if I try to run this now what will happen is that
Python will trigger the capture, video capture and it will show the
window, but it's just one window only so just an image, is the first image of
the video because what what's happening is that the while loop starts, but it
goes, it creates the first frame of the video, so the current frame which is
the very first frame when the video starts, and then it converts the frame to grayscale
image, and then it sleeps for 3 seconds, ok. Then it shows the image, and then what
the while loop does here it waits for you to press a key, and then if you press
a key the process stops. So what you can do in this case is to enter another, let
me make some space there, so you need to alter these values of the arguments. When we
enter 0 it means that any key that you press on the keyboard it will close the
the window but if you enter let's say 2000, that means the script will wait for
2000 milliseconds which is 2 seconds and let's see what will happen here, so it
shows my face first and then it waits for 3 seconds here, then it waits for
another 2 seconds and then it shows the other frame so it's very slow, just like
that. Ok, now if you press any key nothing will happen because you removed
the zero from here. So in that case I'll forcely stop
the script here. Control C. Ok, we don't need this time sleep anymore and we also
need a conditional here, and before doing that we will store this action in a key
variable, and then we say if key is equal to ord method, and then you pass a key
from the keyboard so if this equals to q, then break the while loop and let's put
it 1000 this time. Okay, so now what this will do is that it will capture the
video, it will start the video and then it will iterate through all these lines
so it will create a frame converted to gray, show the frame here, and then it will
wait for one second and if you press the q key from keyboard it will break the
while loop, and then if you if it breaks the video dot release it will be
executed so the video will stop and the window will be destroyed.
But if you don't press any key this will wait for one second, and then it will
show the next frame, and then after one second it will show the next frames,
and so on. So save the script, execute. So the video is improving.
Yeah, looks good now, okay. If you press q, it will stop it.
Okay, let's improve it more. Why not for one millisecond? Hey, hi you're doing?
So it's working good, and you can also see here that the check booleon, the Numpy
array is being printed at every iteration, and we can stop this now we if
we press q. Just like that. Now if you want to know how many frames
are being generated there, there is a nice trick we could do. We could create
a variable, let's say a equals to 1 outside the while loop and then here you say
a equals to a plus 1, and then after the loop breaks here you want to print a.
So what this will do is when you run the script, it gets a value of 1 and then
when the while loop starts running, so it could go through the first iteration and
it will increase the a value by 1 so a equals to 1 plus 1 so it will get 2, so it
will be updated to 2, and then it will go up to the end, and then it will start up
again, so this line will be a again equals to 2 plus 1 so a will be 3 and
the second iteration, and so on. Or you can put this to 1 actually, so if we have
2 iterations that means a will be 2 and if we have 3 it will be 3 and so on.
And then we print a here outside the loop. Ok, let's see what we have. I'll keep the
video running for 3 seconds and we will see how many iterations we will have.
So capture one, two, three and stop. So the last value that was printed out
here is a value. That means we had 51 iterations in about three seconds which
is let's say around 20 frames per second. So our camera, my camera is capturing 20
frames per second, and let me execute this one more time, the last time, so if
I put my finger here in camera so I blacked out the image and you'll see
that the Numpy array a is now 000 because the frames of the image all
it has is zero values, so there's zero intensity in the image. Okay that's
what I wanted to show you in this lecture, so as I said once you have these
frames you can apply operations to them, so you can draw rectangles there,
detect images and so on. And we will do that, so we will build our real world
application by implementing some motion detection in our frames and then we will
capture the times that an object entered our frame, our video, and we'll have a list
of those times. So I hope you loved this lecture, not hated it,
and I'll see you in the next one.