个性化阅读
专注于IT技术分析

Python使用OpenCV反向播放视频?

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.

在Python中, 可以使用一个名为CV2。 Python不随cv2一起提供, 因此用户需要单独安装它。

对于Windows:pip install opencv-python
对于Linux:sudo apt-get install python-opencv

OpenCv库可用于对视频执行多项操作。让我们尝试使用CV2。将视频作为输入, 然后通过将视频逐帧拆分为反向并将其存储在列表中, 以反向模式播放。获取帧列表后, 我们对帧进行迭代。要以反向模式播放视频, 我们只需要在帧列表中迭代反向即可。使用列表的反转方法可以反转列表中帧的顺序。

下面是实现:

# Python program to play a video
# in reverse mode using opencv 
  
# import cv2 library
import cv2
  
# videoCapture method of cv2 return video object
  
# Pass absolute address of video file 
cap = 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 frames
counter = 0
  
# Initialize the value
# of check variable
check = True
  
frame_list = []
  
# If reached the end of the video 
# then we got False value of check.
  
# keep looping untill 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 List
     frame_list.append(vid)
      
     # increment the counter by 1
     counter + = 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 List
frame_list.pop()
  
# looping in the List of frames.
for frame in frame_list:
      
     # show the frame.
     cv2.imshow( "Frame" , frame)
      
     # waitkey method to stoping the frame
     # for some time. q key is presses, # stop the loop
     if cv2.waitKey( 25 ) and 0xFF = = ord ( "q" ):
         break
      
# release method of video 
# object clean the input video
cap.release()
  
# close any open windows
cv2.destroyAllWindows()
  
# reverse the order of the element 
# present in the list by using
# reverse method of the List.
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()

输出:

Python如何使用OpenCV反向播放视频?

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(1)
未经允许不得转载:srcmini » Python使用OpenCV反向播放视频?

评论 抢沙发

评论前必须登录!