视频批量转化为图片
本代码使用到了CV2库
下面介绍如何安装opencv-python库
win+r打开运行窗口,输入cmd打开命令行窗口
activate
conda activate
conda activate tf-pt #激活之前创建的虚拟环境,tf-pt是博主自己创建的虚拟环境name
pip intall opencv-python #安装opencv库
import os
import cv2
def video2img(video_folder, img_folder, frequency=10):
video_files = os.listdir(video_folder)
for video_file in video_files:
print("{}图片提取中...".format(video_file))
times = 0
img_idx = 0
video_file_name = video_file.split(".")[0]
video_file_path = os.path.join(video_folder, video_file)
img_sub_folder = os.path.join(img_folder, video_file_name)
if not os.path.exists(img_sub_folder):
os.makedirs(img_sub_folder)
camera = cv2.VideoCapture(video_file_path)
while True:
times += 1
res, image = camera.read()
if not res:
print('EOF')
break
if times % frequency == 0:
img_name = video_file_name + "_" + str(img_idx) + ".jpg"
img_idx = img_idx + 1
img_save_path = os.path.join(img_sub_folder, img_name)
cv2.imwrite(img_save_path, image)
print('{}文件图片提取结束:{}张'.format(video_file, img_idx))
camera.release()
if __name__ == '__main__':
video2img(video_folder="E:/Test_Mov/Mov", img_folder="E:/Test_Mov/Train", frequency=10)
#此处放置视频文件路径(全英文)/照片输出文件路径(全英文)