python3從網(wǎng)絡攝像機解析mjpeg http流的示例
前言
網(wǎng)絡攝像頭的視頻流解析直接使用通過http的Mjpeg是具有邊界幀信息的multipart / x-mixed-replace,而jpeg數(shù)據(jù)只是以二進制形式發(fā)送。因此,實際上不需要關心HTTP協(xié)議標頭。所有jpeg幀均以marker開頭,0xff 0xd8并以結尾0xff 0xd9。因此,上面的代碼從http流中提取了此類幀,并將其一一解碼。像下面
...(http)0xff 0xd8 --|[jpeg data] |--this part is extracted and decoded0xff 0xd9 --|...(http)0xff 0xd8 --|[jpeg data] |--this part is extracted and decoded0xff 0xd9 --|...(http)
如果圖像的獲取是從tcp網(wǎng)絡中傳輸?shù)奖镜剡M行解析需要對bytes類型數(shù)據(jù)進行解碼
在使用OpenCV直接調用網(wǎng)絡攝像頭時可能會出現(xiàn)
Cam not found
這時候就需要下面這種辦法
代碼: 幀解析
import cv2cap = cv2.VideoCapture(’http://localhost:8080/frame.mjpg’) while True: ret, frame = cap.read() print(frame) if ret == True: cv2.imshow(’Video’, frame) if cv2.waitKey(1) == 27: exit(0)
視頻流解析
import cv2import requestsimport numpy as np r = requests.get(’http://192.168.1.xx/mjpeg.cgi’, auth=(’user’, ’password’), stream=True)if(r.status_code == 200): bytes = bytes() for chunk in r.iter_content(chunk_size=1024): bytes += chunk a = bytes.find(b’xffxd8’) b = bytes.find(b’xffxd9’) if a != -1 and b != -1: jpg = bytes[a:b+2] bytes = bytes[b+2:] i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR) cv2.imshow(’i’, i) if cv2.waitKey(1) == 27:exit(0)else: print('Received unexpected status code {}'.format(r.status_code))
以上就是python3從網(wǎng)絡攝像機解析mjpeg http流的示例的詳細內容,更多關于python 解析mjpeg http流的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. Python調用接口合并Excel表代碼實例2. 一文透徹詳解.NET框架類型系統(tǒng)設計要點3. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁4. 通過CSS數(shù)學函數(shù)實現(xiàn)動畫特效5. .net如何優(yōu)雅的使用EFCore實例詳解6. ASP.NET MVC實現(xiàn)橫向展示購物車7. 通過Ajax方式綁定select選項數(shù)據(jù)的實例8. ajax動態(tài)加載json數(shù)據(jù)并詳細解析9. Python快速將ppt制作成配音視頻課件的操作方法10. ASP.Net Core對USB攝像頭進行截圖
