DEV Community

Cover image for Scaring a stray cat away using ML
Saifur Rahman Mohsin
Saifur Rahman Mohsin

Posted on

Scaring a stray cat away using ML

I recently began to face an issue at my place that was causing a lot of stress. A stray cat wandered into my apartment and started to poop on my doormat. First, I thought it was a one-off and had the rug washed. But then it did it again. I then kept my doormat in for a couple of weeks and put it back later hoping the cat got a different place to relieve itself. But this continued.

So I figured let's use technology and put my Raspberry Pi Zero to a different use than just a homebridge server. So I purchased a pi camera module, and began to code away. Took me a couple of days to get the whole thing running coz I had to juggle between an ongoing freelance project and this fun personal project but I was quite satisfied with the end result. The crazy thing is that I've never worked with Python in the past so this was technically my first python project.

Anyway, without further ado, here's how a video demo of me setting this up:

The way it works is this:

  1. Keep the camera video open and running forever.
  2. Whenever there's a motion detected, start the recording and detection process.
  3. The recording basically stored video continuously until motion stops for at-least 5 seconds, so I get a nice slice of recorded motion (however long it maybe).
  4. The detection process takes one image every 10 seconds, sends it to Amazon Rekognition where their deep learning algorithm classifies labels on the image. If there is a cat then it sounds an alert.
  5. The alert is basically a dog barking sound that should scare the cat away. While there is no audio, I would still get a recording of this in action so if that happens, I'll post that here. In the meantime, here's a demo of the project barking to a cat image from the internet:

I'm yet to actually see if this solves my cat issue and am more curious if the cat "figures it out". Will add some videos with results when it happens. Also, it would be interesting to try using a different group of sounds to see if that behaviour changes. Anyway, if you're interested to look at the code, feel free to check it out here.

GitHub logo mohsin / cat-detection

A cat detector and scarer using Raspberry Pi Zero

Cat Detection

Introduction

I recently started facing an issue--a wild cat was pooping outside my apartment on my doormat. I figured it would be a good time to put the Raspberry Pi to use to scare the cat away.

Setup

First install all the dependencies using:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

As you see in requirements, the AWS CLI is installed so you would need to link the AWS API credentials to the AWS console. Once complete, you can run the program.

I have added a setup mode for convience so you can setup and align the camera to the field of view properly before running it properly. You can enter setup mode using:

python main.py --setup
Enter fullscreen mode Exit fullscreen mode

Click to see introduction on youtube:

Demonstration of the project setup mode

Solution

This code makes a dog barking sound whenever a cat comes into the view of the pi camera, thus scaring the cat away.

Click to see demonstration

Top comments (0)