python opencv獲取攝像頭視頻流


工作需要獲取攝像頭的視頻流,原本通過sdk 接入很穩定,也沒有延遲,但后來需要改造成python,

通過opencv 處理來獲取,記錄下例子來學習

 

import cv2
import time

def getTime():
return time.strftime("%Y%m%d%H%M%S", time.localtime())

if __name__ == "__main__":

video_full_path = "RTSP流"
cap = cv2.VideoCapture(video_full_path)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
out_path = 'test/'
print(cap.isOpened())
frame_count = 1
n = 1
success = True
# 攝像頭fps=25 逐幀讀取,即每秒25張
while True:
if(success):
success, frame = cap.read()
print('Read a new frame: ', success)
print('流狀態'+str(success))
params = []
#params.append(cv.CV_IMWRITE_PXM_BINARY)
params.append(1)

#if (n % 25 == 0): # 每隔5幀保存一張圖片
#cv2.imwrite(out_path + getTime() + "_%d.jpg" % frame_count, frame, params)
#frame_count +=1
#print('完成截圖')
#n += 1

else:
print('無法獲取視頻流,嘗試重新連接,重連次數')
cap = cv2.VideoCapture(video_full_path)
steamingstate = cap.isOpened()
print('連接狀態'+str(steamingstate))
success = steamingstate
cap.release()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM