5.4 KiB
Repository Guidelines
Project Overview
Feedmill is a production edge counter for feed sacks and boxes. A Rockchip RK3588 NPU runs YOLO (RKNN) over an RTSP camera stream; ByteTrack tracks objects; a line-crossing rule increments counts. Counts persist to SQLite + JSON state; a Flask dashboard exposes batch control/reporting on the configured port (default 7000 via .env). Truck detections define the horizontal counting zone only and are not counted. Batches are unified: one batch tracks combined count plus separate sack_count/box_count.
Architecture & Data Flow
- Camera:
connect_stream(SOURCE)incounter_live_rknn.py:run()reads RTSP frames. - Optional motion gate skips inference when frame diff is below
MOTION_THRESHOLD. - Inference:
RKNNYOLO.__call__(RKNN on NPU) returns detections. - Detections filtered by active batch class IDs (
0=sack,1=box) and horizontal zone (zone_x1..zone_x2). - Tracking:
ByteTracker.updateassociates detections across frames. - Crossing:
crossed_line(prev_pos, pos, line_pos)plusis_duplicate_crossdedup; on crossing,BatchStore.record_counted_object_crossing(tid, object_label=...). - Persistence:
batch_store.pywritesbatches/daily_summariesandcurrent_batch.json. - Dashboard (
counter_dashboard.py) reads DB/state and proxies start/stop to the counter control server (counter_control.py,127.0.0.1:5001).
Batches: manual mode (operator start/stop; passing objects ignored while stopped) or auto mode (first crossing opens; idle timeout closes; timeout held while truck in zone). Peer-sync requests are ignored in auto mode.
Key Directories
/— runtime modules and config:counter_live_rknn.py,batch_store.py,counter_control.py,counter_dashboard.py,.env,config.env.example.tests/— stdlibunittestsuites.docs/— peer-sync and unified-batch specs/plans.templates/—dashboard.html(Jinja, Chart.js).static/img/— logos/icons./opt/models/—.rknnmodel files (outside repo).
Development Commands
# venv
python3 -m venv /opt/feedmill/venv
/opt/feedmill/venv/bin/pip install -r requirements.txt
# run counter
/opt/feedmill/venv/bin/python /opt/feedmill/counter_live_rknn.py
# run dashboard
/opt/feedmill/venv/bin/python /opt/feedmill/counter_dashboard.py
# tests
cd /opt/feedmill && /opt/feedmill/venv/bin/python -m unittest discover -s tests -v
# services
sudo systemctl status feedmill-counter feedmill-dashboard
sudo journalctl -u feedmill-counter -f
sudo ./install-services.sh
sudo ./uninstall-services.sh
# optional model conversion (needs rknn, torch, onnx, ultralytics)
/opt/feedmill/venv/bin/python convert_pt_to_rknn.py model.pt -o /opt/models/model.rknn --imgsz 480 --target rk3588
No build step, lint config, or package manifest beyond requirements.txt.
Code Conventions & Common Patterns
- Config constants:
UPPER_SNAKE_CASEloaded from env viaos.getenv(counter ~lines 27-121; dashboard ~32-52)..envloaded withpython-dotenv. - Classes:
PascalCase; functions/variables:snake_case. - Class IDs fixed: sack=0, box=1, truck=2; labels normalized to
sack/box/mixed. - State guarded by
BatchStore.state_lock; SQLite opened withcheck_same_thread=False. - Error handling: stream reconnect loop, model release on failure, try/except around DB ops, JSON error responses in dashboard.
- Atomic live-frame writes via temp file +
os.replace. - Dedup keys:
{label}:{track_id}incounted_event_ids. - Tests mock
rknnliteimport when unavailable; use temp SQLite/state files. - Peer API validation:
X-API-Keychecked only when header present; wrong/missing expected token returns 401.
Important Files
counter_live_rknn.py— main pipeline entry (if __name__ == "__main__": run()), RKNN wrapper, ByteTrack, crossing logic.batch_store.py— SQLite schema migration,BatchStore, batch lifecycle, cutoff watcher.counter_control.py— internal HTTP control server (/status,/start*,/stop).counter_dashboard.py— Flask app, reporting, XLSX export, peer sync.config.env.example/.env— all runtime configuration.requirements.txt— Python dependencies.feedmill-counter.service,feedmill-dashboard.service— systemd units.API.md,DEPLOY.md,README.md— API, deployment, overview docs.docs/peer_sync_handoff.md— FCP peer-sync contract.
Runtime/Tooling Preferences
- Python 3.12.3 in
/opt/feedmill/venv(system-site-packages false). - Dependencies:
numpy<2,rknn-toolkit-lite2,opencv-python,flask,python-dotenv,openpyxl. - RKNN runtime targeting RKNPU v2 (RK3588); models live in
/opt/models/*.rknn. - Install scripts require root; services run as root with
PYTHONNOUSERSITE=1. - Dashboard port comes from
.env(DASHBOARD_PORT, typically 7000); code default is 5000. - Internal control port defaults to 5001.
- No Node/Bun tooling; frontend is server-rendered Jinja + Chart.js.
Testing & QA
- Framework: stdlib
unittest; no pytest/conftest/CI config. - Suites:
tests/test_batch_store.py(12),tests/test_crossing_guards.py(5),tests/test_dashboard.py(15). - Run from
/opt/feedmillso top-level imports resolve. - Coverage gaps: inference loop/RKNN loading, camera capture, control-server error paths, peer forwarding, frontend JS, concurrency/migration against production DB, service scripts.
- Dashboard tests mutate module globals (
DB_PATH,CURRENT_BATCH_PATH) and create a persistent temp root at import.