Question about possible Raspberry Pi Picamera2 integration #4
-
I've been experimenting with the Picamera2 MJPEG example, but as a sample it lacks in robustness and taht lead to me to finding your repo. Here's the Picamera2 example: https://github.com/raspberrypi/picamera2/blob/main/examples/mjpeg_server.py I read the through the README and didn't quite follow the inputs for the frames. Would you mind elaborating on that? If your familiar with Picamera2 and could offer more explicit guidance that could unlock your solution for a lot of other as well. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi Patrick, Unfortunately, I don't have any practical experience with picamera2. I am assuming you want to implement a streaming MJPEG server. The key step is to define a Python function to yield frames, for example: def picamera2_reader():
while True:
frame = ... # do something to get a frame from picamera2
yield frame The yielded value is binary JPEG data, i.e., one frame. The type of the value must be either The example in mjpeg_server.py, specifically the picam2 = Picamera2()
picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)}))
output = StreamingOutput()
picam2.start_recording(JpegEncoder(), FileOutput(output)) and then define the picamera2_reader() function as follows: def picamera2_reader():
while True:
with output.condition:
output.condition.wait()
yield output.frame Then start the Flask server as follows: from Flask import Flask, Response
from mjpeg.server import MJPEGResponse
@app.route('/')
def stream():
return MJPEGResponse(picamera2_reader())
if __name__ == '__main__':
app = Flask(__name__)
app.run(host='0.0.0.0', port=8080) Hope this helps. I have not tested the above code (I don't have the camera). If you make it work and would be willing to contribute an example or documentation, I would greatly appreciate it! Jan |
Beta Was this translation helpful? Give feedback.
Hi Patrick,
Unfortunately, I don't have any practical experience with picamera2. I am assuming you want to implement a streaming MJPEG server. The key step is to define a Python function to yield frames, for example:
The yielded value is binary JPEG data, i.e., one frame. The type of the value must be either
bytes
ormemoryview
. The py-mjpeg library prepends the necessary HTTP headers and writes the binary data to the socket as payload. The Flask server will call the picamera2_reader() function repeatedly until the client closes the connection, or until the function…