python3从网络摄像机解析mjpeg http流的示例
作者:enumx 时间:2021-01-12 09:00:54
前言
网络摄像头的视频流解析直接使用通过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))
来源:https://www.cnblogs.com/enumx/p/12392248.html
标签:python,mjpeg,http流,摄像机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python3中int(整型)的使用教程
2021-12-29 19:51:31
在PyCharm下使用 ipython 交互式编程的方法
2021-05-06 11:12:01
Python3简单实现串口通信的方法
2022-03-10 03:39:53
python每次处理固定个数的字符的方法总结
2021-06-23 15:45:48
Python base64编码解码实例
2022-06-11 07:08:27
PHP和JAVA中的重载(overload)和覆盖(override) 介绍
2023-11-01 00:14:33
golang中sync.Map并发创建、读取问题实战记录
2023-07-16 12:23:27
Python纯代码通过神经网络实现线性回归的拟合方式
2022-07-12 23:54:59
![](https://img.aspxhome.com/file/2023/0/84880_0s.png)
利用Pygame绘制圆环的示例代码
2022-04-02 12:55:05
![](https://img.aspxhome.com/file/2023/5/98985_0s.png)
对python pandas 画移动平均线的方法详解
2023-11-16 04:21:33
再谈 Web 字体的现状与未来[译]
2009-11-24 13:55:00
![](https://img.aspxhome.com/file/UploadPic/200911/24/webfonts_example-thumb-640xauto-7429-10s.png)
python 中pyqt5 树节点点击实现多窗口切换问题
2021-07-28 06:19:39
![](https://img.aspxhome.com/file/2023/8/101098_0s.png)
Thinkphp5文件包含漏洞解析
2023-07-01 19:42:51
![](https://img.aspxhome.com/file/2023/5/55515_0s.jpg)
numpy工程实践之np.savetxt()存储数据
2023-06-19 07:33:11
![](https://img.aspxhome.com/file/2023/5/59655_0s.png)
ASP+XML制作菜单管理
2008-05-19 12:38:00
Python正则表达式中的'r'用法总结
2021-08-22 23:16:34
![](https://img.aspxhome.com/file/2023/2/109852_0s.png)
Python实现新年愿望代码雨效果
2022-08-02 00:52:35
![](https://img.aspxhome.com/file/2023/6/72186_0s.jpg)
python Tkinter版学生管理系统
2021-03-11 11:07:48
![](https://img.aspxhome.com/file/2023/9/75379_0s.jpg)
python+opencv实现高斯平滑滤波
2023-04-02 15:56:55
![](https://img.aspxhome.com/file/2023/1/82531_0s.jpg)
js+php实现静态页面实时调用用户登陆状态的方法
2023-10-09 22:32:45