A lightweight Python script that streams a Raspberry Pi camera (or webcam fallback) into an MJPEG HTTP stream, intended for use with VisiCut.
The camera only starts when a client is connected, disconnecting when all clients disconnect (with a configurable timeout).
The script is meant for a Raspberry Pi. It depends on picamera2 to access the camera module and
opencv-python for webcam fallback. The script can gracefully shut down with Ctrl+C.
sudo apt update
sudo apt install python3-picamera2 python3-opencvClone the code and test the internal flags:
python3 server.py --help--host/--port: Defaults to127.0.0.1:8080.--source: Ordered list of camera sources to attempt. Defaults topicamera webcam.--width/--height: Camera stream output scale--fliph/--flipv: Flips the camera image horizontally and/or vertically.--timeout: Seconds to wait before stopping the camera when there are no viewers. Defaults to5. A negative value (e.g.-1) keeps the idle camera running indefinitely.--fps: Target camera framerate (frames per second). Defaults to1.--debug: Enable debug level logging. This logs the requester's IP address.
- Continuous Stream: Any path except the snapshot ones (e.g.,
http://<host>:<port>/) returns a standard multipart MJPEG stream. - Single Snapshot:
/snapshotor/snapshot.jpgreturns a single static JPEG frame.
VisiCut expects a single image rather than a continuous MJPEG stream. Use the snapshot endpoint for the camera URL:
http://<your_host>:<your_port>/snapshot
In the VisiCut Camera Settings:
- URL:
http://<your_host>:<your_port>/snapshot - Timings (ms): This field controls the polling rate. It is recommended to set this between 1000 and 3000 (1 to 3 seconds) to ensure a stable feed without overwhelming the network.
Serve the stream on all interfaces on port 1234:
python3 server.py --host 0.0.0.0 --port 1234Flip the lens completely and keep the camera running indefinitely, even when idle:
python3 server.py --fliph --flipv --timeout -1