本文共 1401 字,大约阅读时间需要 4 分钟。
# -*- coding: utf-8 -*- import os import cv2import csvimport numpy as npdef GetImgNameByEveryDir(file_dir,videoProperty): FileNameWithPath = [] FileName = [] FileDir = [] # videoProperty=['.png','jpg','bmp'] for root, dirs, files in os.walk(file_dir): for file in files: if os.path.splitext(file)[1] in videoProperty: FileNameWithPath.append(os.path.join(root, file)) # 保存图片路径 FileName.append(file) # 保存图片名称 FileDir.append(root[len(file_dir):]) # 保存图片所在文件夹 return FileName,FileNameWithPath,FileDir# 以视频文件名创建文件夹,然后保存图像到对应文件夹def AVI_To_Img_And_save(video_file,save_dir): if os.path.exists(save_dir)==False: os.makedirs(save_dir) cap = cv2.VideoCapture(video_file) rval = cap.isOpened() framenum=0 while rval: rval, frame = cap.read() if rval==False: continue if framenum%25!=0: framenum+=1 continue Img_savename = save_dir + '/' + 'image_' + '%07d'%framenum +'.jpg' if rval: if os.path.exists(Img_savename)==False: cv2.imwrite(Img_savename, frame,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) else: break framenum+=1 cap.release()videoDir = './videoProperty = ['.mp4]FileName,FileNameWithPath,FileDir = GetImgNameByEveryDir(videoDir,videoProperty)for k in range(len(FileName)): AVI_To_Img_And_save(FileNameWithPath[k],FileName[k][:-4])
转载地址:http://puhws.baihongyu.com/