網(wǎng)站首頁 編程語言 正文
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
相關推薦
- 2024-07-18 maven:解決release錯誤:Artifact updating: Repository =‘
- 2022-05-14 解決Linux未啟用網(wǎng)卡的問題_Linux
- 2022-12-14 React?Native設備信息查看調試詳解_React
- 2022-11-02 Python嵌套函數(shù)與nonlocal使用詳細介紹_python
- 2022-05-17 go實現(xiàn)冒泡排序算法_Golang
- 2022-09-06 jquery點擊實現(xiàn)升序降序圖標切換_jquery
- 2022-07-13 Springboot 項目中使用 @RestControllerAdvice 注解不生效
- 2022-08-15 element的form表單中如何一行顯示多el-form-item標簽
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支