signag / raspi-cam-srv

Web Server for Raspi Camera Access
MIT License
36 stars 4 forks source link
camera flask-application focus-stacking motion-capture motion-detection picamera2 python3 raspberry-pi timelapse webcam webserver

raspiCamSrv

The Raspberry Pi Camera Server (raspiCamSrv) is a web server which can be deployed on a Raspberry Pi device giving access to installed cameras and allows to control these.

It can be installed on all Raspberry Pi platforms which allow connection of one or multiple cameras and supports the currently existing camera types. Up to now, it was tested on Pi Zero W, Pi Zero 2 W, Pi 4 and Pi 5 running Bullseye as well as Bookworm together with camera modules 1, 2, 3, HQ and GS. On Pi 5, also parallel installation of two different cameras was tested.

raspiCamSrv is built with Flask 3.0 and uses the Picamera2 library.

Due to responsive layout from W3.CSS, all modern browsers on PC, Mac or mobile devices can be used as clients.

Feature Overview V2.6.3

For more details and update procedure, see the User Guide and Release Notes.
For installation procedure, see below.

Live Overview

New in V2.6.0

Known Issues

Limitations

The software is still being tested and extended.

Credits

Setup / Getting Started

Required

The setup description, below, assumes a completely autonomous or 'headless' setup, where the Raspberry Pi requires nothing but a power supply cable without any necessity to ever connect it to a display, keyboard or mouse.
Pi Zero Cover
Here, the camera model 2 is installed.

The described steps were successfully executed with Raspberry Pi Imager version 1.8.5 and a Raspberry Pi Zero W.

System Setup

For system setup, follow the instructions of the Raspberry Pi Getting Started Documentation for OS installation using Imager.
Make sure that SSH is enabled on the Services tab.

Once the SD card is written, insert it into the Raspberry Pi and power it up.
Initially, it will take several minutes until it is visible in the network.

RaspiCamSrv Installation

If you want to update an existing installation to the latest version, see Update Procedure.

In case of problems during installation and usage, see Troubleshooting

Step Action
1. Connect to the Pi using SSH:
ssh <user>@<host>
with \ and \ as specified during setup with Imager.
2. Update the system
sudo apt update
sudo apt full-upgrade
3. Create a root directory under which you will install programs (e.g. 'prg')
mkdir prg
cd prg
4. Check that git is installed (which is usually the case in current Bullseye and Bookworm distributions)
git --version
If git is not installed, install it with
sudo apt install git
5. Clone the raspi-cam-srv repository:
git clone https://github.com/signag/raspi-cam-srv
6. Create a virtual environment ('.venv') on the 'raspi-cam-srv' folder:
cd raspi-cam-srv
python -m venv --system-site-packages .venv
For the reasoning to include system site packages, see the picamera2-manual.pdf, chapter 9.5.
7. Activate the virtual environment
cd ~/prg/raspi-cam-srv
source .venv/bin/activate
The active virtual environment is indicated by (.venv) preceeding the system prompt
8. Make sure that picamera2 is available on the system:
python
>>>import picamera2
>>>quit()
If you get a 'ModuleNotFoundError', see the picamera2 Manual, chapter 2.2, how to install picamera2.
For raspiCamSrv it would be sufficient to install without GUI dependencies:
sudo apt install -y python3-picamera2 --no-install-recommends
9. Install Flask 3.0 with the virtual environment activated.
Raspberry Pi OS distributions come with Flask preinstalled, however with versions 1.1 or 2.2.
RaspiCamSrv requires Flask 3.0, which can be installed with
pip install Flask==3.0.0
If you want to check the Flask version, you may need to deactivate/activate the virtual environment first:
deactivate
source .venv/bin/activate
flask --version
This should reveal version 'Flask 3.0.0'.

Make sure that Flask is really installed in the virtual environment:
which flask should output
/home/<user>/prg/raspi-cam-srv/.venv/bin/flask
10. Optional installations:
The following installations are only required if you need to visualize histograms for some of the Photo Series
or if you are interesten in using Extended Motion Capturing Algorithms.
It is recommended to do the installation with an activated virtual environment (see step 7), although some of these packages might come preinstalled.
Install OpenCV: sudo apt-get install python3-opencv
Install numpy: pip install numpy
Install matplotlib: pip install matplotlib
11. Initialize the database for Flask
(with raspi-cam-srv as active directory and the virual environment activated - see step 7):
flask --app raspiCamSrv init-db
12. Check that the Flask default port 5000 is available
sudo netstat -nlp \| grep 5000
If an entry is shown, find another free port (e.g. 5001)
and replace port 5000 by your port in all flask commands, below and also in the URL in step 12.
13. Start the server
(with raspi-cam-srv as active directory and the virual environment activated - see step 7):
flask --app raspiCamSrv run --port 5000 --host=0.0.0.0
14. Connect to the server from a browser:
http://<raspi_host>:5000
This will open the Login screen.
15. Before you can login, you first need to register.
The first user will automatically be SuperUser who can later register other users (User Management)
16. After successful log-in, the Live screen will be shown
17. Done!
18. For usage of raspiCamSrv, please refer to the User Guide

When the Flask server starts up, it will show a warning that this is a development server.
This is, in general, fine for private environments.
How to deploy with a production WSGI server, is described in the Flask documentation

Service Configuration

When the Flask server is started in a SSH session as described in step 10, above, it will terminate with the SSH session.

Instead, you may want the server to start up independently from any user sessions, restart after a failure and automatically start up when the device is powered up.

In order to achieve this, the Flask server start can be configured as service under control of systemd.

The following procedure is for the case where audio recording with video is not required. Otherwise, see next section.

Step Action
1. Open a SSH session on the Raspberry Pi
2. Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~
3. Adjust the service configuration:
nano ~/raspiCamSrv.service
Replace all (4) occurrences of '\' with the user ID, specified during System Setup
If you need a port different from 5000 (see RaspiCamSrv Installation, step 10), replace also port 5000 by your port.
4. Stage the service configuration file to systemd:
sudo cp ~/raspiCamSrv.service /etc/systemd/system
5. Start the service:
sudo systemctl start raspiCamSrv.service
6. Check that the Flask server has started as service:
sudo journalctl -e
7. Enable the service so that it automatically starts with system boot:
sudo systemctl enable raspiCamSrv.service
8. Reboot the system to test automatic server start:
sudo reboot

Service Configuration for Audio Support

If it is intended to record audio along with videos, a slightly different setup is required (see Settings):
Instead of installing the service as a system unit, it needs to be installed as user unit (see systemd/User) in order to get access to PulseAudio.

Bookworm Systems

If your system is a bookworm system (lsb_release -a) follow these steps:

Step Action
1. Open a SSH session on the Raspberry Pi
2. Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~
3. Adjust the service configuration:
nano ~/raspiCamSrv.service
Replace '\' with the user ID, specified during System Setup
If necessary, raplace also the standard port 5000 with your port.
Remove the entry User=\ from the [System] section
In section [Install], change WantedBy=multi-user.target to WantedBy=default.target
4. Create the directory for systemd user units
mkdir -p ~/.config/systemd/user
5. Stage the service configuration file to systemd for user units:
cp ~/raspiCamSrv.service ~/.config/systemd/user
6. Start the service:
systemctl --user start raspiCamSrv.service
7. Check that the Flask server has started as service:
journalctl --user -e
8. Enable the service so that it automatically starts with a session for the active user:
systemctl --user enable raspiCamSrv.service
9. Enable lingering in order to start the unit right after boot and keep it running independently from a user session
loginctl enable-linger
10. Reboot the system to test automatic server start:
sudo reboot

Bullseye Systems

If your system is a bullseye system (lsb_release -a), which is currently still the case for Pi Zero, follow these steps:

Step Action
1. Open a SSH session on the Raspberry Pi
2. Clone branch 0_3_12_next of Picamera2 repository
cd ~/prg
git clone -b 0_3_12_next https://github.com/raspberrypi/picamera2
3. Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~
4. Adjust the service configuration:
nano ~/raspiCamSrv.service
- Replace '\' with the user ID, specified during System Setup
- If necessary, raplace also the standard port 5000 with your port.
- Add another Environment entry: Environment="PYTHONPATH=/home/<user>/prg/picamera2"
- Remove the entry User=\ from the [System] section
- In section [Install], change WantedBy=multi-user.target to WantedBy=default.target
For an example of the final .service file, see below
5. Create the directory for systemd user units
mkdir -p ~/.config/systemd/user
6. Stage the service configuration file to systemd for user units:
cp ~/raspiCamSrv.service ~/.config/systemd/user
7. Start the service:
systemctl --user start raspiCamSrv.service
8. Check that the Flask server has started as service:
journalctl --user -e
9. Enable the service so that it automatically starts with a session for the active user:
systemctl --user enable raspiCamSrv.service
10. Enable lingering in order to start the unit right after boot and keep it running independently from a user session
loginctl enable-linger
11. Reboot the system to test automatic server start:
sudo reboot

Below is an example .service specification for user "sn":

[Unit]
Description=raspiCamSrv
After=network.target

[Service]
ExecStart=/home/sn/prg/raspi-cam-srv/.venv/bin/flask --app raspiCamSrv run --port 5000 --host=0.0.0.0
Environment="PATH=/home/sn/prg/raspi-cam-srv/.venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Environment="PYTHONPATH=/home/sn/prg/picamera2"
WorkingDirectory=/home/sn/prg/raspi-cam-srv
StandardOutput=inherit
StandardError=inherit
Restart=always

[Install]
WantedBy=default.target