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
  • Try gooling for "picamera mmal out of resources". – jogco Jul 18 '16 at 21:47
  • Looks very similar to: http://raspberrypi.stackexchange.com/questions/26829/picamera-not-working – goobering Jul 19 '16 at 10:02
  • Thanks @jogco I actually found some helpful info by googling that. – Iwotastic Jul 19 '16 at 13:55
  • Depending on the server that Flask is running under, it may be using multiple processes. Bear in mind that the camera firmware only permits a single process to own the camera at any one time. I don't think that's the issue here because it's complaining about the null-sink (if multiple processes try to access the camera, the error occurs when creating the MMAL component representing the camera). Still - you'll need to be very careful that only a single process owns the camera at one time. – Dave Jones Jul 19 '16 at 14:15
  • @Iwotastic, great! That was my intention. :) – jogco Jul 19 '16 at 20:59

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