Badger and Fox Deterrent

By Rosemary Hattersley. Posted

Moving house is stressful enough; unpacking only to discover your new garden is favoured by unexpected local wildlife that doesn’t respond to unsubtle hints is unsettling too. Software engineer James Milward decided on a technology-based approach to tackling his night-time visitors, creating a Badger and Fox Deterrent based around Raspberry Pi 4 and some machine learning. With a day job involving data and AI, James already had a fair clue of how to go about it.

He had a number of Raspberry Pis at home and built a solar-powered watering system using Raspberry Pi A+ back in 2015. He says the low-powered footprint paired with the significant compute capabilities of Raspberry Pi 4 prompted him to explore its machine learning potential at home.

Inaudible alarm bells

Having moved house in 2022, James and his family quickly discovered an issue with their new home: local foxes and badgers “loved ripping the garden up, digging deep holes and leaving ‘gifts’ everywhere for us”. Most disturbingly, they brought in insects carrying who knew what in terms of germs and potentially harmful diseases. Once local wildlife experts confirmed these fleas and ticks were definitely from their garden visitors, James was “set on a path to create a deterrent that could help with our problem”.

He initially tried scent-marking repellent and natural mixes of citronella and chilli, blocking entry points along the fences in the garden, but “This just resulted in our fluffy friends ignoring my initial attempts and digging more holes”. He tried one solution that offered a glimmer of hope: solar-powered ultrasonic repellents. The Milwards duly placed a few around their garden. However, the repellents required manual setting depending on whether it was badgers or foxes they wanted to warn off, as well as deactivating so as not to trigger when the family was in the garden. This led to occasions when no ultrasonic repellent was in use, and a further incursion of wildlife. Combined with inconsistent charging of the solar-powered units, James realised he needed something smarter: Raspberry Pi would be the perfect choice to help add some intelligence to these very basic devices.

Tense, nervous, headache

James had already experimented with TensorFlow and figured the combination of this open-source machine learning platform’s object recognition capabilities and Raspberry Pi could work well for his Fox and Badger Deterrent, modifying the ultrasonic repellent for remote use. By adding wireless connectivity and an ESP8266 microprocessor, he would even be able to have the repellent operate at a targeted frequency range to “accurately deter targets”.

The project’s real headache came when trying to get video frame rates good enough for object detection at a resolution of 640×640 pixels. James used existing YouTube tutorials designed for 320×320 models, but soon found that more detailed images would be needed, and set about reducing the complexity of the model without losing too much accuracy. Unfortunately, this method brought the frame rate up to just under 1 FPS, which was fine for a single video stream, but James had built the code to work on multiple cameras at once. He then discovered that the Coral USB Accelerator Edge TPU works superbly with Raspberry Pi 4, which has the necessary USB 3.0 ports. “I was able to use this device to get the frame rate for object detection up to 5 FPS for one video feed and about 2 FPS for two feeds running concurrently.”

Warning: Working with animals

Please be mindful when creating devices that interact with animals. Read the RSPCA’s guide on deterrents and animal population control.

From The MagPi store


Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.