import cv2

def save_image(addr, image, num):
    address = addr + 'img_' + str(num) + '.jpg'
    print(address)
    cv2.imwrite(address, image)

video_path = r'D:\GameHelper\yolov5-7.0\labelimg\video\1.mp4'
out_path = r'D:/GameHelper/yolov5-7.0/labelimg/images/'

is_all_frame = False  # 是否取所有帧
sta_frame = 1
end_frame = 1000

time_interval = 8  # 时间间隔

# 打开视频文件
videocapture = cv2.VideoCapture(video_path)

# 检查视频文件是否成功打开
if not videocapture.isOpened():
    print("错误:无法打开视频文件。")
    exit()

success, frame = videocapture.read()

frame_count = 0
image_count = 0

while success:
    frame_count += 1
    if frame_count % time_interval == 0:
        if not is_all_frame:
            if sta_frame <= frame_count <= end_frame:
                image_count += 1
                print('保存帧', image_count)
                save_image(out_path, frame, image_count)
            elif frame_count > end_frame:
                break
        else:
            image_count += 1
            print('保存帧', image_count)
            save_image(out_path, frame, image_count)
    success, frame = videocapture.read()

# 释放视频捕获对象
videocapture.release()


微信扫描下方的二维码阅读本文

  • alipay_img
  • wechat_img
届ける言葉を今は育ててる
最后更新于 2024-06-03