用opencv把视频转为图像

opencv读取视频保存为帧图像。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 24 11:59:51 2020

@author: cong
"""

import cv2
import shutil
import os

def video2img(input_path,outpath):
    cap = cv2.VideoCapture(input_path)
    total_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print('总帧数:', total_frame)
    
    if not os.path.isdir(outpath):
        os.makedirs(outpath)
    else:
        shutil.rmtree(outpath)
        os.makedirs(outpath)
    counter = 0
    if cap.isOpened():
        while True:
            ret, frame = cap.read()
            if not ret:
                break
            counter += 1
            imgname = "%s.jpg"% str(counter)
            path = os.path.join(outpath, imgname)
            cv2.imwrite(path,frame)
            print(counter)
    cap.release()
    
video2img('1.mp4','images')

你可能感兴趣的:(图像预处理,opencv,video,images,opencv)