1、从单个视频中提取
import cv2
if __name__ == '__main__':
# 指定视频序列
video_name = 'DSC_0239.MOV'
capture = cv2.VideoCapture('./video/' + video_name)
index = 0
while (capture.isOpened()):
ret, frame = capture.read()
if ret==True:
index += 1
if (index >= 5 and index <= 1000 and (index) % 5 == 0):
filename = '1_' + str(int((index)/5)) + '.jpg'
print(filename)
# 显示帧
# cv2.imshow('img', frame)
# 保存帧
cv2.imwrite('./image/1/' + filename, frame)
cv2.waitKey(10)
else:
break
capture.release()
cv2.destroyAllWindows()
2、从某一个文件夹中的所有视频序列中提取
import cv2
import os
if __name__ == '__main__':
label = "3_4"
path = './video/' + label + "/"
files = os.listdir(path)
image_index = 0
for video_name in files:
index = 0
capture = cv2.VideoCapture(path + video_name)
while (capture.isOpened()):
ret, frame = capture.read()
if ret==True:
index += 1
if (index >= 0 and index <= 1000 and (index) % 50 == 0):
filename = label +'_' + str(image_index) + '.jpg'
image_index += 1
print(filename)
# 显示帧
# cv2.imshow('img_test', frame)
# 保存帧
cv2.imwrite('./image/' + label + "/" + filename, frame)
cv2.waitKey(10)
else:
break
capture.release()
cv2.destroyAllWindows()