A LabThings-FastAPI Thing for working with picamera2
This package defines a Thing
using labthings-fastapi that exposes an HTTP interface to
the Raspberry Pi camera, using picamera2
. This includes autogenerated OpenAPI documentation
viewed with a built-in Swagger/Redocly page at /docs/
, and a Web of Things compliant
Thing Description at the camera's root (usually /camera/
).
For this to work you should be on a 32-bit version of Bullseye or Bookworm os with python v3.9 or 3.11.
It's usually best to install in a virtual environment, but note that you should use
python -m venv .venv --system-site-packages
as the picamera2
package and its dependencies are not all installable using pip
.
The package is on PyPI and can be installed using
pip install labthings-picamera2
You will also need to separately install the server with:
pip install labthings-fastapi[server]
If you want to edit labthings-picamera2 then you can clone this repository. Move to the directory and run
pip install -e .
You can start the server using:
labthings-server --json '{"things":{"/camera/":"labthings_picamera2.thing:StreamingPiCamera2"}}'
This assumes you have labthings[server]>=0.0.6
installed.
You may then point a web browser at http://localhost:5000/camera
or http://localhost:5000/docs/
.
It's also possible to control it from Python, if you have labthings-fastapi
installed (note that
if you're using a separate machine or separate environment for the client,
if you don't add [server]
it won't install the rather heavy server dependencies like starlette
.)
To connect, run:
from labthings_fastapi.client import ThingClient
from PIL import Image
cam = ThingClient.from_url("http://localhost:5000/camera/")
blob = cam.capture_jpeg()
image = Image.open(blob.open())
All of the properties and actions described in the Thing Description should be present as Python properties and methods - currently the types and docstrings don't propagate to the client object, this is planned for the future but isn't reality yet.