Mouse-free data science. Detect your cat’s prey with a Raspberry…
By: Gerrit Bojen
Tags: Machine Learning, Cats, Data Science, Artificial Intelligence, Raspberry Pi

Detect your cat’s prey with a Raspberry Pi and a Google Coral Edge TPU

Photo by Author

My neighbor Emma has a real love for animals. A little hedgehog family shows up frequently in her garden. She buys special hedgehog food that is highly appreciated by those heart-melting little guys. She has set up four feeding places with daily refills. In a recent conversation over-the-fence, Emma proudly told me about the large appetite of her animal friends and I congratulated her. But as I walked from the garden into my house it dawned on me … Emma’s special hedgehog food may well be a welcome nutritional supplement for the local mouse community.

Video by Author

For months our two out-door cats carried dead, living and partially living mice into our home at night. Dead prey found on the carpet in the morning quickly took a ride down the ceramic subway in our bathroom, that was not the big deal. What impressed me more was the agility of the living species. Some tried to settle under our couch. It would take an average of 45 minutes for my wife Lisa and me to locate and chase these little rodents out of the house. Our cats started to reach a high score of four mice per night and our dismay at the increase of rodents found in our living room was not acknowledged at all by our cats. They seemed to consider this as a necessary exercise to train our poor mouse hunting skills and enjoyed delivering more and more “gifts” every night in order for us to do so. The unwelcome roommates left urine stains on our parquet floor and ate parts of our tablecloth as they prepared for a new long-term stay inside of our living room furniture. The straw that broke the camel’s back was when the rodent started to nibble holes in the cat food bags.

Lisa and I started to think of a preventative approach. The first discussion circled around a nightly curfew to prevent our animal surprise gift baskets. We decided to replace our old pet door with a smart cat flap including a programmable curfew allowing our cats to leave the house at night but preventing them from re-entering before the morning. We abandoned this approach after the first night. Having woken up to the sound of our cats knocking their heads against the locked flap door, causing us to feel miserable about our egocentric behavior. This smart cat flap was not smart at all.

While I do not consider myself as a nerd, Lisa observed that I devoted (in her terms) “a disproportionate amount of time on a technical pursuit in order to develop a solution”. The plan to create a smart-er cat flap that prevents cats unwanted gifts turned into an “idée fixe” that received a subdued smile from my wife.

Photo by Author

My second approach involved a Raspberry Pi 4 Model B, a motion sensor and an infrared camera. Over a timeframe of four months I selected 1532 night vision images of our cats entering our cellar window and uploaded these images to an AWS S3 bucket. 415 out of the 1532 images were images with prey. I created a mobile app that displayed notifications when a cat entered the house and allowed me to label the images “with-prey” and “with-out prey”.

Video by Author

I used YOLO to train an object detection model and triggered the cat flap API which was reverse engineered by rcastberg. The object detection took c. 2–3 seconds, in addition I needed to take into account the duration for the cat flap API call which took c. 2–12 seconds. The cat’s trip down the cellar window takes c. five seconds. Moving the object detection to an edge TPU based on Google Coral with a TFLite MobileNet V1 SSD, reduced the inference time to 7ms and helped to collect enough data to calculate a prey detection score with a very good prediction precision. However 8 out of 10 detections resulted in the cat flap locking after the cat had already passed through. At least now I knew when to start the mouse hunt.

Photo by Author

I tried to shorten the delay with a by-pass to the API service of the cat flap. Then I tried to connect my Raspberry to the cat flap via ZigBee. This was a proprietary encrypted protocol. I saw on Github that a bunch of experts (definitely not nerds) keep cutting their teeth on that issue. If someone at Sure Petcare reads this: your product is awesome, I like the mobile app, I would love to have a way to communicate with the Connect Hub locally with a documentation of the API.

I decided to take the easy route and soldered a relay on the lockout button of the cat flat. This reduced time-to-lockout to below 1 second. Tada!

Photo by Author

This whole pursuit was a continual process that took more than seven months. The source code to create and run the model on your Raspberry is available on Github.