From d5c28cdf3d61b9499129cbc6dcc44b2a29fb13ee Mon Sep 17 00:00:00 2001 From: Akesa-24 Date: Thu, 15 Oct 2026 12:06:03 +0000 Subject: [PATCH] adding cam viewer --- rpi-cam-viewer.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 rpi-cam-viewer.py diff --git a/rpi-cam-viewer.py b/rpi-cam-viewer.py new file mode 100644 index 0000000..f9ac703 --- /dev/null +++ b/rpi-cam-viewer.py @@ -0,0 +1,40 @@ +import io +from flask import Flask, Response +from picamera2 import Picamera2 +from PIL import Image + +WIDTH, HEIGHT = 640, 400 + +cam = Picamera2() +cam.configure(cam.create_video_configuration( + main={"size": (WIDTH, HEIGHT), "format": "YUV420"}, + controls={"FrameRate": 30})) +cam.start() + +app = Flask(__name__) + + +def frames(): + while True: + gray = cam.capture_array("main")[:HEIGHT, :WIDTH] # Y plane = greyscale image + buf = io.BytesIO() + Image.fromarray(gray).save(buf, format="JPEG", quality=70) + yield b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + buf.getvalue() + b"\r\n" + + +@app.route("/") +def index(): + return ('' + '') + + +@app.route("/stream") +def stream(): + return Response(frames(), mimetype="multipart/x-mixed-replace; boundary=frame") + + +if __name__ == "__main__": + try: + app.run(host="0.0.0.0", port=8000, threaded=True) + finally: + cam.stop() \ No newline at end of file