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

How do I run my Python cv2 code on all video files in a folder?

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

但是,我有一个包含数百个视频文件的文件夹,我想从该文件夹中的所有视频os 中提取两帧。有没有一种方法可以对文件夹中的所有文件执行此操作?也许使用 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()

只需让它运行并在每个视频的 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)

选择的答案很好,但 运行 对我来说已经超过一个小时了,所以我创建了目录并将其设置为我的工作目录,在此代码之前的一个步骤中,然后添加了一个 'break' 在上面代码中的最后一个 if 语句之后。成功了!

    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()