5

I am trying to create a live streaming camera with my Raspberry Pi and the Pi Camera module. I have chosen to create it with Flask so that I can use the Python API for it. Though when I run this script:

from flask import *
from picamera import PiCamera

app = Flask(__name__)

camera = PiCamera()

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/image.jpg")
def getImage():
     camera.capture("./templates/image.jpg")
     return send_file("./templates/image.jpg")

@app.route("/script.js")
def getTheScript():
     return render_template("script.js")

if __name__ == "__main__":
     app.run(debug=True, host="0.0.0.0")

I get the following error:

mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x131e7b0 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled
Traceback (most recent call last):
  File "/home/pi/Desktop/camera_flask/app.py", line 6, in <module>
    camera = PiCamera()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 417, in __init__
    self._init_preview()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 496, in _init_preview
    self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
  File "/usr/lib/python3/dist-packages/picamera/renderers.py", line 512, in __init__
    self.renderer.connect(source)
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1465, in connect
    self._connection = MMALConnection(source, self.inputs[0])
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1278, in __init__
    prefix="Failed to enable connection")
  File "/usr/lib/python3/dist-packages/picamera/exc.py", line 157, in mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources (other than memory)

My first assumption based on the Failed to enable connection: Out of resources (other than memory) part is that the pi was was not receiving enough power. So I got a 5V - 2.5A micro USB power adapter instead of the 5.1V - 1A adapter that I was using and it still shows this error.

Hope you can help

~ Iwotastic

Iwotastic
  • 53
  • 1
  • 5

3 Answers3

8

I struggled for some time with this exact same problem. I believe that I've solved it, though I have to be honest, I don't understand exactly why it works. The key was turning off debugging in Flask. I got a hint at doing it by the comments on this page Video Streaming with Flask

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=False)

I've verified it by running the exact code with the debug flag set to True (and I get the mmal error) and set to False and everything works as expected. I hope it helps someone

Nick
  • 96
  • 1
  • 2
1

Well i've same issue, add camera.close() and work!

@app.route("/image.jpg")
def getImage():
     camera.capture("./templates/image.jpg")
     camera.close()
     return send_file("./templates/image.jpg")
Robin Gomez
  • 111
  • 2
0

I ended in the same pitfall.

I got a several threads application using opencv and picamera.

Without flask => no problem

With flask launched => out of ressources

Flask alone in a seperated app handling the camera only => nice streaming.

So even with one python process handling flask and threads, the camera crash.

I'm suspecting flask to have clever thread/gevent in background which causes this.

Yoyo
  • 1