日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python快速從視頻中提取視頻幀的方法詳解_python

作者:WYKB_Mr_Q ? 更新時間: 2022-08-23 編程語言

Python快速提取視頻幀(多線程)

今天介紹一種從視頻中抽取視頻幀的方法,由于單線程抽取視頻幀速度較慢,因此這里我們增加了多線程的方法。

1、抽取視頻幀

抽取視頻幀主要使用了 Opencv 模塊。

其中:

camera = cv2.Videocapture( ) ,函數(shù)主要是通過調用筆記本內置攝像頭讀取視頻幀;

res, image = camera.read( ) 函數(shù)主要是按幀讀取視頻,返回值 “res” 是布爾型,成功讀取返回 True,讀取失敗返回 False;

最后用 cv2.imwrite( ) 函數(shù)存儲讀取到的視頻幀。

視頻幀抽取方法可參考這篇文章

import cv2
import os

def video_to_frames(video_path, outPutDirName):
    times = 0
    
    # 提取視頻的頻率,每1幀提取一個
    frame_frequency = 1
    
	# 如果文件目錄不存在則創(chuàng)建目錄
    if not os.path.exists(outPutDirName):
        os.makedirs(outPutDirName)
        
    # 讀取視頻幀
    camera = cv2.VideoCapture(video_path)
    
    while True:
        times = times + 1
        res, image = camera.read()
        if not res:
            print('not res , not image')
            break
        # 按照設置間隔存儲視頻幀
        if times % frame_frequency == 0:
            cv2.imwrite(outPutDirName + '\\' + str(times)+'.jpg', image)
            
    print('圖片提取結束')
    # 釋放攝像頭設備
    camera.release()

2、多線程方法

多線程的應用主要使用了 threading 庫。

其中:

threading.Thread( ) 函數(shù)主要用來調用多線程,其中參數(shù) “target” 是上面用到的函數(shù),參數(shù) “args” 是上面函數(shù)的輸入?yún)?shù)。

其中有關多線程的詳細介紹,以及速度提升效果可參考這篇文章

import threading
threading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start()

經(jīng)驗證,速度提升還是很快的!

3、整體代碼

import cv2
import os
import threading

def video_to_frames(video_path, outPutDirName):
    times = 0
    
    # 提取視頻的頻率,每1幀提取一個
    frame_frequency = 1
    
	# 如果文件目錄不存在則創(chuàng)建目錄
    if not os.path.exists(outPutDirName):
        os.makedirs(outPutDirName)
        
    # 讀取視頻幀
    camera = cv2.VideoCapture(video_path)
    
    while True:
        times = times + 1
        res, image = camera.read()
        if not res:
            print('not res , not image')
            break
        if times % frame_frequency == 0:
            cv2.imwrite(outPutDirName + '\\' + str(times)+'.jpg', image)
            
    print('圖片提取結束')
    camera.release()


if __name__ == "__main__":
    input_dir = r'D:\datasets\cow_dataset'       # 輸入的video文件夾位置
    save_dir = r'E:\relate_code\dataset'         # 輸出圖片到當前目錄video文件夾下
    count = 0   # 視頻數(shù)
    for video_name in os.listdir(input_dir):
        video_path = os.path.join(input_dir, video_name)
        outPutDirName = os.path.join(save_dir, video_name[:-4])
        threading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start()
        count = count + 1
        print("%s th video has been finished!" % count)

補充

還可以利用Python實現(xiàn)抽取剔除視頻幀工具

代碼

下面是使用opencv對視頻中間幾幀抽取的方法。

主要的思路是在讀取frame的時候,順便把幀寫下來。

同時如果不是需要抽取剔除的幀,直接continue到下個循環(huán)。

樣例代碼如下,主要按照MP4格式進行處理。

#!/user/bin/env python
# coding=utf-8
"""
@project : csdn-pro
@author  : 劍客阿良_ALiang
@file   : test.py
@ide    : PyCharm
@time   : 2022-06-30 17:55:48
"""
 
import cv2
 
 
# 視頻抽幀
def extract_frame(video_path: str, result_path: str, fps, weight, height, start, end):
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    videoWriter = cv2.VideoWriter(result_path, fourcc, fps, (weight, height))
    vc = cv2.VideoCapture(video_path)
    if vc.isOpened():
        ret, frame = vc.read()
    else:
        ret = False
    count = 0  # count the number of pictures
    while ret:
        ret, frame = vc.read()
        if start <= count <= end:
            count += 1
            continue
        else:
            videoWriter.write(frame)
            count += 1
    print(count)
    videoWriter.release()
    vc.release()
 
 
if __name__ == '__main__':
    extract_frame('C:\\Users\\xxx\\Desktop\\123.mp4', 'C:\\Users\\xxx\\Desktop\\114.mp4', 25, 640, 368, 119, 125)

注意

1、extract_frame方法的入?yún)⒎謩e為:輸入視頻地址、輸出視頻地址、視頻fps、視頻分辨率寬、視頻分辨率高、視頻需要抽掉的起始幀、視頻需要抽掉的結束幀。

原文鏈接:https://blog.csdn.net/WYKB_Mr_Q/article/details/124929081

欄目分類
最近更新