opencv讀取視頻并保存圖像的方法
問題重述
實(shí)習(xí)項(xiàng)目要做安全帽目標(biāo)檢測,拿到了公司給的一些視頻數(shù)據(jù),使用Opencv讀取視頻并每隔 1 s 1s 1s存儲一副圖像,下面是一些視頻數(shù)據(jù)
實(shí)現(xiàn)步驟 添加依賴庫
import cv2 import os
定義視頻路徑和圖像存儲路徑
video_path = './未戴安全帽視頻01/' image_path = './images/'
讀取視頻文件
video_files = [i for i in os.listdir(video_path) if i.split('.')[-1] in ['mp4']] len(video_files)
獲取視頻幀
# video_file:'./未戴安全帽視頻01/中建四局-東圍墻5_001_2021-03-22-18-04-28_2021-03-22-18-04-33.mp4', # pic_dir:'中建四局-東圍墻5_001_2021-03-22-18-04-28_2021-03-22-18-04-33' def get_image(video_file, pic_dir): if not os.path.exists(pic_dir): os.makedirs(pic_dir) # cv2讀取視頻文件 vc = cv2.VideoCapture(video_file) index = 0 # 判斷載入的視頻是否可以打開 rval = vc.isOpened() while rval: # 循環(huán)讀取視頻幀 index = index + 1 rval, frame = vc.read() # 每十幀保存一張圖片 if index * 10 % 1 == 0: if rval: # cv2.imshow("capture", frame) save_file = pic_dir + str(index).zfill(5) + '.png' cv2.imwrite(save_file, frame) # 存儲為圖像,保存名為文件夾名 cv2.waitKey(1) else: break vc.release() print("已保存%d" %(index - 1) + "張圖片") # video_file = './未戴安全帽視頻01/01.mp4' # pic_path = '01/' # get_image(video_file, image_path + pic_path)
遍歷視頻文件
for file in video_files: video_file = video_path + file pic_path = image_path + file.replace('.mp4', '/') get_image(video_file, pic_path)
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
完整代碼
import cv2 import os def save_img(): video_path = r'F:\test\3.10' videos = os.listdir(video_path) for video_name in videos: file_name = video_name.split('.')[0] folder_name = video_path +'_'+ file_name os.makedirs(folder_name, exist_ok=True) print(video_path + '/' + video_name) vc = cv2.VideoCapture(video_path + '/' + video_name) # 讀入視頻文件 c = 0 rval = vc.isOpened() while rval: # 循環(huán)讀取視頻幀 c = c + 1 rval, frame = vc.read() if c%10 ==0: pic_path = folder_name + '/' if rval: cv2.imwrite(pic_path + str(c) + '.png', frame) # 存儲為圖像,保存名為文件夾名 cv2.waitKey(1) else: break vc.release() print('save_success') print(folder_name) save_img()
存在問題
讀取路徑問題
問題:讀取視頻結(jié)果顯示沒有打開視頻,檢查發(fā)現(xiàn)視頻路徑錯誤,導(dǎo)致沒有正確打開
解決:可以在讀取之前檢查路徑,即判斷要保存的文件夾是否存在,不存在就創(chuàng)建該文件夾。代碼如下:
if not os.path.exists(path): os.makedirs(path)
中文路徑問題
問題:cv2.imwrite()保存圖像路徑不能存在中文字符,否則無法保存,并且沒有任何提示?。。?/p>
解決:改為英文路徑即可。
最終結(jié)果
到此這篇關(guān)于opencv讀取視頻并保存圖像的方法的文章就介紹到這了,更多相關(guān)opencv讀取視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python標(biāo)準(zhǔn)庫sys庫常用功能詳解
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫sys庫常用功能詳解,sys是Python提供的程序與解釋器交互的標(biāo)準(zhǔn)庫,文章圍繞主題展開相關(guān)介紹,需要的朋友可以參考一下2022-07-07Python標(biāo)準(zhǔn)庫中隱藏的利器(示例詳解)
在命令行中直接使用Python標(biāo)準(zhǔn)庫的模塊,最大的好處就是就是不用寫代碼,就能使用其中的功能,當(dāng)臨時需要一些某些功能的時候,用這種方式會快捷,方便很多,這篇文章主要介紹了Python標(biāo)準(zhǔn)庫中隱藏的利器,需要的朋友可以參考下2023-11-11flask框架配置mysql數(shù)據(jù)庫操作詳解
這篇文章主要介紹了flask框架配置mysql數(shù)據(jù)庫操作,結(jié)合實(shí)例形式詳細(xì)分析了flask框架配置mysql數(shù)據(jù)庫及連接訪問等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11pytorch標(biāo)簽轉(zhuǎn)onehot形式實(shí)例
今天小編就為大家分享一篇pytorch標(biāo)簽轉(zhuǎn)onehot形式實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

python將ip地址轉(zhuǎn)換成整數(shù)的方法

python實(shí)現(xiàn)m3u8格式轉(zhuǎn)換為mp4視頻格式