I have Pi B2 model in which i have Pi Cam module and i have python flask app which i have followed from following miguelgrinberg blog http://blog.miguelgrinberg.com/post/video-streaming-with-flask
https://github.com/miguelgrinberg/flask-video-streaming
and i have added gevent as web server to serve multi thread camera streaming connection and modified script is as following
#!/usr/bin/env python
from flask import Flask, render_template, Response
from gevent import monkey; monkey.patch_all()
# Raspberry Pi camera module (requires picamera package)
from camera_pi import Camera
app = Flask(__name__)
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
monkey.patch_all()
from gevent.wsgi import WSGIServer
WSGIServer(('', 5000),app).serve_forever()
#app.run(host='0.0.0.0', debug=True, threaded=True)
Every time i start server and try to access it using the client i got this error message
Along with that server stop streaming mjpeg after some 30 min or more time after it start. I also commented out section where there is no connection for next 10 sec
In Camera_pi.py file
# if there hasn't been any clients asking for frames in**
# the last 10 seconds stop the thread
#if time.time() - cls.last_access > 10000:
# break
Error Message after client connect to server even if this message appears still i can view streaming but not more than 30 min or more after 30 + min frame in browser freezes and refresh also does not work i have to ctrl+c python app.py and start it again:
(streampi)pi@niravpi ~/svsapp/streampi $ python app.py
Traceback (most recent call last):
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
self.run_application()
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 495, in run_application
self.process_result()
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 486, in process_result
self.write(data)
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 376, in write
self._write(data)
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 369, in _write
self._sendall(data)
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 355, in _sendall
self.socket.sendall(data)
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/socket.py", line 460, in sendall
data_sent += self.send(_get_memory(data, data_sent), flags)
File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/socket.py", line 445, in send
return sock.send(data, flags)
error: [Errno 32] Broken pipe
{'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',
'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,hi;q=0.6',
'HTTP_CONNECTION': 'keep-alive',
'HTTP_HOST': '192.168.1.6:5000',
'HTTP_UPGRADE_INSECURE_REQUESTS': '1',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
'PATH_INFO': '/video_feed',
'QUERY_STRING': '',
'REMOTE_ADDR': '192.168.1.4',
'REMOTE_PORT': '55311',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'SERVER_NAME': 'niravpi',
'SERVER_PORT': '5000',
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
'werkzeug.request': None,
'wsgi.errors': <open file '<stderr>', mode 'w' at 0x76d850d0>,
'wsgi.input': <gevent.pywsgi.Input object at 0x763cb5d0>,
'wsgi.multiprocess': False,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)} failed with error
192.168.1.4 - - [2015-10-31 14:44:53] "GET /video_feed HTTP/1.1" socket 479452 5.199595
The broken pipe error indicates that Flask tried to write to a socket that was closed from the other end, which is the side of the client.
You will get this error if you have the streaming going and suddenly close the browser window. In such a case the error is harmless, it just causes the thread that was serving that client to stop, which is what you want when the client goes away.
You will also get this error on any kind of connection interruptions. To make the streaming more robust you would need to put a system in place to check that a client connection is alive, and when it isn't, maybe trigger a reload of the image, so that the streaming is reestablished.
To check that a client remains connected, you can record the timestamp when a client fetched the last video frame. Then an Ajax call sent by the client can check how long ago a frame was retrieved, and if this is longer than some threshold, then you declare the connection broken and trigger a refresh of the image from the client.