利用opencv库使用Python将视频逐帧转为图片

发布时间 2023-04-04 14:11:51作者: Do1phln

做成型的语义分割软件需要,写了一个,在博客记录一下

import cv2


def video2pic(videoFile, outputFile):
    vc = cv2.VideoCapture(videoFile)
    c = 1
    if vc.isOpened():
        rval, frame = vc.read()
    else:
        print('error open video!')
        rval = False

    timeF = 100  # 帧率间隔
    while rval:
        print(1)
        rval, frame = vc.read()
        if c % timeF == 0:
            print(2)
            cv2.imwrite(outputFile + str(int(c / timeF)) + '.jpg', frame)
        c += 1
        cv2.waitKey(1)
    vc.release()

if __name__ == '__main__':
    videoFile = './test.mp4'  # 输入路径
    outputFile = './video2pic_res/frame'  # 输出路径
    video2pic(videoFile, outputFile)