python目标检测将视频按照帧率切除成图片

python目标检测将视频按照帧率切除成图片

python目标检测将视频按照帧率切除成图片,并且允许放入多个多个视频

完整代码如下:

bash 复制代码
import os
import cv2

class VideoSplit:
    """
    将视频分帧为图片
    source_path: 视频文件存储地址
    result_path: 图片结果文件保存地址
    frame: 帧率,每frame帧保存一张图片
    """

    def __init__(self, source_path, result_path, frame=10):
        self.source_path = source_path
        if not os.path.exists(self.source_path):
            raise Exception("源文件路径不存在!")

        self.result_path = result_path
        self.frame = frame
        if not os.path.exists(self.result_path):
            os.makedirs(self.result_path)
            print("创建文件夹{},".format(self.result_path))

    def split_video(self):
        video_list = os.listdir(self.source_path)
        for i, name in enumerate(video_list):
            video_list[i] = os.path.join(self.source_path, name)
            basename = name.split('.')[0]
            video_result_path = os.path.join(self.result_path, basename)
            if not os.path.exists(video_result_path):
                os.makedirs(video_result_path)
                print("创建子文件夹{},".format(basename))
            cap = cv2.VideoCapture(video_list[i])
            print("视频{}开始分帧...".format(name))

            sum = 0
            i = 0
            while True:
                ret, frame = cap.read()
                if not ret:
                    break
                sum += 1
                # 保存图片
                if sum == self.frame:
                    sum = 0
                    i += 1
                    imgname = basename + '_' + str(i) + '.jpg'
                    imgPath = os.path.join(video_result_path, imgname)
                    cv2.imwrite(imgPath, frame)
                    print(imgname)
            print("{}视频文件提取完成".format(basename))
        print("完成")

if __name__ == "__main__":
    source_path = r'C:\Users\video'
    result_path = r'C:\Users\result'
    tst = VideoSplit(source_path, result_path)
    tst.split_video()

注意:尽量手动创建两个文件夹video、result

相关推荐
零基础1237 小时前
DeepSeek V4.1 Flash (Batch) 的性能测试与应用
经验分享·python·语言模型·vllm
Yyyyyy~7 小时前
【Anaconda】安装
人工智能·python
LOVE️YOU7 小时前
Python 函数名、函数对象与“把函数作为参数传递”
开发语言·python
YOLO数据集集合7 小时前
红外无人机与鸟类目标检测数据集 | 红外检测 无人机识别 鸟类检测 低空安防 反无人机 yolov11模型9151期
yolo·目标检测·无人机
2601_957883848 小时前
2026年9月:雷神笔记本售后相关资讯
python·电脑
天赐范式8 小时前
天赐范式第185天:让漂变开始定量——扫N看选择主导边界
python·信噪比·数字生命·天赐范式·动态运行时·种群大小·遗传漂变
Jo乔戈里8 小时前
免费本地搜图软件
图像处理·python·搜索引擎·ai
for_ever_love__9 小时前
字符串处理——f-string、切片与正则,清洗文本的第一把刀
python·大模型·虚拟环境
夜晚回家9 小时前
Python 零基础入门 | 输入的数字为什么不能直接算
开发语言·python
打工仔折腾 AI9 小时前
把AI Agent托管在家用电脑:UU远程终端与端口映射实测记录
人工智能·后端·python·langchain·ai agent 实战