Example - copy and change it

Put a camera on your dashboard

Source:
examples/birdbox.py
Widgets:
Camera, Gauge
Hardware:
A Raspberry Pi with a camera for the real thing; any computer for the test pattern
Status:
Tested with a fake connection and the test pattern. Not yet run against a real server. The Camera component itself has run on real Pi cameras (see Bird Box Live).

Put a camera on your dashboard

A camera is the most satisfying thing you can add to a Pi project and, usually, the hardest. ThePiHub's Camera component keeps it to a few lines: one widget that shows live video and carries its own buttons to stream, take a snapshot, record and go full screen.

The whole idea

camera = birdbox.output(id="camera", type="Camera")
if HardwareCameraSource.available():
    camera.use(HardwareCameraSource())      # the Pi's camera, encoded by the Pi's hardware
else:
    camera.use(EncodedTestPatternSource())  # no Pi camera here: a moving test pattern
camera.recorder()                           # adds the Record button

use() says where the pictures come from. recorder() adds recording. That is all: the buttons on the dashboard are answered by the SDK, so there is no handler to write.

Why it is cheap to run

On a Pi with a camera the example uses the Pi's own hardware H.264 encoder, so the picture is compressed by the GPU rather than the CPU. Recording and streaming share that single encode. And the camera does not run all the time: it starts when somebody presses the button (or a recording starts) and stops again when they stop watching or close the page.

No Pi? No problem

Without a Pi camera the example falls back to a moving test pattern that behaves like a camera: it streams, records and takes snapshots. You can try the whole thing on a laptop.

Know when someone is watching

@birdbox.on_first_user_connected
def viewer_arrived(payload):
    birdbox.info("Viewer connected")

Your program is told when the first viewer arrives and when the last one leaves, which is the moment to start or stop anything that only matters while somebody is looking.

Running it

It needs the video extras: pip install "thepihub[webrtc]", which brings PyAV along. (On a 32-bit Pi such as the original Zero W, see the deployment guide.) Copy birdbox.py anywhere and run:

python birdbox.py

If this is the first time you have connected from this device, it prints a link and a code to approve in your browser; answer y when the terminal asks you to confirm the account. That makes the device yours and gives it an id. Pictures are saved to ~/thepihub-snapshots and recordings to ~/thepihub-recordings, on the device. Video goes straight from the Pi to your browser and never through ThePiHub's servers.

Next

This is the small version. Bird Box Live is the same idea as a complete program with settings, storage options and a service that starts at boot.

← Back to Projects