随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
复制代码
import cv2
import os
import threading

# 获取所有文件
def getAllFiles(fire_dir):
    filepath_list = []
    for root,folder_names,file_names in os.walk(fire_dir):
        for file_name in file_names:
            file_path = root+os.sep+file_name
            filepath_list.append(file_path)
            print(file_path)
    print(filepath_list)
    return filepath_list

def save_image(image, addr, num, type=".jpg"):
    address = addr + "img_" + str(num) + type
    cv2.imwrite(address, image)

def video_to_frames(video_path, frame_frequency=1,pic_type=".png"):
    wenjianhouzui =  os.path.splitext(video_path)[-1]

    fi = 0

    # 提取视频的频率,每1帧提取一个
    frame_frequency = frame_frequency

    outPutDirName = video_path.strip(wenjianhouzui)+"\\"
    # 如果文件目录不存在则创建目录
    if not os.path.exists(outPutDirName):
        os.makedirs(outPutDirName)

    # 读取视频帧
    camera = cv2.VideoCapture(video_path)

    while True:
        fi = fi + 1
        res, image = camera.read()
        if not res:
            print('not res , not image')
            break
        # 按照设置间隔存储视频帧
        if fi % frame_frequency == 0:
            save_image(image, addr=outPutDirName, num=fi, type=pic_type)
            print('save image: ', fi)

    print('图片提取结束')
    # 释放摄像头设备
    camera.release()

def getAllAndZhuanHua(src,frame_frequency=1):
    filepath_list = getAllFiles(src)
    for f in filepath_list:
        print(f)
        video_to_frames(video_path=f, frame_frequency=frame_frequency)


def getAllAndZhuanHuaDuoXiancheng(src,frame_frequency=1,pic_type=".png"):
    filepath_list = getAllFiles(src)
    for f in filepath_list:
        print(f)
        threading.Thread(target=video_to_frames, args=(f, frame_frequency,pic_type)).start()



if __name__ == '__main__':
    src = r"E:\englishpic\xintu\video"     #路径不要有中文
    frame_frequency = 1000
    pic_type = ".png"
    getAllAndZhuanHuaDuoXiancheng(src=src, frame_frequency=frame_frequency,pic_type=pic_type)
复制代码

 

posted on   大话人生  阅读(416)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示