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流,摄像机
0
投稿

猜你喜欢

  • Python中文竖排显示的方法

    2022-12-31 20:26:03
  • 使用pyecharts生成Echarts网页的实例

    2023-02-22 10:19:42
  • 使用Python实现控制摄像头的方法详解

    2023-01-15 14:38:12
  • python 如何利用chinese_calendar 获取上一个工作日日期

    2022-01-12 12:07:32
  • 切换路径在Jupyter里调用本地文件的操作

    2022-05-24 15:03:54
  • mysql数据表的基本操作之表结构操作,字段操作实例分析

    2024-01-17 18:15:54
  • python 包之 threading 多线程

    2021-06-03 19:54:41
  • MySQL中字符串与Num类型拼接报错的解决方法

    2024-01-27 11:32:00
  • CSS图片代码效果汇总

    2008-09-04 12:14:00
  • python tkinter库实现气泡屏保和锁屏

    2021-09-17 22:37:57
  • SQL Join的一些总结(实例)

    2024-01-14 04:28:16
  • python 实现删除文件或文件夹实例详解

    2021-03-23 03:16:37
  • windows 64位下mysql8.0.25安装配置教程(最详细!)

    2024-01-22 17:36:27
  • JS实现仿Windows经典风格的选项卡Tab切换代码

    2023-08-25 05:33:54
  • 元素层叠级别及z-index剖析

    2008-07-22 12:03:00
  • javabean servlet jsp实现分页功能代码解析

    2023-06-13 15:21:24
  • 是时候不用考虑基于字体大小(em)的设计了

    2009-10-24 13:25:00
  • mysql 存储过程的问题

    2024-01-29 09:05:15
  • TensorFlow实现模型评估

    2023-10-15 22:36:51
  • 深度学习入门之Pytorch 数据增强的实现

    2021-04-05 22:26:07
  • asp之家 网络编程 m.aspxhome.com