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

相关推荐
万邦科技Lafite5 分钟前
利用淘宝开放API接口监控商品状态,掌握第一信息
大数据·python·电商开放平台·开放api接口·淘宝开放平台
Hy行者勇哥2 小时前
Python 与 VS Code 结合操作指南
开发语言·python
大力水手(Popeye)2 小时前
Pytorch——tensor
人工智能·pytorch·python
飞翔的佩奇6 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance7 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博7 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
小曾同学.com8 小时前
【每天学点‘音视频’】前向纠错 和 漏包重传
音视频·fec·前向纠错
胖虎18 小时前
(二十)深入了解 AVFoundation-编辑:使用 AVMutableVideoComposition 实现视频加水印与图层合成(下)——实战篇
音视频·视频编辑·视频添加水印
lxmyzzs8 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
AI浩9 小时前
跟踪不稳定目标:基于外观引导的运动建模实现无人机视频中的鲁棒多目标跟踪
目标跟踪·音视频·无人机