Python基础学习 Opencv读写视频并抽帧保存

Python基础学习
Opencv读写视频并抽帧保存

import cv2
import os

# 保存图片的路径
video_path = r'G:\MyCode\Test_mycode\collect data\collect data\pictures\orange'
savedpath = r'G:\MyCode\Test_mycode\collect data\collect data\pictures\Video sequence\Video seque'
video_list = os.listdir(video_path)

# 保存图片的帧率间隔
count = 750
i = 0
j = 0
for index, video_name in enumerate(video_list):
    video_path_ = os.path.join(video_path, video_name)
    # 开始读视频
    videoCapture = cv2.VideoCapture(video_path_)
    print("正在处理第{}个视频,总共{}个视频".format(index + 1, len(video_list)))

    while True:
        success, frame = videoCapture.read()
        print(success)
        i += 1
        # 保存图片
        j += 1
        savedname = 'video_' + str(j) + '.jpg'
        cv2.imwrite(os.path.join(savedpath, savedname), frame)
        print('image of %s is saved' % (savedname))

        if not success:
            print('video is all read')
            break
    videoCapture.release()

你可能感兴趣的:(Python基础学习 Opencv读写视频并抽帧保存)