如何通过opencv不停地读取视频中的所有帧

时间:2019-06-14 06:26:49

标签: python opencv google-colaboratory

Iv'编写了这段代码,以从视频中提取所有帧并将其另存为图像。 但是,即使VideoCapture.read()尚未到达末帧,它仍会以某种方式返回false。

如何修复它并读取所有框架?

我尝试了catch异常,但是它什么也不返回。

我遵循了源代码,发现VideoCaputre.read()调用了VideoCapture.grab,这称为icap-> grabFrame(),但无法获取有关icap-> grabFrame()的详细信息。

python3.6.7(默认,2018年10月22日,11:32:17) Opencv 3.4.3 Google合作实验室

视频信息 'coded_width':4000 'coded_height':3000 'r_frame_rate':'30000/1001' 'avg_frame_rate':'30000/1001'

import os
import ffmpeg
import json
import cv2
import sys


base_dir = './gdrive/My Drive/'
files = os.listdir(base_dir)    

# only videos
mp4s =[]
mp4s = [file for file in files if 'MP4' in file ]

# get metadata
file_list = []

for video in mp4s:
  video_dir = base_dir + '/' + video
  video_info = ffmpeg.probe(video_dir)
  time = video_info['streams'][0]['tags']['creation_time']
  slice_str = time.find('T')
  slice_end = time.find('.')

  file_list.append({
      'file_name':video,
      'start_rec':time[slice_str + 1:slice_end],
      'end_rec':video_info['streams'][0]['tags']['timecode'],
      'duration':video_info['streams'][0]['duration'],
      'nb_frames':video_info['streams'][0]['nb_frames'],
      'coded_width':video_info['streams'][0]['coded_width'],
      'coded_height':video_info['streams'][0]['coded_height'],
      'r_frame_rate':video_info['streams'][0]['r_frame_rate'],
      'avg_frame_rate':video_info['streams'][0]['avg_frame_rate']
  }) 

#sort by recording start time
list_sorted = sorted(file_list, key=lambda x:x['start_rec'])
print(list_sorted)

#opencv extract all frames and save as png
dir_path = base_dir + '/' + 'images'
basename ='intervention'
ext = 'png'
n = 0

for dic in list_sorted:
  print('start', dic['file_name'])
  video_path = base_dir + '/' + dic['file_name']
  cap = cv2.VideoCapture(video_path)

  if not cap.isOpened():
    print('not opened')
    sys.exit()

  os.makedirs(dir_path, exist_ok=True)
  base_path = os.path.join(dir_path, basename)

  digit = len(str(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))


# below this code ret supposed to be true until the end frame but did't reach the end

  while True:
    ret, frame = cap.read()
    print('ret=', ret)
    print(cap.get(cv2.CAP_PROP_POS_FRAMES), '/', cap.get(cv2.CAP_PROP_FRAME_COUNT))

    if ret:
      cv2.imwrite('{}_{}.{}'.format(base_path, str(n).zfill(digit), ext), frame)
      n += 1

    else:
      print('done', dic['file_name'])
      cap.release()
      cv2.destroyAllWindows()
      break 

此返回

启动GX010223.MP4 ret =真 1.0 / 96.0 〜 ret =真 96.0 / 96.0 ret =错误 96.0 / 96.0 完成GX010223.MP4 启动GX010224.MP4 ret =真 1.0 / 105.0 〜 ret =真 105.0 / 105.0 ret =错误 105.0 / 105.0 完成GX010224.MP4

启动GX010225.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX010225.MP4

启动GX020225.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX020225.MP4

启动GX030225.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成了GX030225.MP4

启动GX040225.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX040225.MP4

启动GX050225.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX050225.MP4

启动GX060225.MP4 ret =真 1.0 / 539.0 〜 ret =真 29.0 / 539.0 ret =错误 29.0 / 539.0 完成GX060225.MP4

启动GX010226.MP4 ret =真 1.0 / 282.0 〜 ret =真 29.0 / 282.0 ret =错误 29.0 / 282.0 完成GX010226.MP4

启动GX010227.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX010227.MP4

启动GX020227.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX020227.MP4

启动GX030227.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成了GX030227.MP4

启动GX040227.MP4 ret =真 1.0 / 15960.0 〜 ret =真 29.0 / 15960.0 ret =错误 29.0 / 15960.0 完成GX040227.MP4

启动GX050227.MP4 ret =真 1.0 / 2514.0 〜 ret =真 29.0 / 2514.0 ret =错误 29.0 / 2514.0 完成GX050227.MP4

我已尝试通过此代码检查“是否为空”

while True:

      ret, frame = cap.read()
      print('ret=', ret)
      print(cap.get(cv2.CAP_PROP_POS_FRAMES), '/', cap.get(cv2.CAP_PROP_FRAME_COUNT))
      cv2.imwrite('{}_{}.{}'.format(base_path, str(n).zfill(digit), ext), frame)
      n += 1

      if ret == False:
        if frame == None:
          print('done', dic['file_name'])
          cap.release()
          cv2.destroyAllWindows()
          break

但是当cap.read()失败时,它会返回None对象,因此无法正常工作。

1 个答案:

答案 0 :(得分:0)

VideoCapture cap("your_video.avi");
if(!cap.isOpened())  // check if we succeeded
    return -1;

Mat frame;
while(1) // looply reading frames from the video file
{
    cap >> frame; // try to get an image frame

    if (frame.empty())
    {
        // reach to the end of the video file
        break;
    }
}

frame.empty()将解决您的问题。它在c ++中,但在python中可能相同。