Skip to main content
Back to Showcase

ESP32 Cat Water Counter: Knows Which Cat Drank and How Often

Author:emwstudioModel:MiniCPM-V 4.7

Recognizes which cat is drinking and counts its visits each day

Four cats in one home, three of them light-coloured and hard to tell apart. emwstudio built a drinking counter with two ESP32-S3 boards, a MacBook and MiniCPM-V 4.7: it records when each cat drinks, how many times today, and saves a short clip with sound for every visit.

Note

The models run on the MacBook side (a local 1B model plus a cloud 35B model). The ESP32 boards only capture video and audio and drive the desk display.

esp32-eye desk display demo

The camera sits on the bowl rim and looks up at the cat's face, so the water itself is not in view. Detection therefore does not look for water or licking; it looks for "a cat has entered the frame and fills a large part of it".

Features​

  • Live OV5640 video and microphone audio.
  • Background-difference ratio plus a local 1B model as a gate, to keep large-model calls down.
  • A cloud 35B model double-checks the drinking scene and identifies the cat against reference photos.
  • Stores event time, cat identity, today's count and an MP4 clip with audio.
  • The desk display shows a live video strip, four cat avatars, daily counts and progress bars, with an alert sound and highlight animation.
  • Stream reconnects, WebSocket heartbeat, tiered watchdogs, automatic UI recovery, and a daily reset at midnight.

Architecture​

Plain Text
ESP32-S3-CAM-OV5640 (camera + mic)
│ MJPEG / PCM over Wi-Fi
▼
MacBook FastAPI server
background-difference ratio
↓
local MiniCPM-V 4.7 1B is there a cat in the frame?
↓ consecutive valid frames
cloud MiniCPM-V 4.7 35B-A3B verify + pick one of four cats
↓
SQLite + MP4 clips
│ REST / WebSocket / MJPEG
▼
ESP32-S3-Touch-AMOLED-2.16 (video strip + four cat cards)

Detection and Identification​

Step 1: lightweight gate

  • Grayscale difference against an "empty bowl" background estimates how much of the frame the cat fills.
  • Checked every 1.5 s. The local 1B model is only called when the foreground ratio exceeds 15%; otherwise it is a negative vote.
  • The local MiniCPM-V 4.7 1B (llama.cpp, Q4_K_M) only answers "is there a cat?". A positive vote needs the 1B to see a cat and a ratio above 35%.
  • Three positive votes in a row start an event; four negative votes end it.

The 1B model does not make the final identification. In the author's tests the three light-coloured cats looked too similar for reliable four-way classification at 1B, so it is not used for offline identity.

Step 2: identity

After an event ends, the server samples frames and sends them to MiniCPM-V 4.7 35B-A3B, which:

  • checks the frames really show drinking at the bowl, and discards the event if not;
  • compares against each cat's reference photos and returns cat_id, confidence and is_drinking;
  • keeps unknown when unsure instead of forcing a match;
  • retries later if the cloud is unavailable, rather than passing off a 1B guess as confirmed.

Desk Display​

A single-page dashboard: a title bar with time and online status, a 480×200 video strip, and four cat cards with avatar, name, today's count and a /8 progress bar. When drinking starts, the display plays a local alert; when identity is confirmed, that cat's card lights up and its count ticks up.

Stalled video is handled in tiers: after 5 s it re-registers UDP and falls back to TCP, after 20 s it restarts the stream pipeline, after 60 s it reboots. If the UI loop misses its heartbeat for 20 s, the device reboots.

Running It​

You need Python 3.12+, PlatformIO, ffmpeg and llama.cpp, plus a ModelBest API key for cloud 35B identification.

Bash
# Local 1B: MiniCPM-V 4.7 1B + mmproj on port 8080
llama-server \
-m /path/to/MiniCPM-V-4_7-1B-Q4_K_M.gguf \
--mmproj /path/to/mmproj.gguf \
--port 8080

# Server
cp server/.env.example server/.env # camera URL, 1B URL and ID_MODEL_API_KEY
cd server && ./run.sh

Firmware for the two boards lives in firmware/cam and firmware/display. Copy each config.h.example to config.h, fill in Wi-Fi and the server address, then flash with PlatformIO:

Bash
cd firmware/cam && pio run -e esp32-s3-cam-ov5640 -t upload --upload-port /dev/cu.usbmodemXXXX
cd firmware/display && pio run -e amoled216 -t upload --upload-port /dev/cu.usbmodemXXXX

Hardware: Waveshare ESP32-S3-CAM-OV5640 (camera board), Waveshare ESP32-S3-Touch-AMOLED-2.16 (desk display), and an Apple Silicon MacBook.

Test Results​

  • 43 server tests pass.
  • 35B four-way pilot test: 8 out of 8 correct.
  • All 14 manually checked real drinking visits were recorded, and all 14 identities were correct; one non-drinking scene was filtered out during 35B verification.

The author notes these numbers only describe this test sample and are not a promise of long-term accuracy.

Known Limits​

  • No infrared night vision, so detection is not guaranteed with the lights off.
  • Identification depends on the cloud 35B model; without network or API access, confirmation is delayed.
  • Similar-looking cats can still be confused at odd angles, when partly hidden or in low light, so a manual correction path is needed.
  • It counts visits to the bowl, not the actual amount drunk, and is not for diagnosing illness.
  • The repo also has a demo mode for filming (fixed counts + looping video) that does not reflect real recognition.

Project info

Author
emwstudio