This is a small python application that tracks cats (and other movement) on a Raspberry Pi.
It uses OpenCV to perform the motion detection.
The project was based on a couple of blog posts by Adrian Rosebrock
- A Raspberry Pi (3 Model B+ or above is recommended, perhaps a Raspberry Pi 4 B)
- A nice big Micro SD Card from Amazon?
- A camera (i.e. an infra-red wide-angle lense camera with built-in infra-red LEDs from Pimoroni)
- Raspbian "Buster" (available from the raspberrypi.org website)
- Python 3 (included with "Buster")
- OpenCV (see below...)
I got the following instructions from this blog post to set up OpenCV:
- Get a Raspberry Pi 3 B+ or 4 B.
- Ensure it is running Raspbian "Buster". (see the above link in the prerequisites, the above blog post or Google for how to do this).
- Ensure you have "Expanded the filesystem". Do this by running the
raspi-configtool usingsudoand choosing Advanced Options -> Expand Filesystem. - Also ensure that the camera is enabled using
raspi-configby going into the Interfacing Options section. - Reboot your Pi (
sudo reboot) - Install a boat-load of dependencies (again see the post for what they all are):
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libfontconfig1-dev libcairo2-dev
sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
sudo apt-get install python3-dev- Install pip and virtualenv:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py
sudo rm -rf ~/.cache/pip
sudo pip install virtualenv virtualenvwrapper- Set up your virtualenv. Run
nano ~/.bashrcand then add the following to the BOTTOM of the file:
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.shSave and exit. Then reload the .bashrc to ensure it is being used:
source ~/.bashrcThen make your "cv" virtualenv:
mkvirtualenv cv -p python3- Install picamera:
pip install "picamera[array]"- Install opencv:
pip install opencv-contrib-python==4.1.0.25- Test the install:
cd ~
workon cv
python
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>>If this works then all is good!
Then run the application at the command line using:
python3 pi_surveillance.py --conf config.jsonBy default it will detect motion and save images into a ./images subdirectory.