How to Capture Images and Videos With Computer Webcam Using OpenCV in Python ? | Part 1
In the previous post we learnt how to read images using OpenCV in Python Programming Language. Moving on further in the same direction in this post we will understand how to capture videos with computer webcam using OpenCV. Before starting with how to capture a video let us first get a clear idea of what a video actually is ?
A video is actually a collection of multiple images or multiple frames displayed simultaneously one after the other such that, it appears like a video. Having got a clear idea of what a video actually is. What we will do is we will use the OpenCV-Python library to read those frames/images one by one simultaneously.
Now, consider that the video is say comprised of ten frames, it means that we will have to read those ten frames consecutively one after the other or in other words, we will have to repeatedly read the frames till the last frame in the collection.
Now, recall in any programming language if we want to do a task or a collection of tasks repeatedly, which programming construct do we use? We make use of the loops.
So here, in order to capture video, that is to capture frames one after the other we will make use of loops to build a window where the images will appear one after the other simultaneously, so that you can see it as a video.
I guess, now the concept of videos is clear with every one and also you might have got some idea about the utility of loops in capturing a video. With the basics having been covered it's time to see Python Programming in action and read the video.
First and foremost we will have to import the OpenCv module into our program.
import cv2
Done with that, recall that we used a method from the OpenCV module to create an image object, the imread() method. The imread() method accepted two arguments, the path to the image and, a flag value that decided whether the image is to be read as a colored or a grayscale image. Refer the previous post.
Similarly, there is a method in OpenCV that triggers the video camera and returns a video object (the object in which the video is captured) the VideoCapture() method.
The VideoCapture() method in OpenCV will create a video-capture object and trigger the camera so as to record the video. The VideoCapture() methods accepts two types of parameters
- Numeric values 0,1,2 or
- Path to the video file.
import cv2
video=cv2.VideoCapture(0)
#the above line will trigger the webcam for recording the video
video=cv2.VideoCapture("C:/Users/Default/Desktop/video.mp4")
#the above line will read the video file present at the mentioned path
Post this step, we will have to release the camera once we have successfully captured the video, for this we will make use of the release() method
import cv2
video=cv2.VideoCapture(0)
video.release()
we have an instance of the webcam with us which will be active for capturing the video but, by default the webcam will only be active for split seconds in that way we will not be able to capture the video accurately. Now, what if we want that Webcam to active for some more time, we will have to introduce a delay for that we will have to import the time module and then we will invoke the sleep() methods which accepts a numeric value as parameter and will cause the webcam to be active for the duration mentioned in the parameter.
import cv2
import time
video=cv2.VideoCapture(0)
time.sleep(5)
video.release()
We have imported the time module and invoked the sleep() method prior to to releasing the camera this will cause the webcam to be active for 5 seconds. I hope this is clear. Now, we already know that computer reads images in the form of matrices. As video is also a collection of number of images, video should also get stored as a matrix only. Let's add a few lines in our code to see if it is actually so. Well, we can check this with the help of the read() method of the Video object.
The read() methods returns to variables -
- check - A boolean value, if python is able to read the video object it returns true otherwise, it returns false.
- frame - This is the variable in which the first frame that got captured when the camera was active gets stored.
import cv2
import time
video=cv2.VideoCapture(0)
check,frame=video.read()
print(check)
print(frame)
time.sleep(5)
video.release()
Printing check will show either True or False depending on whether python is able to read the video object. Printing frame will show the 3D matrix of the first frame that was captured in the duration when the webcam was active. The output of this block of code is shown below
True
[[[ 78 67 103]
[ 78 67 103]
[ 85 77 102]
...
[210 157 180]
[200 158 171]
[200 158 171]]
[[ 78 70 95]
[ 77 68 94]
[ 84 78 104]
...
[208 158 174]
[205 157 170]
[205 157 170]]
[[ 76 71 90]
[ 77 73 91]
[ 88 83 106]
...
[210 159 171]
[212 163 169]
[213 164 171]]
...
[[ 75 69 92]
[ 73 68 91]
[ 72 67 90]
...
[ 56 53 65]
[ 54 51 61]
[ 54 51 61]]
[[ 64 70 92]
[ 62 68 90]
[ 64 67 92]
...
[ 56 51 63]
[ 58 52 59]
[ 58 52 59]]
[[ 55 69 88]
[ 55 69 88]
[ 62 68 90]
...
[ 50 48 55]
[ 54 51 58]
[ 54 51 58]]]
As you can see check returns true which means that python is able to read the video object also the 3D-matrix visible here is the matrix corresponding to the first frame that was captured in the 5 seconds duration when the webcam was active. So from here it is clear that just like images, videos are also read and stored as matrices.
Next, we will create a window to display the frame being captured by the webcam, we saw in the last post that we can make use of the imshow() method to create a window. We will use the same method here to display the frame. Similarly we will make use of the waitKey() method and the destroyAllWindows() method as well to provide the delay for which to keep the window alive and destroying the window afterwards respectively for a detailed explaination of the waitKey() and the destroyAllWindows() method please read the previous post.
import cv2,time
video=cv2.VideoCapture(0)
check,frame=video.read()
time.sleep(5)
cv2.imshow("Captured",frame)
cv2.waitKey(0)
video.release()
cv2.destroyAllWindows()
Now that we have learnt to capture the first frame we have to understand how to capture a video instead. As mentioned earlier we will make use of loops to read consecutive frames we will continue with that in part 2 of this post. Till then, Happy Coding.
This comment has been removed by the author.
ReplyDelete