从视频中提取帧

时间:2018-03-28 16:39:56

标签: python python-3.x opencv

我正在尝试从视频中提取帧,我正在使用此代码:

import cv2
vidcap = cv2.VideoCapture('video_01.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  print('Read a new frame: ', success)
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  count += 1

返回

  

阅读新框架:错误

我已经在SO中关注了这个问题。我安装了ffmepg,av但没有任何作用。

我在Windows上运行它,通过Anaconda安装opencv。

这是什么解决方法?谢谢你的帮助

我也试过

import cv2
import numpy as np
import os

# Playing video from file:
cap = cv2.VideoCapture('example.mp4')

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

    # Saves image of the current frame in jpg file
    name = './data/frame' + str(currentFrame) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

然而,这可以保存空图像

2 个答案:

答案 0 :(得分:0)

看来您的代码没有问题, 请尝试运行以下命令:

pip install opencv-python

答案 1 :(得分:0)

如何在第二行代码(第15行)中循环时更改为此内容

while(cap.isOpened()):

来自

while(True):