In this blog, we will learn how we can Read, Display, and Save Video with OpenCV using the Video Capture in Python OpenCV Function.
Previously we were working with OpenCV images, now we will be working with videos.
Learn more about Python OpenCV from here.
What is a Video?
Video is just a collection of images that are rolled very fast (30 fps or 60 fps)on the screen and we see a moving picture. Usually, the frames per second are the smoothness of the video.
How to Capture Video in OpenCV Python?
We will answer this question in this blog. We have a webcam and we want to access it to record the video or just read a pre-recorded video in our python program. But how to do it?
We can make use of Python OpenCV’s cv2.VideoCapture()
function to read the video frames directly from the webcam and also to read a pre-recorded video.
Let’s see it in action!
Creating Video Capture Instance
The First step to do anything regarding video is to make the instance of the VideoCapture class present in the OpenCV package.
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
The first two lines are the import of NumPy and cv2.
The instance is stored in the cap variable, which will be used for later purposes. The object creation of VideoCapture takes an argument that is the index of the camera that will be considered for the recording.
The default parameter is 0, which takes the default camera into consideration.
If you have more than one camera and you want to use a specific camera provide your camera number as 1, 2, or 3 (The order in which the camera is inserted into the computer).
Check if the camera is Opened
This process is done for error-handling purposes. It will be not good if the camera is not opened and we start the process of recording. It can also damage the camera.
To avoid this, we check if the camera was opened when we created the object of VideoCapture.
print(cap.isOpened())
This will print True if the camera was opened, and False if the camera was not opened.
Open Camera in OpenCV Python
In the above section, we checked if the camera is opened or not. What if the camera is not opened? How we can open the camera using OpenCV.
We can open the camera using the open()
function.
cap.open()
To confirm if the camera is opened or not, you can repeat the above process in order to check it.
Reading Frames from Video in OpenCV
We have learned the nitty-gritty basics of capturing video using OpenCV, this will help us in reading frames of the video.
Frames of a video can be read using the read() function in OpenCV. The read() function returns a tuple with two variables i.e. retention and the frame. The retention value is a boolean value that lets us know whether the read()
function was able to get the frame or not and the frame is the actual frame of the video.
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't retrive frame")
break
In the next blog, we will see how we can write the height and width of the video frame on each frame of the video.
Learn more about video capture from the OpenCV official documentation.