Raspberry Pi LitterBox Cam and quantified cats

We have three cats. One of our cats occasionally poops outside the litter box. We had our suspicions, but we couldn’t pin down who or why. Territorial issues? Finickiness about box hygiene? Sickness? Fear or surprise? What could we do to reduce the frequency of incidents?

We decided that a litter box webcam was an excellent first project for the Raspberry Pi computer that W- just bought. The Pi is a tiny, quiet, inexpensive Linux server. My webcam worked without hassles, and Motion was easy to set up for motion detection. We set it up to capture videos when the computer detected motion. I watched the videos and encoded the data, tracking which cat and which litter box. I figured that exploring this would be a good excuse to work with the Pi and learn a little more about computer vision.

2014-03-24 Litter Box Cam with Raspberry Pi #raspberry #cats

2014-03-24 Litter Box Cam with Raspberry Pi #raspberry #cats

You might think that watching litter box videos would be boring and somewhat icky. It was surprisingly informative. I had no idea that Luke sniffed so many litter boxes before settling on one. Leia usually checked out one or two boxes before doing her thing, but if all the other boxes were used (even if one of them was used only by her), she sniffed everything and then circled around in indecision before finally pooping in the middle of the basement floor. (Watching her try everything made me feel somewhat better.) The two cats cover, but Neko never does. (Territorial dominance marker by the smallest cat?)

We collected a week of baseline data, which showed that box 1 was twice as popular as box 4 and 5. W- hypothesized that it was because box 4 and box 5 were near the furnace, and the strange noises from the furnace might startle the cats occasionally. Leia pooped outside the box twice, both times sniffing all the boxes before going in the middle.

We took to calling Leia our little data-generator.

2014-03-29 Litterbox analysis #quantified

2014-03-29 Litterbox analysis #quantified

Since the cats often left a little bit of extra food in their bowls and the vet had suggested they needed less food or more exercise, we decided to try reducing the amount of food we gave them. That change seems to be going well.

We also moved box 5 closer to box 1. That led to box 5 being much more popular than it used to be, which was a pleasant surprise. If Leia likes box 5 a lot more now that it’s away from the furnace, maybe it’ll be easier for her to find a clean box to poop in.

Preliminary cat litter box results

Preliminary cat litter box results

We set the camera up to capture 2 frames per second in order to save space. Watching it in real-time eventually lost its novelty, so I looked up how to speed up the AVIs.

for FILE in video-*.avi; do
  if [ ! -f "fast-$FILE" ]; then
    ffmpeg -i $FILE -vf "setpts=0.10*PTS" -r 30 fast-$FILE
  fi
done

I also started looking into how to use SimpleCV for computer vision and image processing. I had a hard time getting SimpleCV set up in my Ubuntu virtual machine, but the Windows version worked fine after a lengthy install process on my computer. After much learning, I figured out how to identify changed areas, get the largest share over a certain area threshold, find the centroid of that shape, and plot it back on the image. The real challenge is figuring out some kind of visual output that makes sense to me when I look at it. The image below is a step in the right direction, but it’s still not quite what I need.

summary-113-20140329224621-00

The Raspberry Pi camera module arrived, so we swapped that in and eventually got everything working again after some SD/power-related grumbling.

It would be great if I could get Python to automatically figure out which cat is in the video, distinguishing between multiple cats and flagging it for manual review if the motion detection got confused. Even better if it can track the path that the cats take!

On the other hand, the speeded-up AVIs are now fast enough that the bottleneck isn’t waiting for the video to play, it’s me typing in the description of the path (since I track not only the litter box they use, but any other litter boxes they check along the way). Maybe this is fine.

While watching me encode data, W- said, “Isn’t this something you can have your assistants do?” It’s data entry, sure, but I feel embarrassed about assigning people to watch our cats poop. <laugh> Besides, I’m learning a lot from the encoding process. We’ll probably treat it as a time-limited experiment.

Pretty cool! =) Next steps: Collect more data, try more experimental changes, learn more about image processing…

Anticipated questions/responses:

  • That’s so geeky. You’re weird. Yup.
  • You have too much time on your hands. I like spending my time learning things, and getting better at computer vision and data analysis will come in handy. =) Better than watching TV or reading forgettable things. (See also: Too much time on her hands)
  • Just get a self-cleaning litter box. We got a Litter Robot because that was the best-reviewed of the self-cleaning litter boxes, but only Neko likes using it. We’re not into fancy litter boxes that require cartridges or special litter. It’s hard to tell if Leia will take to a new automated litter box, and returns/refunds for used litter boxes would be a bit weird. We can probably figure out something that works with our current setup, or maybe with an additional regular litter box. We still need to clean daily anyway, so it makes sense to try low-cost approaches first.
  • How about tracking both input and output? Too much work at the moment, and not enough interest in the data. (Weigh the food bowls? Weigh the boxes? Distinguish between cats?)
  • What about rigging up an automated water sprayer / Nerf gun to fire when cats poo outside the litter box? That will just make them poop outside the furnace room. It’s much easier to clean the furnace room than the carpet, so if they’re going to poop outside the litter box, that’s as good a place as any. We don’t want them to get any negative associations.
  • Mike Wilson

    Neat! I bought a couple raspberry pi units with enclosures with the intention of setting one up as a home automation server of some kind and the other as a little perl script daemon box that’ll feed my SAN with stuff from the internet. Haven’t gotten around to setting them up yet. Too many other projects.

    I’ve spent my entire career learning that the goal of any data-based effort has to be first to keep data sources clean and easy to access so that they can be enriched or decorated later (and to never apply such enrichments to the source data, only to ancillary foreign key linked tables.) It’s a silly little insight, but it’s made the life of me and many employers a lot easier.

    • http://sachachua.com sachac

      Right now I’m keeping my main tracking data in a spreadsheet. I’m already getting data drift – as I learn more about the kinds of questions I want to ask (and as I improve the capabilities of the system), I use slightly different notation…

  • Charles Cave

    There must be some university students who would be interested in your Pi + Webcam + software for data acquisition of animal behaviour! I hope you can implement some “cat-recognition” algorithms to fully automate your data acquisition. Maybe each cat could have a different colored collar – something like motion capture?

    • http://sachachua.com sachac

      I was going to go by blob size and average colour, but then we gave one of our cats a lion cut… Maybe it’s time to put them in coloured vests? ;)

  • Paul D

    I LOVE THIS!! This is probably the funniest & COOLEST thing I have seen in a long time.

    “We took to calling Leia our little data-generator.”

    I am going to use that!
    “Where’s Beck?”
    “I think he went to generate some data.”

    I got a Pi awhile back & could not think of anything really awesome to do with it… now I can!! Implant chips in the kids & let’rr rip!

    • http://sachachua.com sachac

      It’s all a matter of perspective… =D

  • furansui

    seconded! i love this too! cool!

    • http://sachachua.com sachac

      Glad you like it. Thanks for dropping by! =)

  • Thomas Worthington

    “Motion” is a particularly appropriate name for the application in use.

    • http://sachachua.com sachac

      Indeed! Also, we just switched to a top-down view, which is great because I can, ah, distinguish more easily between different types of litterbox use. ;)

  • foobar