import cv2
import glob
import os
from tqdm import tqdm
is_stati = True
if __name__ == '__main__':
if is_stati:
path = os.path.join(os.getcwd(), 'result')
path = path+'/*.jpg'
pic_list = glob.glob(path)
print("该目录下总共有:", len(pic_list), '张图片')
else:
if not os.path.exists('./result'):
os.mkdir('./result')
video_list = glob.glob(r'./*.mp4')
for video in tqdm(video_list):
name = video.split('/')[-1]
cap = cv2.VideoCapture(video)
name_list = name.split('-')
assert len(name_list) == 3
count = 1
num = 1
while True:
ret, frame = cap.read()
if not ret:
break
if name_list[1] == '常规场景':
if count % 5 == 0 and num <= 30: # 抽取视频中帧数为5的倍数图片,最多只抽取30张
temp = name_list[0] + '_' + name_list[1] + str(count) + '_' + name_list[2]
img_path = os.path.join(os.getcwd(), 'result', temp)
img_path = img_path[:-3] + 'jpg'
cv2.imwrite(img_path, frame)
num += 1
else:
if count % 30 == 0 and num <= 5: # 抽取视频中帧数为30的倍数图片,最多只抽取5张
temp = name_list[0] + '_' + name_list[1] + str(count) + '_' + name_list[2]
img_path = os.path.join(os.getcwd(), 'result', temp)
img_path = img_path[:-3] + 'jpg'
cv2.imwrite(img_path, frame)
num += 1
count += 1
cap.release()