Python快速從視頻中提取視頻幀的方法詳解
Python快速提取視頻幀(多線程)
今天介紹一種從視頻中抽取視頻幀的方法,由于單線程抽取視頻幀速度較慢,因此這里我們增加了多線程的方法。
1、抽取視頻幀
抽取視頻幀主要使用了 Opencv 模塊。
其中:
camera = cv2.Videocapture( ) ,函數(shù)主要是通過調(diào)用筆記本內(nèi)置攝像頭讀取視頻幀;
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
# 按照設(shè)置間隔存儲視頻幀
if times % frame_frequency == 0:
cv2.imwrite(outPutDirName + '\\' + str(times)+'.jpg', image)
print('圖片提取結(jié)束')
# 釋放攝像頭設(shè)備
camera.release()
2、多線程方法
多線程的應(yīng)用主要使用了 threading 庫。
其中:
threading.Thread( ) 函數(shù)主要用來調(diào)用多線程,其中參數(shù) “target” 是上面用到的函數(shù),參數(shù) “args” 是上面函數(shù)的輸入?yún)?shù)。
其中有關(guān)多線程的詳細(xì)介紹,以及速度提升效果可參考這篇文章
import threading threading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start()
經(jīng)驗(yàn)證,速度提升還是很快的!
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('圖片提取結(jié)束')
camera.release()
if __name__ == "__main__":
input_dir = r'D:\datasets\cow_dataset' # 輸入的video文件夾位置
save_dir = r'E:\relate_code\dataset' # 輸出圖片到當(dāng)前目錄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)
補(bǔ)充
還可以利用Python實(shí)現(xiàn)抽取剔除視頻幀工具
代碼
下面是使用opencv對視頻中間幾幀抽取的方法。
主要的思路是在讀取frame的時候,順便把幀寫下來。
同時如果不是需要抽取剔除的幀,直接continue到下個循環(huán)。
樣例代碼如下,主要按照MP4格式進(jìn)行處理。
#!/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、視頻分辨率寬、視頻分辨率高、視頻需要抽掉的起始幀、視頻需要抽掉的結(jié)束幀。
到此這篇關(guān)于Python快速從視頻中提取視頻幀的方法詳解的文章就介紹到這了,更多相關(guān)Python提取視頻幀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對pandas的dataframe繪圖并保存的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄獙andas的dataframe繪圖并保存的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Python實(shí)現(xiàn)向QQ群成員自動發(fā)郵件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)向QQ群成員自動發(fā)郵件的方法,通過讀取txt文本里的QQ成員數(shù)據(jù)再調(diào)用發(fā)送郵件函數(shù)實(shí)現(xiàn)該功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
Python中的內(nèi)存管理之python list內(nèi)存使用詳解
這篇文章主要介紹了Python中的內(nèi)存管理之python list內(nèi)存使用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
詳解運(yùn)行Python的神器Jupyter Notebook
如果我們想要運(yùn)行Python,就是在Python或者IPython的解釋器環(huán)境中進(jìn)行交互式運(yùn)行,或者程序員最喜歡的編寫.py文件,在文件中編寫python代碼,然后運(yùn)行。如果想寫一篇Python的文章,里面有代碼,還希望代碼在當(dāng)前頁面運(yùn)行,那就是使用我們今天要介紹的Jupyter Notebook。2021-06-06

