Change language

Python | Playing video in reverse using OpenCV

| | | |

OpenCV’s application areas include: 1) Facial recognition system 2) motion tracking 3) Artificial neural network 4) Deep neural network 5) video streaming etc.

In Python, you can use the OpenCV library named CV2 . Python does not come with cv2, so the user has to install it separately.

For Windows:

For Linux:

The OpenCv library can be used to perform several operations on video. Let’s try to do something interesting using CV2 . Take a video as input and play it in reverse by splitting the video frame by frame and saving that frame in the list at the same time. After getting the list of frames, we iterate over the frames. To play the video in reverse mode, we only need to iterate the reverse in the frame list. Use the reverse list method to reorder frames in the list.

The implementation is as follows:

# Python video playback program
# in reverse using opencv

 
# cv2 library import

import cv2

 
# videoCapture cv2 method return video object

  
# Pass the absolute address of the video file

cap = cv2.VideoCapture ( "video_file_location" )

< code class = "undefined spaces">  
# method to read the video object will return
# tuple with 1 -th element means
# the frame was read successfully or not,
# 2nd element - this is the actual frame.

 
# Capture the current frame.

check, vid = cap.read ()

 
# counter variable for
# frame count

counter = 0

 
# Initialize value
# check variable

check = True

  

frame_list = [ ]

 
# If the end of the video is reached
# then we got a False validation value.

 
# keep looping while we
# got a false validation value.

while (check = = True ):

  

# imwrite cv2 method saves

  # image in the specified format.

  cv2.imwrite ( "frame% d.jpg" % counter, vid)

  check, vid = cap.read ()

  

# Add every frame in the list

# using the add list method

frame_list. append (vid)

 

# increment counter by 1

counter + = 1

 
# last value in frame_list is None
# because when the video reaches the end
# then the false value is stored in the control variable
# and None is stored in the vide variable.

  
# deleting last value from
# frame_list using the pop List method
frame_list.pop ()

 
# loop in the frame list.

for frame in frame_list:

 

# show frame.

cv2.imshow ( "Frame" , frame)

 

# waitkey method to stop the frame

# for a while. the q key is pressing,

# stop loop

if cv2.waitKey ( 25 ) and 0xFF = = ord ( "q" ):

break

 
# video release method
# object to clear input video
cap.release ()

 
# close all open windows
cv2.destroyAl lWindows ()

 
# reverse element order
# listed with
# reverse list method.
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 ()

Output:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically