Having a garden in a neighbourhood with tons of cats has the disadvantage that these cats will leave too much fertilizer between my vegetables. Being a garden and Raspberry Pi enthusiast, I have already built an automated irrigation system for my pumpkins, the spices, tomatoes and potatoes. Fortunately, I still had one valve unconnected, which I use now to deter cats.
Summary
In short, the cats are recorded with a Raspberry Pi Pi Noir (night vision) camera, using the software motion. Motion will record every movement it detects in its field of view. In a first step, I sorted these images in two groups:
- images containing cats
- images without cats
I then copy these images to a Windows computer with a decent graphics card, where I train a deep learning network to distinguish images of cats from images without cats.
The neural network tree is copied to the raspberry pi. When then motion records a movement, it calls a python script that checks with the neural network tree if the image contains a cat or not. In case a cat is present, it opens the valve and the cat is wet. And will hopefully never again enter my garden...
Detailed guide, part I: Setting up the cat camera
First you need a raspberry pi, best a raspberry pi 3. Install the latest raspbian stretch image: https://www.raspberrypi.org/downloads/raspbian/
Start up the raspberry pi and set up the wlan to be able to connect to your wireless lan. With:
sudo raspi-config
open up the configuration, and enable the camera. I run the raspberry pi directly outside in my garden, so it is completely headless. So also enable ssh. If you prefer a graphical desktop, enable the vnc, see for example here:
https://www.raspberrypi.org/magpi/vnc-raspberry-pi/
Check with:
ifconfig wlan0
what the IP of your raspberry pi is, and connect then from your laptop / windows machine with vnc or ssh to the raspberry pi.
Then connect your pi noir camera with a infrared LED (e.g. from here: https://www.play-zone.ch/de/hdtv-pi-noir-ir-camera-inkl-led-module.html). I also added a infrared LED so that it also finds cats during the night.
Check with:
raspistill -o image.jpg
if everything is connected correctly. If there is no error, it will write a file called image.jpg
We will need to install some libraries, but first start with updating and upgrading the raspberry pi from a terminal:
sudo apt update
sudo apt upgrade
sudo apt-get install libmariadbclient18 libpq5 libavcodec57 libavformat57 libavutil55 libswscale4
Then download and install the motion software:
wget https://github.com/Motion-Project/motion/releases/download/release-4.0.1/pi_stretch_motion_4.0.1-1_armhf.deb
sudo dpkg -i pi_stretch_motion_4.0.1-1_armhf.deb
Then configure motion:
sudo nano /etc/motion/motion.conf
Change:
daemon on
then:
stream_localhost off
Set the framerate, width and height of the image
stream_maxrate 20
framerate 20
width 640
height 480
First we will do a quick sanity check if everything is setup correctly. In order to start motion, you will have to edit:
sudo nano /etc/default/motion
and change:
start_motion_daemon=yes
If you use the raspberry pi camera (pi noir), you will have to edit:
sudo nano /etc/modules
and add:
bcm2835-v4l2
This will load a driver which is needed that the raspberry pi camera appears as a video device (under /dev/video0)
Let's now do a sanity check, and start the motion service:
sudo service motion start
Connect with a webbrowser to
<rpi-ipAddress>:8081
and check if you see the stream!
In my case, this is:
http://192.168.178.48:8081/
And I see a liveview of my garden:
If nothing appears, you can check the status of the motion service:
sudo service motion status
if it does not say active ( running) in green, then something went wrong. Maybe restarting the raspberry pi helps?
Here is part II:
https://steemit.com/raspberrypi/@mragic/how-to-build-a-cat-detector-with-a-raspberry-pi-and-a-pi-noir-camera-using-deep-learning-part-ii