Detect your cat’s prey with a Raspberry Pi and a Google Coral Edge TPU
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 …
For months our two out-door cats carried dead, living and partially living mice into our home at night. I flushed dead prey the toilet down as soon as I found it on the carpet in the morning, that was not the big deal. I was more impressed by the agility of the living species. Some tried to settle under our couch. It took an average of 45 minutes for my wife Lisa and me to locate and chase these little rodents out of the house. Our cats began to reach a high score of four mice per night. Our nerve-wracking performance 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. Emma’s special hedgehog food was also a welcome nutritional supplement for the local mouse community.
The little roommates left urine stains on our parquet floor and ate parts of our tablecloth while they accommodated for a new long-term quarter 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 discussed our first remediation approach. The first discussion circled around a nightly curfew to prevent our animal surprise gift baskets. We decided to replace our cellar pet door with a smart cat flap with a programmable curfew that allowed our cats to leave the house at night but prevents them from reentering before the morning. After the first night we turn the curfew off. We woke up at night hearing our cats knock their heads against the locked flap door and felt 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 slavishly devoted (to her terms) a disproportionate amount of time to a technical pursuit to develop a solution. The idea to create a smart pet flap that locks out cats turned into an idée fixe that was smiled at by Lisa.
My second remediation approach involved a Raspberry Pi 4 Model B, a motion sensor and an infrared camera. Over a timeframe of four month I selected 1532 night vision images showing our cats climbing up and down to and through our cellar window and uploaded these images to an AWS S3 bucket. 415 out of the 1532 images were images with pray. I created a mobile app that showed notification when a cat entered the house and allowed me to label the images “with-pray” and “with-out pray”.
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 ~2–3 seconds in addition I need to take into account the duration for the cat flap API call which took between two and 12 seconds. The cat’s trip down the cellar window takes ca. five seconds. Moving the object detection to an edgeTPU based on Google Coral with a TFLite MobileNet V1 SSD reduced the inference time to 7ms and helped to collect enough detections to calculate a prey detection score with a very good prediction precision. But 8 out of 10 detections resulted in locking the flap after the cat passing it. So now at least I did know when we had to start the mouse hunt.
I tried to shorten the delay with a by-pass to the API service of the cat flap. 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 (I would not call them 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.
So I decided to take the easy route and soldered a relay on the lockout button of the cat flat. This reduced the time-to-lockout to below 1 second. Tada!
This whole pursuit was an iterative process. It took more than seven months. The source code to create and run the model on your Raspberry is available on Github.