How I made an ai to watch my cats poop for me

I started this project in 2020 and it did take me 2 years to finish and made a video about it.. so any shares are extremely appreciated 😅 Anyway, I made a poop monitor for my cats. Like a baby monitor but a poop monitor. Here is the video for context, scroll down for how I did the thing.

 
 

Live view and data

I wanted my project to be mobile so that I could see my cats pooping from anywhere in the house. So I decided to make a website to be hosted in the Pi. This way anyone on the same network (my wifi) can see it from any device connected to my WiFi from anywhere in my house. To do that, I used a Raspberry Pi to run a Node.JS server (back-end) and host a website (front-end) to display all the sensor data.

The back-end

Installing NodeJS on Raspberry pi

1. Checked the ARM version of my Pi:

$ uname -m

2. Went to the Node.JS download page and copied the right version which was ARMv7. I copied the download link.

3. In the terminal I pasted the download link to download:

$ wget https://nodejs.org/dist/v12.19.0/node-v12.19.0-linux-armv7l.tar.xz

4. Extracted the archive:

$ tar -xvf node-v12.19.0-linux-armv71.tar.xz

5. Copied node to /usr/local

$ cd node-v12.19.0-linux-armv71/
$ sudo cp -R * /usr/local/

Done. You can check with node -v or npm -v to check the version and verify that it did install correctly.

The front-end

The HTML which is hosted, in the same Raspberry Pi as the back-end. Any device connected to the same network as my Raspberry Pi can see it.

The thermal camera + view

I wanted to see when my cats poop, but I didn’t want to see it. So I got a thermal camera to see their temperature silhouettes à la Predator, which looks super cool. Then I thought might as well take their temperature. So I got Pimoroni’s thermal camera - the mlx90640. Pimoroni also provides an experimental library for this camera that works with Raspberry Pi, I tried it, and it worked!

To visualize it in the website, I took the test.cpp example code from Pimoroni’s mlx90640 library and recreated it in Javascript.

Here is what I did:

  • I modified test.cpp which reads from the thermal camera to make a 2D array of temperature values and output a string with the data in JSON format

  • The NodeJS back-end reads the string and converts it to an object

  • This object is then used by the front-end (written in JavaScript) and draws the thermal view on the html

  • I used web sockets to continuously receive the updated object from the back-end to the front-end and update the thermal view on the website, making it looks like a live view of the camera

 
This is how the thermal camera looks on the website.

This is how the thermal camera looks on the website.

 

Cat Prediction (OpenCV vs. Custom Vision)

To determine whether Teddy Bear or Luna is using the litter box, I went for object recognition.

I started with OpenCv to do the object recognition. I followed these instructions to install it on a Pi. I don’t really recommend doing OpenCV on a Pi, but if you’re going to do this, do it with the Pi 4, not the 3B. The 3B took 7 hours to build OpenCV and the 4 took 1 hour. Learn from my mistakes and don’t do OpenCV on a Pi, the Jetson nano comes with it.

Also my models took about a week to train each on my computer and didn’t work well. I tried almost everything I could find online and the fastest easiest one was a free trial of Custom Vision. It only took me like a couple afternoons (because of my learning curve) to train the models on their server and the prediction working. Not an ad or sponsored in any way. I did the free trial and I really liked it because it was very easy to do and also liked their UI and it was very easy to do the Python in the Pi once I had the trained models in their servers.

what the prediction returns

 
 

To train the models I had to get a whole bunch of pictures of Teddy and Luna using the bathroom.

I needed positive and negative models. The Teddy positive model is trained with pictures of Teddy using the litter box. A Teddy negative model is trained with pictures of Luna using the litter box, blurry pictures, an empty litter box, my hands cleaning the litter box, and the occasional foot pic loll. And the same would go for Luna’s models.

To get the pictures I had to do “night vision”…

I have thousands of these

thousands and thousands of these too

NIGHT VISION

The litter box area is very dark and I didn’t want to use a flash for the pictures because I didn’t want to scare my cats. So I went for infrared LEDs which are invisible to the eye (and cat’s eyes) that turn on when a cat goes into the cabinet. I paired with a NoIR camera to be able to see the IR shined on the cats and be able to see in the dark and be able to do the cat detection.

To mount the LEDs I designed and 3D printed tiny stadium light panels to hold the individual IR LEDs and and had a pivot to adjust once inside the cabinet. I did five 4x4 panels in total 80 IR LEDs.

I am so proud of these cases :)

I am so proud of these cases :)

Screen Shot 2020-10-09 at 8.29.53 PM.png

These LEDs turn on and off with a transistor. I didn’t want to use a relay because of the clicking sound. I don’t want to train my cats to feel like pooping or peeing when they hear a relay.

So all the LEDs are wired to a 2N3906 transistor, something like this.

IMG_3969.jpg

NoIR camera settings

#setting camera's exposure and resolution for night view
print "setting camera"
camera = PiCamera()
camera.resolution = (640, 480)
camera.sensor_mode =3
camera.iso = 0
camera.framerate_range = (0.167, 6)
camera.exposure_mode = 'nightpreview'
time.sleep(10)

The poop threshold

Once I had all the photos I grabbed the timestamps and calculated how long it took them to go to the bathroom. The time difference between pee and poo was significant not only because it takes them longer to poo but also because they also spend more time trying to bury the poo and scent longer. Yes I watched them poo and pee for almost 2 years. Don’t ask me about my mental health I did it for science.

Anyway, I made a time threshold and basically

if (seconds > poopThreshold)
return poop;

Making it pretty ~

The rest was just making it pretty in the UI ^^ Made a couple gifs of my cats and the vacant sign, the main page shows the picture it took, the name of the cat that is currently using the bathroom, their temperature, the live feed of the thermal camera, a timer, and when they are done either the poo or the pee drawing highlights to show which one it was.

There is also a tab with tables that show all the historical data and I can also filter through it if I just want to see Teddy’s or Luna’s by tapping their faces. It is a very human UX ;] You can see what they did, the date, how long it took them, and the photo that was taken when they went.

I drew these :]

Teddy’s bathroom remodel

TBH this was my favorite part of the project. I used oak plywood and basically made my first cabinet ever and folding table. I learned a LOT just watching this video so 98% credit for the design should go to Evan and Katelyn :]

before

after

And if there is anything else to say is to follow your dreams because you’ll never know when you will spend 2 years watching your cats poop.

e

ok byeeeeeee