Learn to deploy a Groundlight application on a Raspberry Pi Zero 2W to monitor vehicles entering and leaving a parking spot. The code can easily be adapted to any other monitoring task that you would like to deploy on a Raspberry Pi.
Watch the video: https://youtu.be/YpNKHjuZ7tE?si=MGGrh45V31GhRPiN
- Raspberry Pi Zero V2 with basic accessories (we recommend an HDMI to micro HDMI adapter and a USB to micro USB adapter)
- Any USB Webcam or Raspberry Pi camera
- A tripod or some other mount for positioning your camera
- Double-sided adhesive for attaching Raspberry Pi to wall
- Power supply for the Raspberry Pi
- Flash Raspberry Pi OS
- Go to https://www.raspberrypi.com/software/. Download and install the Raspberry Pi Imager for your operating system.
- Open the Raspberry Pi Imager application.
- Choose the OS for your Raspberry Pi. Under 'Raspberry Pi OS (other)' select 'Raspberry Pi OS Lite (32-bit)'. This will give you a lightweight, headless OS.
- Insert your micro SD card into your computer. Select 'Choose Storage' and select your micro SD card.
- Configure some settings for your OS image:
- Click on the Settings button (gear icon).
- On the Settings window, choose a hostname that is relevant to your project. We chose 'parkmon' for our parking spot monitoring application.
- Enable SSH so that you can access your Raspberry Pi remotely from your computer.
- Choose 'Allow pulic-key authentication only' to keep your Raspberry Pi secure.
- Choose a username and password for your Raspberry Pi in case you ever need to plug it into a monitor and keyboard to debug it.
- Configure wireless LAN. Enter your network's SSID and password.
- Click 'Save'.
- Click 'Write' to write the OS to your SD card.
- Boot up Raspberry Pi
- Remove the micro SD card from your laptop.
- Insert micro SD card into the Raspberry Pi.
- Connect Raspberry Pi to power and wait for it to boot.
- On your computer, run
ssh parkmonto network into your Raspberry Pi. It takes a bit of time for the Raspberry Pi to come online, especially the first time it boots, so be patient and try it a few times before assuming there are any issues. If it doesn't work after waiting 5-10 minutes and after multiple attempts, you can connect the Raspberry Pi to a monitor and keyboard to debug. - Once you have successfully connected to your Raspberry Pi, unplug it and deploy on site. We connected a USB webcam and attached our Raspberry Pi to the wall with adhesive tape.
- Ssh back into your Raspberry Pi:
ssh parkmon - Create a folder for your project:
mkdir parkmon - Enter project folder:
cd parkmon - Set up a virtual Python environment for your dependencies:
python3 -m venv pm_env - Activate the virtual environment by running
source pm_env/bin/activate - Update your system packages:
sudo apt update && sudo apt upgrade - Install the system dependencies for OpenCV.
sudo apt install -y build-essential cmake git pkg-config libjpeg-dev libtiff-dev libpng-dev \ libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev \ libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev \ libgtk-3-dev libatlas-base-dev gfortran python3-dev python3-numpy \ libopenblas-dev sudo apt-get install libxml2-dev libxslt1-dev - Install OpenCV. OpenCV can be difficult to install on a Raspberry Pi Zero 2W due to the size of OpenCV and the resource constraints of the Raspberry Pi. To make it easy to install, we recommend installing from a prebuilt wheel.
- Download the wheel:
wget https://www.piwheels.org/simple/opencv-python/opencv_python-4.7.0.72-cp311-cp311-linux_armv7l.whl - Install the wheel:
pip install opencv_python-4.7.0.72-cp311-cp311-linux_armv7l.whl --no-cache-dir
- Download the wheel:
- Install the other Python dependencies for this project:
pip install groundlight framegrab - Ensure that you have a compatible version of numpy. We recommend version 1.26.4.
- Check your version of numpy:
pip show numpy - If necessary, uninstall numpy:
pip uninstall numpy - Install numpy:
pip install numpy==1.26.4
- Check your version of numpy:
- Authenticate your Raspberry Pi with Groundlight.
- Find your Groundlight API token in your password manager. If you need to generate a new API token, you can log into your Groundlight account at https://login.groundlight.ai and go to 'Api tokens'.
- Put your Groundlight API token into a file:
echo 'GROUNDLIGHT_API_TOKEN="<YOUR API TOKEN>"' >> .env_secrets - Source this file in your
.bashrcso that you are always authenticated with Groundlight:echo 'source /home/pi/parkmon/.env_secrets' >> ~/.bashrc && source ~/.bashrc
- Connect to your Raspberry Pi with your favorite code editor. The following instructions are for VSCode.
- Open VSCode on your computer.
- Open the Command Palette, press Ctrl + Shift + P on Windows or Cmd + Shift + P on macOS.
- From the Command Palette, select "Remote-SSH: Connect to Host..."
- In the Command Palette, write
ssh pi@parkmon. Press enter. - It will ask which SSH configuration file you want to use. Typically the first one listed is correct.
- After you do this the first time, you may need to attempt the previous steps again to actually get connected. You'll know that you are connected if it says 'SSH: parkmon' in the bottom left corner.
- Create a detector for your application:
- On your computer, log into your account on groundlight.ai.
- Go to the 'Detectors' tab and click 'Create New'.
- Choose a name and a query for your detector.
- Click 'Create'
- We recommend turning off cloud labeling for now. This will give you a chance to review image queries on your own before allowing queries to be escalated to humans in the cloud.
- Look in the address bar and notice the detector ID (starts with det_). Save this detector ID; you will need it when writing your application.
- Go to the Explorer tab in VSCode an open the parkmon folder.
- In the parkmon folder, create a file called
app.py. - Write your application. Check out the
app.pyfile in this repo to see the code we used in the tutorial. - Set up your application to run at boot.
- Write a shell script that runs the application and reruns it if it crashes. The script we used is available as
run_parkmon.shin this repo. - Create a cronjob to start the application at boot. Run:
crontab -e - At the bottom of this file, write:
@reboot /home/pi/parkmon/run_parkmon.sh. - Save the file. Press Ctrl + O to save and Ctrl + X to exit.
- Reboot your Raspberry Pi:
sudo reboot. Your application should start shortly after it reboots. - Check the log file to see what the application is doing:
tail /home/pi/parkmon/run_parkmon.log -f
- Write a shell script that runs the application and reruns it if it crashes. The script we used is available as
- Collect some data.
- Leave your detector running for a while (usually a day or so) to collect data.
- Log into groundlight.ai and label image queries on your detector. You will see either a "Label Image Queries" or "Keep Labeling" button on the detector detail page.
- Once you have labeled at least a few dozen image queries and your Projected ML Accuracy seems reasonable, activate Cloud Labeling.
- Set up SMS alerts.
- Log in to groundlight.ai.
- Go to Outputs and click on "Create New Groundlight Action".
- Give your action a name. Something like "Car arrives, send me a text".
- Select a condition. In this example, we choose "Changes to" and "Yes".
- Enter your phone number.
- Save.
- Sit back and wait for SMS alerts from your Groundlight detector. To debug, look at the logs
tail /home/pi/parkmon/run_parkmon.log -for view the detector detail page on groundlight.ai.