Life2Coding
Play a Video in Reverse Mode using Python OpenCV

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to play a video in reverse using opencv python

Documentation:

VideoCapture()

“VideoCapture object”=cv.VideoCapture()
“VideoCapture object”=cv.VideoCapture(filename[, apiPreference])
“VideoCapture object”=cv.VideoCapture(index[, apiPreference])
“VideoCapture object”=cv.VideoCapture()
“VideoCapture object”=cv.VideoCapture(filename[, apiPreference])
“VideoCapture object”=cv.VideoCapture(index[, apiPreference])
“VideoCapture object”=cv.VideoCapture()
“VideoCapture object”=cv.VideoCapture(filename[, apiPreference])
“VideoCapture object”=cv.VideoCapture(index[, apiPreference])

Default constructor.

Opens a video file or a capturing device or an IP video stream for video capturing with API Preference.

Opens a camera for video capturing.

Parameters
filename it can be:

  • name of video file (eg. video.avi)
  • or image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
  • or URL of video stream (eg. protocol://host:port/script_name?script_params|auth). Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL.
apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW.
Parameters
index id of the video capturing device to open. To open default camera using default backend just pass 0. (to backward compatibility usage of camera_id + domain_offset (CAP_*) is valid when apiPreference is CAP_ANY)
apiPreference preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_DSHOW or cv::CAP_MSMF or cv::CAP_V4L.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • Create a VideoCapture object using cv2.VideoCapture() to read the video file
  • Read the number of frames using cv2.CAP_PROP_FRAME_COUNT
  • Set the starting frame position to the last frame using capture.set(cv2.CAP_PROP_POS_FRAMES,frame_idx)
  • Check for return value for each frame read from the video file with capture.read()
  • Decrement the frame index by 1 in the while loop()
  • Display all the frames using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2

# Create a VideoCapture object to read the video file
capture = cv2.VideoCapture("./bunny.mp4")

# check for camera openning
if capture.isOpened() is False:
    print("Error opening video")

# Get the total number of frames
frame_idx = capture.get(cv2.CAP_PROP_FRAME_COUNT) - 1
print("Starting Frame: '{}'".format(frame_idx))

# Read until video is finished:
while capture.isOpened() and frame_idx >= 0:

    # Set the current frame position to start:
    capture.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)

    # Capture frame-by-frame from the video:
    ret, frame = capture.read()

    if ret is True:
        # Display the frame:
        cv2.imshow('Frame in Reverse', frame)

        # Reduce the index to read next frame:
        frame_idx = frame_idx - 1
        print("Next index: '{}'".format(frame_idx))

        # Press q on keyboard to exit the program:
        if cv2.waitKey(30) & 0xFF == ord('q'):
            break
    # Break the while loop
    else:
        break

# Release the VideoCapture object:
capture.release()
cv2.destroyAllWindows()

Output:

5cd8c607db002 Play a Video in Reverse Mode using Python OpenCV
life2coding_icon [] Play a Video in Reverse Mode using Python OpenCV

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.