将视频转换为图片并以时间戳命名(制作orbslam3数据集)

import os
import cv2
import datetime

videos_src_path = r'F:/video'
videos_save_path = r'F:/video'

videos = os.listdir(videos_src_path)
i = 0
for each_video in videos:
    print('Video Name :', each_video)
    each_video_name, _ = each_video.split('.')
    os.mkdir(videos_save_path + '/' + each_video_name)

    each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'

    each_video_full_path = os.path.join(videos_src_path, each_video)

    cap = cv2.VideoCapture(each_video_full_path)
    frame_count = 1
    success = True
    while (success):
        success, frame = cap.read()
        cv2.waitKey(10)
        i = i + 1
        if success == True:
            # uuid_str = time.strftime("%Y-%m-%d-%H_%M_%S.%f", time.localtime())  # 图片命名为当前时间
            uuid_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
            uuid_str = str(uuid_str)
            uuid_str = uuid_str.replace(":", "-")  # 去掉python读取时间中的:,文件命名不能有:
            uuid_str = uuid_str.replace(" ", "")  # 去掉python读取时间中空格,文件命名不能空格:
            uuid_str = uuid_str.replace("-", "")
            uuid_str = uuid_str.replace("_", "")
            if i % 1 == 0:
                cv2.imwrite(each_video_save_full_path + uuid_str + '.png', frame)
                # cv2.imwrite(each_video_save_full_path + uuid_str + "%d.png" % frame_count, frame)

        frame_count = frame_count + 1
    print('Final frame:', frame_count)

猜你喜欢

转载自blog.csdn.net/m0_61509658/article/details/126985533
今日推荐