视频批量转化为图片

视频批量转化为图片

本代码使用到了CV2库

下面介绍如何安装opencv-python库
win+r打开运行窗口,输入cmd打开命令行窗口

activate 
conda activate
conda activate tf-pt #激活之前创建的虚拟环境,tf-pt是博主自己创建的虚拟环境name
pip intall opencv-python #安装opencv库
import os
import cv2


def video2img(video_folder, img_folder, frequency=10):
    video_files = os.listdir(video_folder)
    for video_file in video_files:
        print("{}图片提取中...".format(video_file))
        times = 0
        img_idx = 0
        video_file_name = video_file.split(".")[0]
        video_file_path = os.path.join(video_folder, video_file)
        img_sub_folder = os.path.join(img_folder, video_file_name)
        if not os.path.exists(img_sub_folder):
            os.makedirs(img_sub_folder)
            camera = cv2.VideoCapture(video_file_path)
            while True:
                times += 1
                res, image = camera.read()
                if not res:
                    print('EOF')
                    break
                if times % frequency == 0:
                    img_name = video_file_name + "_" + str(img_idx) + ".jpg"
                    img_idx = img_idx + 1
                    img_save_path = os.path.join(img_sub_folder, img_name)
                    cv2.imwrite(img_save_path, image)
            print('{}文件图片提取结束:{}张'.format(video_file, img_idx))
            camera.release()


if __name__ == '__main__':
    video2img(video_folder="E:/Test_Mov/Mov", img_folder="E:/Test_Mov/Train", frequency=10)
    #此处放置视频文件路径(全英文)/照片输出文件路径(全英文)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值