【问题标题】:Python - Extracting distinct/unique frames from VideosPython - 从视频中提取不同/独特的帧
【发布时间】:2019-05-31 10:16:54
【问题描述】:

我正在尝试从视频中提取不同的帧。我使用了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

【问题讨论】:

  • 从一帧到下一帧的“独特”图像的定义是什么?
  • 如果第一张图片覆盖了视频的第一秒和第二秒,那么第二张图片应该包含视频的第三秒和第四秒。所以,第二帧应该从第一帧的边缘开始。让我知道这是否有意义,否则我会尝试以不同的方式详细说明。
  • 你能详细说明一下吗?您的视频是否包含不同的图像作为幻灯片,还是一个连续的视频?请定义“独特”。
  • 我已经用一个例子更新了这个问题。希望它清楚。

标签: python python-3.x opencv


【解决方案1】:

如果你想要秒,你可以划分代码并检查每秒的第一帧(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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 2012-05-27
    • 2013-12-22
    • 2014-02-27
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多