1.背景
根据业务需要,需要把视频转换为图片,生成样本集用来进行训练。因此记录一下,自己写的脚本,方便以后的使用
2.代码
import cv2
import os
def video_to_pic(video_path,save_path):
cap = cv2.VideoCapture(video_path)
sucess = cap.isOpened()
frame_count = 0
i = 0
while sucess:
frame_count += 1
sucess, frame = cap.read()
if sucess:
if (frame_count % 65 == 0): # 每秒采集一张,65是每秒视频流的帧数
i += 1
cv2.imwrite(os.path.join(save_path,str(i))+".jpg", frame)
cap.release()
if __name__ == '__main__':
video_path = "D:\handle_problem\handle_video\8.mp4"
image_path = "D:\handle_problem\handle_video\images\9"
video_to_pic(video_path,image_path)