OpenCV cv2.VideoCapture() Function

Learn via video courses
Topics Covered

Overview

A video is nothing more than a collection of images known as frames. Hence, it would help if you looped through all the frames in a video sequence and then processed them one at a time. In this article, we'll go into detail about how to read, show, and write videos from a file, an image sequence, and a webcam.

The cv2.VideoCapture() function can be used in Python with the OpenCV library to record video by creating a VideoCapture object. The input to the function can be a device index or the name of a video file, and frames can be captured by passing 0 or 1 as the input to select a camera.

Syntax of OpenCV cv2.VideoCapture() Method

Python cv2.VideoCatpture() is a function in the python-opencv module that captures video from a camera or a file. It only accepts one argument: the index of the camera to be utilized or the name of the video file to be opened.

Parameters of OpenCV cv2.VideoCapture() Method

The cv2.VideoCapture() method in OpenCV requires relevant parameters to work properly. Here are the important parameters needed:

ParametersFunction
filenameThe filename of the video to be read. The filename can take one of the following forms:1.The name of the video file in the current directory, the full path to the video file if it is not in the current working directory, name of the image sequence that will be iterated over.2.The video stream's URL.
apiPreferenceThe primary API backend for capturing or reading video files. It is a purely optional parameter. If various reader implementations are available, this can be used to enforce a specific reader implementation.

Return Value of OpenCV cv2.VideoCapture() Method

  • VideoCapture object - The cv2.VideoCapture() method returns a VideoCapture object, which represents the input video stream. This object can be used to read frames from the video input source, set properties of the video stream, and release the resources used by the stream.
  • Boolean value - When the VideoCapture.read() method is called, it returns a tuple consisting of a Boolean value and the actual image data as a NumPy array. The Boolean value indicates whether the read operation was successful or not. If the read operation was successful, the Boolean value is True, and if it failed, the Boolean value is False.
  • Numeric value - When the VideoCapture.get() method is called with a specific property identifier, it returns a numeric value representing the value of that property for the input video stream. For example, calling VideoCapture.get(cv2.CAP_PROP_FRAME_WIDTH) returns the width of the frames in the video stream.

video capture

Exceptions of OpenCV cv2.VideoCapture() Method

  • cv2.error - This exception is raised when there is an error reading from the video input source. cv2.error can happen if the video file does not exist or there is a camera problem.
  • AttributeError - This exception is raised when an invalid property identifier is passed to the VideoCapture.set() method. Ensure you are using the correct property identifier constants provided by OpenCV.
  • TypeError - This exception is raised when the input argument to the cv2.VideoCapture() method is of an incorrect type or format. Make sure you pass a valid integer or string argument to the method, depending on whether you want to read from a camera or a video file.

How does the OpenCV cv2.VideoCapture() Method Work?

The cv2.VideoCapture() method is a function the OpenCV library provides for reading video input from a camera or a file. It is used to capture frames from a video stream, process them, and display or save them as required.

To use cv2.VideoCapture(), you first create an instance of the cv2.VideoCapture class and pass it either the device index of the camera to be used (e.g., 0 for the default camera) or the name of the video file to be opened—the cv2.VideoCapture() function returns a VideoCapture object that provides access to the video stream.

Once the VideoCapture object has been created, you can use the read() method to read frames from the video stream. The read() method returns two values: a Boolean value indicating whether the read operation was successful and the actual image data in the form of a NumPy array. You can use this image data to perform various image processing tasks, such as filtering, edge detection, and object detection.

If you are reading frames from a video file, you can use the opened() method to check whether the file was opened successfully or not. Once you have finished processing the frames, you can release the resources used by the VideoCapture object using the release() method.

In summary, the cv2.VideoCapture() method is a useful function for reading video input from cameras or files. It provides a simple and convenient way to process video data in Python using the OpenCV library.

Examples

Here are some examples of using the cv2.VideoCapture() class in OpenCV,

1. Reading a video file -

Use the cv2.VideoCapture() method to create a VideoCapture object for reading from a video file. Then, use the VideoCapture.read() method in a loop to read each frame of the video as a NumPy array. You can manipulate the frames as needed and display them on the screen using OpenCV's cv2.imshow() function.

2. Reading from a camera -

To read from a camera, create a VideoCapture object with the argument 0 or -1 to use the default camera. Then, use the VideoCapture.read() method in a loop to read each frame from the camera as a NumPy array. You can manipulate the frames as needed and display them on the screen using OpenCV's cv2.imshow() function.

3. Checking if the video opened successfully -

Use the VideoCapture.isOpened() method to check if a video file or camera was opened successfully. If the method returns False, it means there was an error opening the video input source.

4. Setting properties of a VideoCapture object-

Use the VideoCapture.set() method to set the properties of a VideoCapture object. For example, you can set the frame size or the frame rate of the video using the cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, and cv2.CAP_PROP_FPS properties.

5. Saving a video -

Use the cv2.VideoWriter() method to create a VideoWriter object for writing a video file. Then, use a loop to read each frame from the VideoCapture object and write it to the VideoWriter object using the VideoWriter.write() method. Finally, release the resources using the VideoCapture.release() and VideoWriter.release() methods.

Example 1: Capturing Videos through a Webcam

To capture videos through a webcam in OpenCV, you can use the cv2.VideoCapture() method.

Step-by-step implementation of capturing videos from a webcam:

We begin by importing numpy and cv2, nothing special there.

This creates a new VideoCapture object, which is used to capture video frames from a camera or a video file. In this case, the 0 parameter specifies that the first available camera should be used for capturing video.

So, I just passed 0. (or -1). You can choose the second camera by moving on to 1 and beyond. After that, you can take individual frames of video. Remember to release the capture, though, at the end.

This creates a four-character code (fourcc) representing the video codec to be used for the output video file. In this case, the codec is XVID, which is a popular MPEG-4 video codec.

A new VideoWriter object is created, which is used to write video frames to a file. In this case, the output file is named demo.avi, the video codec is XVID, the frame rate is set to 20 frames per second, and the frame size is set to 640 x 480 pixels.

This starts an infinite loop that will continuously capture frames from the camera or video file and write them to the output file. Then reads a single frame from the input video source and stores it in the frame variable. The ret variable is a boolean that indicates whether the frame was successfully read from the source.

Checking whether the current frame was successfully read from the input source. Then flips the current frame horizontally using the cv2.flip() function, which takes two arguments: the input frame and the flip code. In this case, the flip code is set to 1, which means that the frame should be flipped horizontally.

Write the flipped frame to the output video file using the demo.write() function.

This displays the current frame in a new window titled "frame" using the cv2.imshow() function. The first argument is the title of the window, and the second argument is the image data for the current frame.

This waits for a key press from the user for 1 millisecond using the cv2.waitKey() function. If the key press is the letter 'q', the loop will exit.

This releases the input video capture object, freeing up any resources used by the OpenCV library. The output video writer object is released, finalizing the output video file and freeing up any resources used by the OpenCV library. All windows created by the cv2.imshow() function is closed, freeing up any resources used by the OpenCV library.

capturing videos through a webcam

Conclusion

  • An easy-to-use interface provided by OpenCV makes it possible to record live video from the camera (webcam). It displays a video that has been converted to grayscale.
  • To record a video, we first build a VideoCapture object. Either the device index or the filename of a movie can be used.
  • The cv2.VideoCapture object facilitates video processing by performing frame-by-frame processing.

See also

Open cv Documentation

https://docs.opencv.org/3.4/d8/dfe/classcv_1_1VideoCapture.html

cv2.VideoCapture() function Tutorials

https://docs.opencv.org/3.4/dd/d43/tutorial_py_video_display.html