栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

open cv视频抽帧

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

open cv视频抽帧

import cv2
import glob
import os
from tqdm import tqdm

is_stati = True

if __name__ == '__main__':
    if is_stati:
        path = os.path.join(os.getcwd(), 'result')
        path = path+'/*.jpg'
        pic_list = glob.glob(path)
        print("该目录下总共有:", len(pic_list), '张图片')
    else:
        if not os.path.exists('./result'):
            os.mkdir('./result')
        video_list = glob.glob(r'./*.mp4')
        for video in tqdm(video_list):
            name = video.split('/')[-1]
            cap = cv2.VideoCapture(video)
            name_list = name.split('-')
            assert len(name_list) == 3
            count = 1
            num = 1
            while True:
                ret, frame = cap.read()
                if not ret:
                    break
                if name_list[1] == '常规场景':
                    if count % 5 == 0 and num <= 30: # 抽取视频中帧数为5的倍数图片,最多只抽取30张
                        temp = name_list[0] + '_' + name_list[1] + str(count) + '_' + name_list[2]
                        img_path = os.path.join(os.getcwd(), 'result', temp)
                        img_path = img_path[:-3] + 'jpg'
                        cv2.imwrite(img_path, frame)
                        num += 1
                else:
                    if count % 30 == 0 and num <= 5: # 抽取视频中帧数为30的倍数图片,最多只抽取5张
                        temp = name_list[0] + '_' + name_list[1] + str(count) + '_' + name_list[2]
                        img_path = os.path.join(os.getcwd(), 'result', temp)
                        img_path = img_path[:-3] + 'jpg'
                        cv2.imwrite(img_path, frame)
                        num += 1
                count += 1
            cap.release()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/280534.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号