Python-从视频中提取不同/独特的帧

时间:2019-01-04 20:43:39

标签: python python-3.x opencv

我正在尝试从视频中提取不同的帧。我使用过opencv(下面的代码),它使我可以从视频中提取所有帧,但是我要寻找的是彼此不重叠的独特图像。有什么建议么? TIA。

import cv2
import os

video = cv2.VideoCapture("INPUT.MOV")

currFrame =0

while(True):
   ret,frame = video.read()

   if ret:
       name = './data/frame'+ str(currFrame) + '.jpg'

       cv2.imwrite(name, frame)

       currFrame += 1
   else:
       break

video.release()
cv2.destroyAllWindows()

编辑: 这是连续的视频。例如https://www.youtube.com/watch?v=JBLQbOG8Z1Y,在此,我需要分别拍摄天际线,即第一秒和第六秒。

1st Second
6th Second

1 个答案:

答案 0 :(得分:0)

如果你想要秒,你可以分割代码并检查每秒的第一帧(Frames per second)

<块引用>
video = cv2.VideoCapture("INPUT.MOV")
  
try:
    # creating a folder named data
    if not os.path.exists('secondWiseData'):
        os.makedirs('secondWiseData')

except OSError:
    print ('Error: Creating directory of data')

currentframe = 0

#Check the version of opencv you are using
# Find OpenCV version

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

# if int(major_ver)  < 3 :
fps = int(video.get(cv2.cv.CV_CAP_PROP_FPS))
#Else    
fps = int(video.get(cv2.CAP_PROP_FPS))

#The FPS generated could be decimal as well. We get its greatest interger function by casting to int().

print('The FPS of the video is ', str(fps))
time_start=time.time()
    
while(True):
      
    # reading from frame
    ret,frame = cam.read()
  
    if ret:
        # if video is still left continue creating images
        name = './secondWiseData/frame' + str(currentframe) + '.jpg'
        
        if currentframe % fps == 1: 
            print ('Creating...' + name)
            cv2.imwrite(name, frame)
            currentframe += 1
            continue
        else:
            currentframe += 1
            continue
  
        # increasing counter so that it will
        # show how many frames are created
        
    else:
        break

time_end=time.time()
print('Total time taken is ', time_end-time_start)    
print('Total frames are ', currentframe)

#Release all space and windows once done
cam.release()
cv2.destroyAllWindows()

这将为您提供所考虑视频中每秒的帧数。