a60723610 发表于 2021-6-15 14:57:33

cv2从视频中截取图片报错

从视频中截取图片
视频时一秒一帧的



代码:
import cv2
import os
import time

# 视频路径
video_path = r'D:\ceshi\data\videos'
# 保存图片的路径
savedpath = r'D:\ceshi\data\video_imgs'

video_list = os.listdir(video_path)

# 保存图片的帧率间隔
count = 1
i = 0

for index, video_name in enumerate(video_list):
    j = 0
    video_path_ = os.path.join(video_path, video_name)
    # 某个视频帧保存路径
    save_path_ = os.path.join(savedpath, video_name.split('.'))
    if os.path.exists(save_path_):
      pass
    else:
      os.mkdir(save_path_)

    # 开始读视频

    videoCapture = cv2.VideoCapture(video_path_)

    print("正在处理第{}个视频,总共{}个视频".format(index + 1, len(video_list)))

    while True:

      success, frame = videoCapture.read()

      i += 1

      if (i % count == 0):
            # 保存图片

            j += 1

            savedname = video_name[:-4] + str(i).zfill(6) + '.jpg'

            cv2.imwrite(os.path.join(save_path_, savedname), frame)

            print('image of %s is saved' % (savedname))

      if not success:
            print('video is all read')

            break

    videoCapture.release()

    time.sleep(5)


报错:
Traceback (most recent call last):
File "D:/ceshi/ffmpeg.py", line 45, in <module>
    cv2.imwrite(os.path.join(save_path_, savedname), frame)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-1y7gm6kn\opencv\modules\imgcodecs\src\loadsave.cpp:721: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

xiaosi4081 发表于 2021-6-15 20:19:03

https://blog.csdn.net/Kimtschau/article/details/103399565

975050208@qq.co 发表于 2021-6-16 11:34:56

检查这两句话按照D:\ceshi\data\videos这个路径 能找到视频吗

# 视频路径
video_path = r'D:\ceshi\data\videos'
# 保存图片的路径
savedpath = r'D:\ceshi\data\video_imgs'


页: [1]
查看完整版本: cv2从视频中截取图片报错