Poopatron 3000

Tensorflow & OpenCV

The story

Disclaimer: I LOVE cats, but the situation was dire. The garden has become a poop battlefield; a social club for all the feline players in the area to chill, have an occasional fight and try to reach that goal of perfectly balancing one poop on top of another (I swear it happened, I have pictures because… honestly… skills!). It got so bad I was worried about a house invasion by Golgothan himself. Something had to be done.

The distinguished Country Living lifestyle and home magazine lists 9 simple tips to stop cats from pooping in your garden. I tried 7 of them because throwing banana peels on the ground was a bit too retro gaming even for geeky self, and if I have to put out a litterbox for the neighbourhood cats it just feels like not fixing the problem but coping with it. Rest of them... LIES!

I saw cats eating lavender, rolling in peppermint, jumping around on twigs. Apart from the fact that I could actually “hear” the sound repellent, cats would just stare at it with bewilderment and go about their business. The motion activated sprinkler did exactly what is said on the box, activated on motion… with every wind gust… every 2 min.

I approached the problem the only way I know, by over thinking and over engineering the solution. Enter: Poopatron 3000

What is it?

In a nutshell, it’s a haphazardly connected system of devices which:

  • Ingests a security camera image of the garden
  • Analyses frames for movement with Frigate NVR
  • If movement is detected the frame is analysed using OpenCV and Tensorflow for cat object from Google Coral models
  • If confidence on the match is > 70% an MQTT message is sent to the Home Assistant server instance
  • Home Assistant server triggers an automation when a match MQTT message is received from Frigate
  • The automation switches on a smart plug waits 3 seconds and switches it off
  • On the garden side there is a hose pipe connected to a sprinkler which when activated covers the whole area of the garden
  • A solenoid valve is connected to the hose, when it’s switched on allows the flow of water
  • When the smart plug is switched on and the solenoid is powered the valve opens and activates the sprinkler for 3 seconds
  • Frigate saves a clip from the camera from the moment movement is detected until object has been matched and sends it to home assistant

Diagram

Here’s a quick diagram to demonstrate how the hardware is connected:

Diagram

Frigate

I’ve been following the work of Blake Blackshear’s https://github.com/blakeblackshear on Frigate for a long time now. It’s a complete and local NVR designed for Home Assistant with AI object detection. Uses OpenCV and Tensorflow to perform real time object detection locally for IP cameras.

Frigate is really at the centre of the Poopatron. It’s a really robust system which supports even the most minimal setup and can scale really easily. I run the docker image on a Raspberry PI4 which also contains my Home Assistant instance, a local server and a bunch of other docker images and the CPU is surprisingly stoic about the load (although Coral TPU is recommended - good luck getting your hands on one of them!)

Setting up

To set up your own Poopatron 3000 you’ll need a few things:

Hardware-y things:

  • Solenoid valve - I used the cheapest 12V Normally Closed on I could find
  • 12 V power supply - I had one kicking around, but any cheap one will do
  • Lawn sprinkler - this will depend on your garden and the area you want to cover. I’d suggest an impact brass sprinkler like this because the sound itself is enough to startle the poop bandits
  • Security camera - check out the camera recommendations section on Frigate hardware. I’ve tested this with 3 different cheap cameras and they all work. Just ensure the streams available are not too high resolution. 720p is plenty!
  • Smart plug - again this can be any type of smart plug as long as you can switch it off and on from Home Assistant
  • Some computer/server/instance to run it all on. I’m using an rpi4 with dietpi.

Software-y things:

  • Home Assistant - I’ll refer you to official installation docs to get going. This doesn’t necessarily need to be the central hub. Any server which is capable of receiving MQTT traffic and managing a smart plug will do. For this case I’ll focus on HA
  • Frigate - you can install Frigate as an add-on in HA or as a standalone container in Docker
  • MQTT - You can run your own private instance of Mosquitto Broker by adding an integration in Home Assistant
  • Smart Plug integration - There’s a plethora of smart plugs out there so you need to follow the instructions for integration the one you have into Home Assistant. Once it’s configured you will have an entity to control on/off state

Configuration

Firstly we need to configure Frigate. In Home Assistant config/ folder create frigate.yaml file. Depending on the hardware and software you selected your configuration may vary. Please refer to the manual My configuration file (using a reolink camera and HA mqtt addon) looks like this:

frigate.yaml

mqtt:
  host: //name of your mqtt host
  port: 1883
  topic_prefix: frigate
  client_id: frigate
  user: //username for your mqtt host
  password: //password for your mqtt host

cameras:
  reolink: //name of your camera
    ffmpeg:
      input_args:
        - -avoid_negative_ts
        - make_zero
        - -fflags
        - +genpts+discardcorrupt
        - -flags
        - low_delay
        - -strict
        - experimental
        - -analyzeduration
        - 1000M
        - -probesize
        - 1000M
        - -rw_timeout
        - "5000000"
      inputs:
        - path: //path to your camera rtmp feed
          roles:
            - record
            - rtmp
            - detect

    detect:
      width: 1240
      height: 720
      fps: 7
      
    record:
      enabled: True
      retain:
        days: 7
        mode: motion
      events:
        retain:
          default: 14
          mode: active_objects

detectors:
  cpu1:
    type: cpu

objects:
    track:
        - cat
    filters:
        cat:
            # Optional: minimum score for the object to initiate tracking (default: shown below)
            min_score: 0.5
            # Optional: minimum decimal percentage for tracked object's computed score to be considered a true positive (default: shown below)
            threshold: 0.7

Once you have Frigate up and running it’s time to configure the Home Assistant automation. Make a note of the entity id of your smart plug. Make sure it’s connected to HA and working.

automations.yaml

- id: '' //enter unique id 
  alias: //name it however you like
  description: “
  trigger:
  - platform: mqtt
    topic: frigate/events
  condition:
  - condition: template
    value_template: '{{ trigger.payload_json[''after''][''label''] == ''cat'' }}'
  - condition: template
    value_template: '{{ trigger.payload_json[''after''][''camera''] == ''reolink''
      }}' //change this name to the camera configured in Frigate
  - condition: template
    value_template: '{{ trigger.payload_json[''after''][''top_score''] > 0.70 }}'
  action:
  - type: turn_on
    device_id: //your switch entity id
    entity_id: //enter the name of the smart plug entity gere
    domain: switch
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - type: turn_off
    device_id: //your switch entity id
    entity_id: //enter the name of the smart plug entity gere
    domain: switch
  mode: single

RESULT

cat scarer in action

Also... NOT A POOP IN SIGHT since the Poopatron has been installed... I just jinxed it didn't I?

Copyright 2023 @Everybodytothelimit