Example explanation 14-WiFi-Shield->mjpeg_streamer video stream

# MJPEG视频流
#
# 这个例子展示了如何将MJPEG流式传输到火狐或者谷歌浏览器,Firefox和Android上的MJpegViewer应用程序已经过测试。
# 连接到从ifconfig打印出的IP地址/端口以查看流。

import sensor
import time
import network
import socket

SSID = ""  # Network SSID
KEY = ""  # Network key
HOST = ""  # 使用第一个可用的端口
PORT = 8080  # 任意非特权端口

# 初始化感光元件
sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.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())

# 创建服务器socket
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()
    # 将客户端套接字超时设置为5s
    client.settimeout(5.0)
    print("Connected to " + addr[0] + ":" + str(addr[1]))

    # 从客户端读取请求
    data = client.recv(1024)
    # 这里解析客户端请求

    # 发送多部分header
    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
    clock = time.clock()

    # 开始流式图像
    # 注意:禁用IDE预览以增加流的FPS帧率。
    while True:
        clock.tick()  # 跟踪快照()之间经过的毫秒数。
        frame = sensor.snapshot()
        cframe = frame.compressed(quality=35)
        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)

results matching ""

    No results matching ""