GitHunt
AD

adamlerer/discipline

Discipline - Cat Food Bowl Guardian

A hardware/software system that ensures each cat eats only from their designated bowl. Uses computer vision to identify cats and triggers a water spray deterrent when the wrong cat eats from the wrong bowl.

The Problem

Ilana keeps eating Abbi's food. This system watches the food bowls and sprays Ilana with water if she tries to eat from Abbi's bowl before Abbi walks away.

How It Works

  1. Camera monitors the feeding area continuously
  2. When a cat approaches a bowl, the system identifies which cat it is (Abbi or Ilana)
  3. Tracks which cat is at which bowl
  4. If Ilana eats from Abbi's bowl while Abbi hasn't finished, activates the water sprayer
  5. Logs all feeding events for analysis

Hardware Required

Core Components

Component Description Approximate Cost
Raspberry Pi 4 (2GB or 4GB) Main controller running the vision system $45-75
Raspberry Pi Camera Module 3 Wide-angle camera for monitoring $25-35
12V Solenoid Valve (normally closed) Controls water flow $10-15
5V Relay Module Switches the solenoid valve $5-8
12V Power Supply (2A) Powers the solenoid $10-15
USB-C Power Supply (5V 3A) Powers the Raspberry Pi $10-15
Silicone tubing (1/4" inner diameter) Connects water source to nozzle $8-12
Spray nozzle Creates a fine mist spray $5-10
Water reservoir (1-2 gallon) Gravity-fed water source $10-15
Jumper wires Connections $5-8
Project enclosure Weatherproof box for electronics $15-20

Total estimated cost: $160-230

  1. Raspberry Pi 4 Model B (4GB) - Any official retailer
  2. Raspberry Pi Camera Module 3 Wide - For better field of view
  3. DIGITEN 12V 1/4" Solenoid Valve - Commonly available, food-safe
  4. HiLetgo 5V Relay Module - Optoisolated for safety
  5. 12V 2A DC Power Adapter - Standard barrel jack
  6. Misting Nozzle Kit - Garden misting nozzles work well

Optional Enhancements

  • IR illuminator - For nighttime operation
  • NoIR Camera Module - Better low-light performance
  • Pressure tank - For more consistent spray pressure
  • Flow sensor - To monitor water usage

Core Components

Component Best Price Amazon Alternative
Raspberry Pi 4 (4GB) $60 - Adafruit CanaKit Starter Kit - Amazon
Pi Camera Module 3 Wide $35 - Adafruit Camera Module 3 Wide - Amazon
12V Solenoid Valve (1/4" NC) - DIGITEN 12V Solenoid - Amazon
5V Relay Module (Opto-isolated) - HiLetgo 5V Relay - Amazon
12V 2A Power Supply - 12V 2A Adapter - Amazon
USB-C Power Supply (5V 3A) - Raspberry Pi 4 Power Supply - Amazon
Silicone Tubing (1/4" ID) - Food Grade Silicone Tubing 10ft - Amazon
Misting Nozzle Kit - Brass Misting Nozzles - Amazon
Jumper Wires - 120pcs Jumper Wire Kit - Amazon
Waterproof Enclosure $9.95 - Adafruit Sixfab IP65 Enclosure - Amazon

Adafruit (often cheaper):

Amazon (all-in-one shopping):

Wiring Diagram

                                    +12V Power Supply
                                           |
                                           v
[Raspberry Pi 4]                    [12V Solenoid Valve]
      |                                    ^
      | GPIO 17                            |
      v                                    |
[5V Relay Module] -----> Switches --------+
      |
      | VCC -> Pi 5V
      | GND -> Pi GND
      | IN  -> Pi GPIO 17

[Pi Camera] -----> CSI Port on Raspberry Pi

Water Flow:
[Reservoir] --> [Tubing] --> [Solenoid Valve] --> [Tubing] --> [Spray Nozzle]
                                                                    |
                                                                    v
                                                            [Aimed at Abbi's bowl]

Quick Start (Adam's Setup)

Web Interface: http://192.168.86.36:5000

Start the service:

ssh alerer@192.168.86.36
cd ~/discipline && source venv/bin/activate && nohup python3 -m discipline.main --no-spray > /tmp/discipline.log 2>&1 &

Check logs:

ssh alerer@192.168.86.36 "tail -f /tmp/discipline.log"

Stop the service:

ssh alerer@192.168.86.36 "pkill -f 'python3 -m discipline.main'"

Software Setup

1. Raspberry Pi OS Setup

# Flash Raspberry Pi OS (64-bit) to SD card using Raspberry Pi Imager
# Enable SSH and set up WiFi during imaging

# After first boot, update the system
sudo apt update && sudo apt upgrade -y

# Enable the camera
sudo raspi-config
# Navigate to: Interface Options -> Camera -> Enable

# Install required system packages
sudo apt install -y python3-pip python3-venv libcamera-apps python3-libcamera python3-picamera2

2. Project Installation

# Clone the repository
git clone https://github.com/adamlerer/discipline.git
cd discipline

# Create virtual environment (--system-site-packages required for picamera2)
python3 -m venv --system-site-packages venv
source venv/bin/activate

# Install Python dependencies
pip install -r requirements.txt

# Download the cat detection model (YOLOv8)
python scripts/download_model.py

3. Cat Training (Teaching the System Your Cats)

Before the system can identify Abbi and Ilana, you need to train it with photos of each cat.

# Capture training images for each cat
python scripts/capture_training_images.py --cat abbi --count 50
python scripts/capture_training_images.py --cat ilana --count 50

# Train the cat classifier
python scripts/train_classifier.py

4. Configuration

Edit config.yaml to set up your specific environment:

# Bowl positions (in pixel coordinates from camera view)
bowls:
  abbi:
    x: 200
    y: 300
    radius: 80
  ilana:
    x: 500
    y: 300
    radius: 80

# Spray settings
spray:
  duration_ms: 500  # How long to spray
  cooldown_s: 10    # Minimum time between sprays

# Camera settings
camera:
  resolution: [1280, 720]
  framerate: 30

5. Calibration

# Run the calibration tool to set bowl positions
python scripts/calibrate_bowls.py

6. Running the System

# Start the discipline system
python -m discipline.main

# Or run as a service (recommended for production)
sudo cp discipline.service /etc/systemd/system/
sudo systemctl enable discipline
sudo systemctl start discipline

7. Optional: Sound Deterrent

In addition to the water spray, you can enable a sound deterrent.

# Install audio support
pip install pygame

# Add a sound file (MP3 or WAV)
cp /path/to/your/sound.mp3 sounds/deterrent.mp3

Enable in config.yaml:

sound:
  file: "sounds/deterrent.mp3"
  volume: 0.7
  duration_ms: 2000
  cooldown_s: 5
  enabled: true

8. Optional: Web Admin Interface

Enable a web dashboard for live video streaming and remote control.

# Install web dependencies
pip install flask

Enable in config.yaml:

web:
  enabled: true
  host: "0.0.0.0"
  port: 5000

Then access the dashboard at http://<pi-ip>:5000. Features include:

  • Live video stream with cat identification overlays
  • Toggle spray/sound deterrents on/off
  • Test buttons for spray and sound
  • Real-time system status and event log

Project Structure

discipline/
├── README.md
├── requirements.txt
├── config.yaml
├── discipline/
│   ├── __init__.py
│   ├── main.py              # Main entry point
│   ├── camera.py            # Camera capture and processing
│   ├── cat_detector.py      # YOLO-based cat detection
│   ├── cat_identifier.py    # Identifies Abbi vs Ilana
│   ├── bowl_monitor.py      # Tracks cats at bowls
│   ├── sprayer.py           # Controls water sprayer
│   ├── sound_player.py      # Sound deterrent playback
│   ├── logger.py            # Event logging
│   └── web/
│       ├── __init__.py
│       ├── app.py           # Flask web application
│       ├── templates/
│       │   └── dashboard.html
│       └── static/
│           └── style.css
├── scripts/
│   ├── download_model.py
│   ├── capture_training_images.py
│   ├── train_classifier.py
│   └── calibrate_bowls.py
├── models/                   # Trained models stored here
├── sounds/                   # Sound files for deterrent
├── data/
│   └── training/            # Training images
└── logs/                    # Event logs

Safety Notes

  1. Water and Electronics: Keep all electronics in a waterproof enclosure away from the spray zone
  2. Electrical Safety: Use proper gauge wires and ensure all connections are secure
  3. Cat Safety: The spray should be a gentle mist, not a powerful jet. Test pressure before deployment
  4. Supervision: Monitor the system during initial deployment to ensure correct operation

Troubleshooting

Camera not detected

# Check camera connection
rpicam-hello --list-cameras

Relay not triggering

# Test GPIO manually
python -c "import RPi.GPIO as GPIO; GPIO.setmode(GPIO.BCM); GPIO.setup(17, GPIO.OUT); GPIO.output(17, True)"

Cats not being identified correctly

  • Ensure good lighting in the feeding area
  • Capture more training images from various angles
  • Check that cats are visually distinct (color, pattern, size)

License

MIT License - Feel free to use and modify for your own cat discipline needs!