所以我遵循这个教程,但它似乎没有做任何事情。只是没有。它等待几秒钟并关闭程序。这段代码有什么问题?

import cv2
vidcap = cv2.VideoCapture('Compton.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  if cv2.waitKey(10) == 27:                     # exit if Escape is hit
      break
  count += 1

此外,在评论中它说这限制帧到1000?为什么?

编辑: 我试着先做success = True,但没有帮助。它只创建了一个0字节的图像。


当前回答

之前的答案已经失去了第一帧。把图片存储在文件夹里会很好。

# create a folder to store extracted images
import os
folder = 'test'  
os.mkdir(folder)
# use opencv to do the job
import cv2
print(cv2.__version__)  # my version is 3.1.0
vidcap = cv2.VideoCapture('test_video.mp4')
count = 0
while True:
    success,image = vidcap.read()
    if not success:
        break
    cv2.imwrite(os.path.join(folder,"frame{:d}.jpg".format(count)), image)     # save frame as JPEG file
    count += 1
print("{} images are extacted in {}.".format(count,folder))

顺便说一下,你可以通过VLC检查帧率。进入“windows ->媒体信息->编解码器详细信息”

其他回答

之前的答案已经失去了第一帧。把图片存储在文件夹里会很好。

# create a folder to store extracted images
import os
folder = 'test'  
os.mkdir(folder)
# use opencv to do the job
import cv2
print(cv2.__version__)  # my version is 3.1.0
vidcap = cv2.VideoCapture('test_video.mp4')
count = 0
while True:
    success,image = vidcap.read()
    if not success:
        break
    cv2.imwrite(os.path.join(folder,"frame{:d}.jpg".format(count)), image)     # save frame as JPEG file
    count += 1
print("{} images are extacted in {}.".format(count,folder))

顺便说一下,你可以通过VLC检查帧率。进入“windows ->媒体信息->编解码器详细信息”

我可能在这里迟到了,但你可以使用这个PIP包从视频中快速生成图像。你也可以使用特定的fps来获取图像。

pip安装videoToImages

然后在终端中输入以下命令

视频到图像 --视频文件夹 [路径到视频文件夹]

示例:videoToimages——videoofolder "c:/videos"

对于特定的输出FPS,将——FPS 10设置为任何所需的值。-fps 1表示视频中每秒钟一个图像。

完整的命令:

videoToimages—视频文件夹“c:/videos” videoToimages——videoofolder "c:/videos"——fps 10——img_size (512, 512)

从这里下载这个视频,这样我们就有了相同的测试视频文件。确保mp4文件与python代码位于同一目录。然后确保从相同的目录运行python解释器。

然后修改代码,抛弃浪费时间的waitKey,也没有窗口,它不能捕捉键盘事件。同时,我们打印success值以确保它成功读取帧。

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

这是怎么回事?

这是对python 3之前答案的一个调整。x来自@ gsurprised,我想把它贴在评论里,但是没有足够的声誉

import sys
import argparse

import cv2
print(cv2.__version__)

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

if __name__=="__main__":
    print("aba")
    a = argparse.ArgumentParser()
    a.add_argument("--pathIn", help="path to video")
    a.add_argument("--pathOut", help="path to images")
    args = a.parse_args()
    print(args)
    extractImages(args.pathIn, args.pathOut)

这段代码从视频中提取帧并以.jpg格式保存帧

import cv2
import numpy as np
import os

# set video file path of input video with name and extension
vid = cv2.VideoCapture('VideoPath')


if not os.path.exists('images'):
    os.makedirs('images')

#for frame identity
index = 0
while(True):
    # Extract images
    ret, frame = vid.read()
    # end of frames
    if not ret: 
        break
    # Saves images
    name = './images/frame' + str(index) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # next frame
    index += 1