mjpeg_streamer.py
N6、RT、AE等型号
以下代码使用 network.WLAN,适用于N6、RT、AE等使用内置WiFi的型号。
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# MJPEG流媒体
#
# 此示例展示了如何向FIREFOX浏览器进行MJPEG流媒体传输
# Chrome、Firefox和Android上的MJpegViewer应用已通过测试。
# 连接到ifconfig打印出的IP地址/端口以查看流媒体。
import csi
import time
import network
import socket
SSID = "" # 网络 SSID
KEY = "" # 网络密钥
HOST = "" # 使用第一个可用的接口
PORT = 8080 # 任意非特权端口
# 初始化传感器
csi0 = csi.CSI()
csi0.reset()
csi0.framesize(csi.QVGA)
csi0.pixformat(csi.RGB565)
# 初始化wlan模块并连接到网络
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)
while not wlan.isconnected():
print('Trying to connect to "{:s}"...'.format(SSID))
time.sleep_ms(1000)
# 我们现在应该通过DHCP获得一个有效的IP地址
print("WiFi Connected ", wlan.ifconfig())
# 创建服务器套接字
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
# 绑定并监听
s.bind([HOST, PORT])
s.listen(5)
# 将服务器套接字设置为阻塞模式
s.setblocking(True)
def start_streaming(s):
print("Waiting for connections..")
client, addr = s.accept()
# 将客户端套接字超时设置为5秒
client.settimeout(5.0)
print("Connected to " + addr[0] + ":" + str(addr[1]))
# 从客户端读取请求
data = client.recv(1024)
# 应在此处解析客户端请求
# 发送多部分头部
client.sendall(
"HTTP/1.1 200 OK\r\n"
"Server: OpenMV\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n"
"Cache-Control: no-cache\r\n"
"Pragma: no-cache\r\n\r\n"
)
# FPS时钟
clock = time.clock()
# 开始流式传输图像
# 注意:禁用IDE预览以提高流媒体FPS。
while True:
clock.tick() # 跟踪snapshots()之间经过的毫秒数。
frame = csi0.snapshot()
cframe = frame.to_jpeg(quality=35, copy=True)
header = (
"\r\n--openmv\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length:" + str(cframe.size()) + "\r\n\r\n"
)
client.sendall(header)
client.sendall(cframe)
print(clock.fps())
while True:
try:
start_streaming(s)
except OSError as e:
print("socket error: ", e)
# sys.print_exception(e)
H7、H7 Plus + WINC1500
以下代码使用 network.WINC,专门适用于H7、H7 Plus搭配WINC1500 WiFi扩展板。
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# OpenMV Cam H7 / H7 Plus + WINC1500 WiFi扩展板
# 站模式下的MJPEG流媒体传输(支持WPA/WPA2)。
#
# 请在下方设置SSID和KEY。将查看设备连接到同一路由器,
# 然后在浏览器中打开OpenMV IDE终端打印出的URL。
import csi
import network
import socket
import time
SSID = "YOUR_WIFI_NAME"
KEY = "YOUR_WIFI_PASSWORD"
HOST = ""
PORT = 8080
# 初始化摄像头。
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)
# H7/H7 Plus使用外接的WINC1500扩展板。
# WINC1500加入其他接入点时支持WPA/WPA2。
print('Connecting to "{}"...'.format(SSID))
wlan = network.WINC()
wlan.connect(SSID, KEY, security=network.WINC.WPA_PSK)
if not wlan.isconnected():
raise OSError("WiFi connection failed")
IP = wlan.ifconfig()[0]
print("WiFi connected:", wlan.ifconfig())
print("MJPEG URL: http://{}:{}/".format(IP, PORT))
def stream_mjpeg(client, addr):
client.settimeout(5.0)
# 读取浏览器的HTTP请求。
request = client.recv(1024)
if not request:
return
print("HTTP client:", addr)
client.sendall(
b"HTTP/1.1 200 OK\r\n"
b"Server: OpenMV\r\n"
b"Connection: close\r\n"
b"Content-Type: multipart/x-mixed-replace; boundary=openmv\r\n"
b"Cache-Control: no-cache, no-store, must-revalidate\r\n"
b"Pragma: no-cache\r\n"
b"Expires: 0\r\n\r\n"
)
clock = time.clock()
frame_count = 0
while True:
clock.tick()
frame = csi0.snapshot()
jpeg = frame.to_jpeg(quality=35, copy=True)
part_header = (
"\r\n--openmv\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Length: {}\r\n\r\n".format(jpeg.size())
)
client.sendall(part_header.encode())
client.sendall(jpeg)
frame_count += 1
if frame_count % 30 == 0:
print("FPS:", clock.fps())
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((HOST, PORT))
server.listen(1)
# 较短的超时时间可以在没有浏览器连接时保持USB调试器的响应。
server.settimeout(1.0)
while True:
client = None
try:
client, addr = server.accept()
except OSError:
time.sleep_ms(20)
continue
try:
stream_mjpeg(client, addr)
except OSError as error:
print("Client disconnected:", error)
finally:
if client:
client.close()