網站首頁 編程語言 正文
Python快速提取視頻幀(多線程)
今天介紹一種從視頻中抽取視頻幀的方法,由于單線程抽取視頻幀速度較慢,因此這里我們增加了多線程的方法。
1、抽取視頻幀
抽取視頻幀主要使用了 Opencv 模塊。
其中:
camera = cv2.Videocapture( ) ,函數主要是通過調用筆記本內置攝像頭讀取視頻幀;
res, image = camera.read( ) 函數主要是按幀讀取視頻,返回值 “res” 是布爾型,成功讀取返回 True,讀取失敗返回 False;
最后用 cv2.imwrite( ) 函數存儲讀取到的視頻幀。
視頻幀抽取方法可參考這篇文章
import cv2
import os
def video_to_frames(video_path, outPutDirName):
times = 0
# 提取視頻的頻率,每1幀提取一個
frame_frequency = 1
# 如果文件目錄不存在則創建目錄
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( ) 函數主要用來調用多線程,其中參數 “target” 是上面用到的函數,參數 “args” 是上面函數的輸入參數。
其中有關多線程的詳細介紹,以及速度提升效果可參考這篇文章
import threading
threading.Thread(target=video_to_frames, args=(video_path, outPutDirName)).start()
經驗證,速度提升還是很快的!
3、整體代碼
import cv2
import os
import threading
def video_to_frames(video_path, outPutDirName):
times = 0
# 提取視頻的頻率,每1幀提取一個
frame_frequency = 1
# 如果文件目錄不存在則創建目錄
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 # 視頻數
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實現抽取剔除視頻幀工具
代碼
下面是使用opencv對視頻中間幾幀抽取的方法。
主要的思路是在讀取frame的時候,順便把幀寫下來。
同時如果不是需要抽取剔除的幀,直接continue到下個循環。
樣例代碼如下,主要按照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方法的入參分別為:輸入視頻地址、輸出視頻地址、視頻fps、視頻分辨率寬、視頻分辨率高、視頻需要抽掉的起始幀、視頻需要抽掉的結束幀。
原文鏈接:https://blog.csdn.net/WYKB_Mr_Q/article/details/124929081
相關推薦
- 2024-02-25 前后端的時間問題
- 2022-05-23 一起來學習Python的列表_python
- 2022-06-28 nginx使用內置模塊配置限速限流的方法實例_nginx
- 2022-05-29 Android?老生常談LayoutInflater的新認知_Android
- 2022-08-26 Redis哨兵模式實現一主二從三哨兵_Redis
- 2022-12-13 Android之rk3588?開發環境準備及問題解決方法_Android
- 2024-03-08 jdk版本不兼容 Error creating bean with name ‘springSecu
- 2022-11-04 ASP.NET?MVC使用jQuery的Load方法加載靜態頁面及注意事項_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支