Play a video in reverse mode using OpenCV Python

In this tutorial, we will get to know the method detecting to play a video in reverse mode using OpenCV Python. Here, we will learn about video streaming and playing in reverse mode. Also, we will see a Python program for the same. So, if you are looking to play a video in reverse mode using OpenCV Python then you are in the right place.

OpenCV (Open Source Computer Vision Library) is Python library to solve computer vision related problems. OpenCV is a vast library which provides various functions for video operations. Using OpenCV, we can capture a video from the webcam. It lets you create a video capture object which helps to capture videos through webcam and then we can perform desired operations on that video.

Basic algorithm to capture video using OpenCV Python:-

  • Use cv2.VideoCapture() to get a video capture object for the camera.
  • Set up an infinite while loop and use the read() method to read the frames using the above created object.
  • Use cv2.imshow() method to show the frames in the video.
  • Break the loop by clicking specific key on the keyboard.

Here is the Script to play a video in reverse mode using OpenCV Python:

 

from cv2 import cv2

cap = cv2.VideoCapture("vtest.avi")

ret, vid = cap.read()

count = 0

ret = True

frame_list = []

while(ret == True):

    #cv2.imwrite("frame%d.jpg" % count, vid)
    ret, vid = cap.read()

    frame_list.append(vid)

    count += 1

frame_list.pop()

for frame in frame_list:

    cv2.imshow("Frame", frame)
    if cv2.waitKey(25) and 0xFF == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

frame_list.reverse()

for frame in frame_list:
    cv2.imshow("Frame", frame)
    if cv2.waitKey(25) and 0xFF == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

Take a video as input  or can capture using .VideoCapture() function also. For playing it in a reverse mode is to first break the video into frame by frame and then simultaneously storing that frames into the list. After getting list of frames we perform the iteration over the frames using loop method. For playing video in reverse mode, we to iterate over the list of frames in reversal order. Using reverse() method on the list for reversing the order of frames in the list then iterating over the list play the video in reverse order.

 

For videos in OpenCV read here:-

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

Also read about:-

Motion Detection using OpenCV in Python

Leave a Reply

Your email address will not be published.