In a Python app, or in Python at the command line, I can initialize a Raspberry pi camera like this:
from picamera2 import Picamera2picam2 = Picamera2()config = picam2.create_video_configuration(main={"size": (540, 304)})(... picamera2 stuff)
When I have a Flask app and try to do the same, I get a RunTime error:
from flask import Flask from picamera2 import Picamera2app = Flask(__name__)(flask stuff ... )picam2 = Picamera2()config = picam2.create_video_configuration(main={"size": (540, 304)})(... picamera2 stuff)if __name__ == "__main__": app.run(debug=False, host='0.0.0.0', port=5000)File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 254, in __init__ raise RuntimeError("Camera __init__ sequence did not complete.") RuntimeError: Camera __init__ sequence did not complete.
If I initialize in a "with ... as" block, I do not get an error, the camera is correctly initialized and I can use it in the block, but I'm not able to use picam2 generally:
from flask import Flask from picamera2 import Picamera2app = Flask(__name__)(flask stuff ... )with Picamera2() as picam2: config = picam2.create_video_configuration(main={"size": (540, 304)}) (picam2 stuff ... )if __name__ == "__main__": app.run(debug=False, host='0.0.0.0', port=5000)
Why does Picamera2 work differently in Flask than in straight Python? How can I initialize the camera so as to be able to use it throughout the script?