Thursday 4 April 2024

Reading videos in OpenCV

In this post, I am going to explain how to read videos in OpenCV. Videos in OpenCV are read frame by frame. Follow below step-by-step procedure to read the video.

 

Step 1: Import opencv library.

import cv2 as cv

 

‘cv’ is an alias name given the cv2 module. This alias is used to reference the OpenCV functions and classes throughout the code

 

Step 2: Get an instance of video capture object.

video_capture = cv.VideoCapture('videos/bird.mp4')

 

Using video_capture object, we can read frames from the video, process the frames, and perform various video processing tasks like object tracking, detection, or any other video-related operations.

 

Step 3: Read the frames continuously from the video and display them in a window.

while True:
    # Read the video frame by frame
    # is_true says whether the frame is successfully read or not
    is_true, frame = video_capture.read()

    if is_true == False:
        break

    # Show the frame
    cv.imshow('video', frame)

    # If the letter x is pressed then come out of video
    if (cv.waitKey(20) & 0xFF) == ord('x'):
        break

 

is_true, frame = video_capture.read()

Above statement read single frame from a video source. ‘video_capture.read’ method read single from the given video and return two values. First value ‘is_true’ indicates whether the frame was successfully read or not. Second value ‘frame’ points to the actual frame data that was read from the video file.

 

cv.imshow('bird video', frame)

 

'imshow' method displays the image in a new window. First argument 'bird video' specifies the title of the window in which the frame is displayed. Second argument ‘frame’ specifies the data that you want to display.

 

if (cv.waitKey(20) & 0xFF) == ord('x'):

    break

Above snippet waits for a key press event with a 20-millisecond timeout, checks if the key pressed is 'x', and if it is, it breaks out of the loop.

 

Step 4: Release the video capture resource.

video_capture.release()

 

Once we are done with video file processing, it is important to release the resources properly. Above statement release the resources associated with video capture object.

 

Step 5: Close all the currently open windows.

cv.destroyAllWindows()

 

When you want to close all OpenCV windows and exit your application gracefully,  use destroyAllWindows method. Calling ‘destroyAllWindows’ method prevent resource leakage issues.

 

Find the below working application.

 

read_video.py
import cv2 as cv

video_capture = cv.VideoCapture('videos/bird.mp4')

while True:
    # Read the video frame by frame
    # is_true says whether the frame is successfully read or not
    is_true, frame = video_capture.read()

    if is_true == False:
        break

    # Show the frame
    cv.imshow('bird video', frame)

    # If the letter x is pressed then come out of video
    if (cv.waitKey(20) & 0xFF) == ord('x'):
        break

video_capture.release()

# Close the OpenCV windows
cv.destroyAllWindows()

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment