video_to_images.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import argparse
  2. import os
  3. import uuid
  4. import datetime
  5. import cv2
  6. from tqdm import tqdm
  7. def work(video_folder, output_folder, interval):
  8. if not os.path.exists(output_folder):
  9. os.makedirs(output_folder)
  10. video_list = os.listdir(video_folder)
  11. num = 1
  12. cnt = 1
  13. for video in tqdm(video_list):
  14. video_path = video_folder + '/' + video
  15. vid = cv2.VideoCapture(video_path)
  16. time = datetime.datetime.now()
  17. t = str(time.year).zfill(4) + '-' + str(time.month).zfill(2) + '-' + str(time.day).zfill(2)
  18. while vid.isOpened():
  19. is_read, frame = vid.read()
  20. if is_read:
  21. if num % interval == 0:
  22. file_name = '%04d' % cnt
  23. cv2.imwrite(output_folder + '/' + 'kdan_' + t + '_' + str(file_name) + '_' + str(uuid.uuid1())[0:4] + '.jpg', frame)
  24. # 00000111.jpg 代表第111帧
  25. cv2.waitKey(1)
  26. cnt += 1
  27. num += 1
  28. else:
  29. break
  30. if __name__ == '__main__':
  31. parser = argparse.ArgumentParser()
  32. parser.add_argument('--video_folder', type=str, default='./video/01')
  33. parser.add_argument('--output_folder', type=str, default='./video_frame')
  34. parser.add_argument('--interval', type=int, default=10, help='帧率间隔')
  35. args = parser.parse_args()
  36. work(args.video_folder, args.output_folder, args.interval)