Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x OpenCV从多个文件中提取第n帧_Python 3.x_List_Opencv_Video_Computer Vision - Fatal编程技术网

Python 3.x OpenCV从多个文件中提取第n帧

Python 3.x OpenCV从多个文件中提取第n帧,python-3.x,list,opencv,video,computer-vision,Python 3.x,List,Opencv,Video,Computer Vision,我试图从目录中的每个视频中捕获每200帧。我写了一个代码来解决这个问题,但问题是它从一个视频中提取所有帧,然后移动到下一个视频。但我试图从一个视频中提取帧,然后从下一个视频中提取相同的帧,以此类推,一旦从所有视频中提取第n帧,然后增加帧计数器,并开始以相同的顺序提取下一帧 我的代码片段如下所示: for samples in path: # path is a list of all the video samples video = os.path.join(rootDir, sample

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

我的代码片段如下所示:

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

解决方案可以是这样的,创建一个名为cap的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()

解决方案可以是这样的,创建一个名为cap的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()

你有多少视频?为什么不创建n-cap对象并同时提取所有视频的帧?@roygbiv最多大约100-200个视频。您能告诉我如何创建n-cap对象并同时提取吗?(我不熟悉简历:D)谢谢!你有多少视频?为什么不创建n-cap对象并同时提取所有视频的帧?@roygbiv最多大约100-200个视频。您能告诉我如何创建n-cap对象并同时提取吗?(我不熟悉简历:D)谢谢!非常感谢,这是完美的。@x2212不客气,如果问题解决了,请选择此问题,否则它将保持打开状态。非常感谢,这是完美的。@x2212不客气,如果问题解决了,请选择此问题,否则它将保持打开状态