# -*- coding:utf8 -*- import cv2 import os import shutil def get_frame_from_video(video_name, save_path,interval,resize): cv2.namedWindow('Read_Image', cv2.WINDOW_NORMAL) """ Args: video_name:输入视频名字 interval: 保存图片的帧率间隔 Returns: """ # 保存图片的路径 #save_path = video_name.split('.MOV')[0] + '/' save_path=save_path+str(resize)+"/" # 保存在对应缩放倍数文件夹 is_exists = os.path.exists(save_path) if not is_exists: os.makedirs(save_path) print('path of %s is build' % save_path) else: shutil.rmtree(save_path) os.makedirs(save_path) print('path of %s already exist and rebuild' % save_path) # 开始读视频 video_capture = cv2.VideoCapture(video_name) i = 0 j = 0 frame_nums = video_capture.get(7) # 获取视频总帧数 print("视频的总帧数为:", int(frame_nums)) frame_rete = video_capture.get(5) # 获取视频帧率 print("视频的帧率为:", int(frame_rete)) while True: success, frame = video_capture.read() if not success: print('video is all read') break i += 1 if i % interval == 0: if resize>1: width = int(frame.shape[1] / resize ) height = int(frame.shape[0] / resize ) dim = (width, height) frame=cv2.resize(frame,dim) cv2.imshow("Read_Image", frame) cv2.waitKey (1) # 保存图片 j += 1 #save_name = save_path + str(j) + '_' + str(i) + '.jpg' save_name = save_path + str(j) + '.jpg' cv2.imwrite(save_name, frame) print('保存图像' ,save_name) if __name__ == '__main__': # 视频文件名字 video_name = '非畸变视频.MOV' save_path="1_1video_undistortion/" # 保存路径不能有中文 interval = 29 #间隔帧数 resize=2 # 是否需要图像改变分辨率 1/resize get_frame_from_video(video_name, save_path,interval,resize) cv2.destroyAllWindows()