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

Nan*_*rai 5 python opencv python-3.x

我正在尝试从视频中提取不同的帧。我使用了 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()
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个连续的视频。例如,https://www.youtube.com/watch?v =JBLQbOG8Z1Y,在此,我需要天际线的单独镜头,即第一秒的一张和第六秒的一张。

第一秒
第六秒

小智 1

如果你想要秒,你可以划分代码并检查每秒的第一帧(每秒帧数)

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()
Run Code Online (Sandbox Code Playgroud)

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