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

相关推荐
小玮看世界9 分钟前
[Python]自动驾驶感知与决策练习题 (1-10)
开发语言·python·自动驾驶
Ming_studying17 分钟前
Python + SQLite FTS5 构建本地文档全文搜索器:增量索引、中文检索与高亮
jvm·数据库·python·sqlite
玉鸯30 分钟前
让 Agent 面向用户:AG-UI 协议构建 Agent 前端
前端·python·agent
小白学大数据43 分钟前
把 Modbus 轮询塞进 Trio 的异步循环:内存映射与定时采集实战
网络·python·搜索引擎
FriendshipT1 小时前
Ubuntu 20.04 下使用 Ollama 本地部署 AI 大模型
linux·人工智能·python·深度学习·ubuntu
天天爱吃肉82181 小时前
# 商用车多体动力学实战笔记|第7篇:制动系统与制动热衰退、ABS滞环控制
大数据·人工智能·笔记·python·嵌入式硬件·汽车
bamb001 小时前
一个项目带你入门AI应用开发08
python
鸿芯微控科技1 小时前
MFC质量流量控制器流量不准怎么排查?设定值、反馈值与参考流量对比,附Python代码
c++·python·mfc
音视频牛哥1 小时前
从数字孪生到机器人操控:Android Unity3D下RTMP/RTSP多路低延迟播放实践
android·unity·音视频·unity rtsp播放器·unity rtmp播放器·rtsp player·rtmp player
大尚来也2 小时前
用 Python 自动整理文件、重命名、分类电脑文件夹
开发语言·python