OpenCV(Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos.
OpenCV's application areas include :1) Facial recognition system2) motion tracking3) Artificial neural network4) Deep neural network5) video streaming etc.
In Python, one can use an OpenCV library namedCV2. Python does not come with cv2, so user needs to install it separately.
For Windows :
pip install opencv-python
For Linux :
sudo apt-get install python-opencv
OpenCv library can be used to perform multiple operations on videos. Let's try to do something interesting usingCV2. Take a video as input and play it in a reverse mode by breaking the video into frame by frame and simultaneously store that frame in the list. After getting list of frames we perform iteration over the frames. For playing video in reverse mode, we need only to iterate reverse in the list of frames. Use reverse method of the list for reversing the order of frames in the list.
Below is the implementation :
Python3# Python program to play a video# in reverse mode using opencv# import cv2 libraryimportcv2# videoCapture method of cv2 return video object# Pass absolute address of video filecap=cv2.VideoCapture("video_file_location")# read method of video object will return# a tuple with 1st element denotes whether# the frame was read successfully or not,# 2nd element is the actual frame.# Grab the current frame.check,vid=cap.read()# counter variable for# counting framescounter=0# Initialize the value# of check variablecheck=Trueframe_list=[]# If reached the end of the video# then we got False value of check.# keep looping until we# got False value of check.while(check==True):# imwrite method of cv2 saves the# image to the specified format.cv2.imwrite("frame%d.jpg"%counter,vid)check,vid=cap.read()# Add each frame in the list by# using append method of the Listframe_list.append(vid)# increment the counter by 1counter+=1# last value in the frame_list is None# because when video reaches to the end# then false value store in check variable# and None value is store in vide variable.# removing the last value from the# frame_list by using pop method of Listframe_list.pop()# looping in the List of frames.forframeinframe_list:# show the frame.cv2.imshow("Frame",frame)# waitkey method to stopping the frame# for some time. q key is presses,# stop the loopifcv2.waitKey(25)and0xFF==ord("q"):break# release method of video# object clean the input videocap.release()# close any open windowscv2.destroyAllWindows()# reverse the order of the element# present in the list by using# reverse method of the List.frame_list.reverse()forframeinframe_list:cv2.imshow("Frame",frame)ifcv2.waitKey(25)and0xFF==ord("q"):breakcap.release()cv2.destroyAllWindows()
Output :
