如何在文件夹中的所有视频文件上运行Python cv2代码?

时间:2018-01-03 20:55:42

标签: python opencv cv2

我编写了一些python代码,用于使用cv2从视频文件中提取两个帧。我已经测试了我的代码,它适用于一个视频文件。

但是,我有一个包含数百个视频文件的文件夹,我想从该文件夹中的所有视频中提取两个帧。有没有为文件夹中的所有文件执行此操作的方法?也许使用os模块或类似的东西?

我的代码如下:

    import cv2
    import numpy as np
    import os

    # Playing video from file:
    cap = cv2.VideoCapture('/Users/batuhanyildirim/Desktop/UCF-
    101/BenchPress/v_benchPress_g25_c07_mp4.m4v')

    try:
        if not os.path.exists('data'):
            os.makedirs('data')
    except OSError:
        print('Error: Creating directory of data')

    currentFrame = 0

    while(True):
        # Capture frame by frame
        ret, frame = cap.read()

        # Only take the first frame and tenth frame
        if currentFrame == 0:
            # Saves image of the current frame in jpg file
            name = './data/frame' + str(currentFrame) + '.jpg'
            print ('Creating...' + name)
            cv2.imwrite(name, frame)

        if currentFrame == 5:
            name = './data/frame' + str(currentFrame) + '.jpg'
            print ('Creating...' + name)
            cv2.imwrite(name, frame)

        # To stop duplicate images
        currentFrame += 1

    cap.release()
    cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:1)

只需将其设为广告,就可以在每个视频的for循环中调用它

import cv2
import numpy as np
import os

def frame_capture(file):
  # Playing video from file:
  cap = cv2.VideoCapture(file)

  try:
      if not os.path.exists('data'):
          os.makedirs('data')
  except OSError:
      print('Error: Creating directory of data')

  currentFrame = 0

  while(True):
      # Capture frame by frame
      ret, frame = cap.read()

      # Only take the first frame and tenth frame
      if currentFrame == 0:
          # Saves image of the current frame in jpg file
          name = './data/frame' + str(currentFrame) + '.jpg'
          print ('Creating...' + name)
          cv2.imwrite(name, frame)

      if currentFrame == 5:
          name = './data/frame' + str(currentFrame) + '.jpg'
          print ('Creating...' + name)
          cv2.imwrite(name, frame)

      # To stop duplicate images
      currentFrame += 1

  cap.release()
  cv2.destroyAllWindows()
循环

import os
for file in os.listdir("/mydir"):
    if file.endswith(".m4v"):
        path=os.path.join("/mydir", file))
        frame_capture(path)

答案 1 :(得分:0)

选择的答案很好,但是对我来说运行了一个多小时,因此我在此代码之前的一步中创建了目录并将其设置为我的工作目录,然后在其中的最后一个if语句之后添加了“ break”上面的代码。做到了!

    import cv2
    import numpy as np
    import os

    def frame_capture(file):
        # Playing video from file:
       cap = cv2.VideoCapture(file)

      currentFrame = 0

      while(True):
          # Capture frame by frame
          ret, frame = cap.read()

            # Only take the tenth frame

          if currentFrame == 5:
              name = './data/frame' + str(currentFrame) + '.jpg'
              print ('Creating...' + name)
              cv2.imwrite(name, frame)
              break

              # To stop duplicate images
          currentFrame += 1

      cap.release()
      cv2.destroyAllWindows()