使用OpenCV读取视频并且保存为图片

使用OpenCV读取视频并且保存为图片

#coding=utf-8
import os,numpy
import cv2
from tqdm import tqdm
import numpy as np


def read_frame(path):
    # 获得视频的格式
    videoCapture = cv2.VideoCapture(path)

    # 获得码率及尺寸
    # fps = videoCapture.get(cv2.CAP_PROP_FPS)
    # size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
    #         int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    # fNUMS = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)

    frame_set = []

    # 读帧
    success, frame = videoCapture.read()
    while success:
        # cv2.imshow('windows', frame)  # 显示
        frame_set.append(frame)
        # cv2.waitKey(int(1000 / int(fps)))  # 延迟
        success, frame = videoCapture.read()  # 获取下一帧

    videoCapture.release()

    return success, frame_set


def main():
    path = '/data/video/'
    save_path_root = '/data/result/'
    file_names = os.listdir(path)
    i = 0
    for file_name in file_names:
        if file_name.endswith('avi'):
            print(file_name)
            sucess, frame_set = read_frame(path + file_name)
            save_path = save_path_root + '/' + file_name.split('.')[0] + '/'
            if not os.path.exists(save_path):
                os.mkdir(save_path)
            for frame in frame_set:
                cv2.imwrite(save_path + file_name.split('.')[0] + '_%05d.bmp' % i, frame)
                i += 1
    # file_name = '00204_h.mp4'

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值