【问题标题】:OpenCV to extract n-th frames from multiple filesOpenCV从多个文件中提取第n帧
【发布时间】:2020-10-03 08:17:36
【问题描述】:

我正在尝试从目录中的每个视频中捕获每 200 帧。我写了一个代码来解决这个问题,但问题是它从一个视频中提取所有帧,然后移动到下一个视频。但是我试图从一个视频中提取帧,然后从下一个视频中提取相同的帧,依此类推,一旦从所有视频中提取了第 n 帧,然后增加帧计数器并开始以相同的顺序提取下一帧.

我的代码 sn-p 如下:

for samples in path: # path is a list of all the video samples
  video = os.path.join(rootDir, samples)

  cap = cv2.VideoCapture(video)
  frameRate = 200 # frame rate
  currentframe=1

  while(cap.isOpened()):
    frameId = cap.get(1)
    ret, frame = cap.read() # reading from frame
    if (ret != True):
        break
    currentframe+=1
    if (currentframe % math.floor(frameRate) == 0):
      filename = './'+str(samples[:-4])+'/image'+str(int(currentframe))+".jpg"
      print('Creating...'+filename)
      cv2.imwrite(filename, frame)

# Release all the sape and windows once done
cap.release()

【问题讨论】:

  • 你有多少个视频?为什么不简单地创建 n 个 cap 对象并同时提取所有视频的帧?
  • @roygbiv 最多大约 100-200 个视频。你能告诉我如何创建 n-cap 对象并同时提取吗? (我是 CV 的新手 :D)谢谢!

标签: python-3.x list opencv video computer-vision


【解决方案1】:

一个解决方案可能是这样的,创建一个称为 caps 的 cap 对象列表并对其进行迭代。

caps = []

for samples in path: # path is a list of all the video samples
  video = os.path.join(rootDir, samples)

  cap = cv2.VideoCapture(video)
  caps.append(cap)


frameRate = 200 # frame rate
currentframe=1

while(caps[0].isOpened()):

    frames = []

    for cap in caps:
        ret, frame = cap.read() # reading from frame
        frames.append(frame)

    if (ret != True):
        break
    currentframe+=1

    if (currentframe % math.floor(frameRate) == 0):
        for frame in frames:
            #filename = './'+str(samples[:-4])+'/image'+str(int(currentframe))+".jpg"
            #print('Creating...'+filename)
            #cv2.imwrite(filename, frame)

# Release all the sape and windows once done
for cap in caps:
    cap.release()

【讨论】:

  • 非常感谢,这太完美了。
  • @x2212 不客气,如果它解决了问题,请选择这个问题,否则它保持打开状态
猜你喜欢
  • 2014-05-07
  • 2021-12-13
  • 2018-08-01
  • 2014-09-30
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多