From ad585caccb00f62dead2bd70546ad28963cd2088 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 30 Aug 2016 00:58:02 +0200 Subject: [PATCH] Update mjpeg streaming example. --- usr/examples/14-WiFi-Shield/mjpeg_streamer.py | 62 +++++++++++-------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/usr/examples/14-WiFi-Shield/mjpeg_streamer.py b/usr/examples/14-WiFi-Shield/mjpeg_streamer.py index a2bf8c783..307184d8d 100644 --- a/usr/examples/14-WiFi-Shield/mjpeg_streamer.py +++ b/usr/examples/14-WiFi-Shield/mjpeg_streamer.py @@ -4,12 +4,12 @@ # (IE and Chrome do not work). Just input your network SSID and KEY and then # connect to the IP address/port printed out from ifconfig. -import sensor, image, time, network, usocket +import sensor, image, time, network, usocket, sys SSID='' # Network SSID KEY='' # Network key HOST = '' # Use first available interface -PORT = 8000 # Arbitrary non-privileged port +PORT = 8080 # Arbitrary non-privileged port # Reset sensor sensor.reset() @@ -19,7 +19,7 @@ sensor.set_contrast(1) sensor.set_brightness(1) sensor.set_saturation(1) sensor.set_gainceiling(16) -sensor.set_framesize(sensor.QVGA) +sensor.set_framesize(sensor.QQVGA) sensor.set_pixformat(sensor.GRAYSCALE) # Init wlan module and connect to network @@ -40,32 +40,40 @@ s.listen(5) # Set timeout to 1s s.settimeout(1.0) -print ('Waiting for connections..') -client, addr = s.accept() -print ('Connected to ' + addr[0] + ':' + str(addr[1])) +def start_streaming(s): + print ('Waiting for connections..') + client, addr = s.accept() + print ('Connected to ' + addr[0] + ':' + str(addr[1])) -# Read request from client -data = client.recv(1024) + # Read request from client + data = client.recv(1024) + # Should parse client request here -# Should parse client request here + # Send multipart header + client.send("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") -# Send multipart header -client.send("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() + + # Start streaming images + # NOTE: Disable IDE preview to increase streaming FPS. + while (True): + clock.tick() # Track elapsed milliseconds between snapshots(). + 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.send(header) + client.send(cframe) -# FPS clock -clock = time.clock() -# Start streaming images while (True): - clock.tick() # Track elapsed milliseconds between snapshots(). - frame = sensor.snapshot() - cframe = frame.compressed(quality=90) - client.send("\r\n--openmv\r\n" \ - "Content-Type: image/jpeg\r\n"\ - "Content-Length:"+str(cframe.size())+"\r\n\r\n") - client.send(cframe) - print(clock.fps()) -client.close() + try: + start_streaming(s) + except OSError as e: + print("socket error: ", e) + #sys.print_exception(e)