五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

opencv讀取視頻并保存圖像的方法

發(fā)布日期:2022-03-21 12:02 | 文章來源:源碼中國

問題重述

​ 實(shí)習(xí)項(xiàng)目要做安全帽目標(biāo)檢測,拿到了公司給的一些視頻數(shù)據(jù),使用Opencv讀取視頻并每隔 1 s 1s 1s存儲(chǔ)一副圖像,下面是一些視頻數(shù)據(jù)

實(shí)現(xiàn)步驟 添加依賴庫

import cv2
import os

定義視頻路徑和圖像存儲(chǔ)路徑

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)  # 存儲(chǔ)為圖像,保存名為文件夾名
 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)  # 存儲(chǔ)為圖像,保存名為文件夾名
  cv2.waitKey(1)
 else:
  break
  vc.release()
  print('save_success')
  print(folder_name)

save_img()

存在問題

讀取路徑問題

問題:讀取視頻結(jié)果顯示沒有打開視頻,檢查發(fā)現(xiàn)視頻路徑錯(cuò)誤,導(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)文章希望大家以后多多支持本站!

香港服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部