python读取网络摄像头的帧_python3从网络摄像机解析mjpeg http流的示例

本文介绍了如何使用Python从网络摄像机通过HTTP获取MJPEG流,并对其进行解析和显示。示例代码包括使用`cv2.VideoCapture`以及直接处理HTTP响应流的方法。
摘要由CSDN通过智能技术生成

前言

网络摄像头的视频流解析直接使用通过http的Mjpeg是具有边界帧信息的multipart / x-mixed-replace,而jpeg数据只是以二进制形式发送。因此,实际上不需要关心HTTP协议标头。所有jpeg帧均以marker开头,0xff 0xd8并以结尾0xff 0xd9。因此,上面的代码从http流中提取了此类帧,并将其一一解码。像下面

...(http)

0xff 0xd8 --|

[jpeg data] |--this part is extracted and decoded

0xff 0xd9 --|

...(http)

0xff 0xd8 --|

[jpeg data] |--this part is extracted and decoded

0xff 0xd9 --|

...(http)

如果图像的获取是从tcp网络中传输到本地进行解析需要对bytes类型数据进行解码

在使用OpenCV直接调用网络摄像头时可能会出现

Cam not found

这时候就需要下面这种办法

代码:

帧解析

import cv2

cap = 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 cv2

import requests

import 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'\xff\xd8')

b = bytes.find(b'\xff\xd9')

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从网络摄像机解析mjpeg http流的示例的详细内容,更多关于python 解析mjpeg http流的资料请关注我们其它相关文章!

本文标题: python3从网络摄像机解析mjpeg http流的示例

本文地址: http://www.cppcns.com/jiaoben/python/364155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值