cv实现抽帧

#coding=utf-8

import os

import cv2
from PIL import Image
import numpy as np

vadiolist = ['E:\\data\\vedio\\vedio\\' + f for f in os.listdir('E:\\data\\vedio\\vedio\\')]
for iii in vadiolist:
    cap = cv2.VideoCapture(iii)  # 获取视频对象
    isOpened = cap.isOpened  # 判断是否打开
    fps = cap.get(cv2.CAP_PROP_FPS)
    imageNum = 0
    sum=0
    timef = 50  
    name = iii.split(os.sep)[-1].split('.')[0]
    if name == "何":
        name = 'he'

    newimgdir = 'E:\\data\\vedio\\' + name
    if not os.path.exists(newimgdir):
        os.makedirs(newimgdir)
    while (isOpened):
        sum+=1
        (frameState, frame) = cap.read()  # 记录每帧及获取状态

        if frameState == True and (sum % timef==0):

            # 格式转变,BGRtoRGB
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            # 转变成Image
            frame = Image.fromarray(np.uint8(frame))

            frame = np.array(frame)

            # RGBtoBGR满足opencv显示格式
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

            imageNum = imageNum + 1

            fileName = newimgdir + os.sep + str(imageNum) + '.jpg'  # 存储路径
            cv2.imwrite(fileName, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])

        elif frameState == False:
            break

    print('finish!')
    cap.release()

欲对个视频进行抽帧并保存在不同目录下,使用cv通过帧数间隔来抽帧 

参考(25条消息) 【OpenCV实战】视频转图像,视频抽帧,图像合成视频_AI 菌的博客-CSDN博客_opencv视频抽帧icon-default.png?t=M85Bhttps://blog.csdn.net/wjinjie/article/details/114931633

参考:(25条消息) 用python+opencv实现视频抽帧_BlackBack_的博客-CSDN博客_opencv对直播流进行抽帧icon-default.png?t=M85Bhttps://blog.csdn.net/you2336/article/details/117732562参考:(25条消息) 【OpenCV实战】视频转图像,视频抽帧,图像合成视频_AI 菌的博客-CSDN博客_opencv视频抽帧

你可能感兴趣的:(cv学习,opencv,python,计算机视觉)