Skip to content

About

Count people and vehicles crossing a line, entirely on a Raspberry Pi 5 — YOLOv11n (NCNN) + ByteTrack with a built-in web interface

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Line Counter Edge Device

CI

Real-time traffic/people counting system for Raspberry Pi 5 with Camera Module v3. Uses YOLOv11n (NCNN optimized) + ByteTrack for detection and tracking with a web-based interface. Achieves ~10 FPS on Raspberry Pi 5.

Runs entirely on the device — no cloud service, no account, no internet connection required. Point it at a doorway or a road, draw a line in the browser, and it counts what crosses it.

Features

  • Real-time Detection: YOLOv11n NCNN with ByteTrack multi-object tracking
  • Dual Mode: Person counting (libraries, retail) or Vehicle counting (parking, traffic)
  • Virtual Line Crossing: Configurable counting line with IN/OUT direction detection
  • ROI Support: Optional Region of Interest to restrict detection area
  • Web Interface: Live MJPEG stream, interactive config, and interval monitor
  • Drag & Drop Config: Interactive line/ROI positioning on live video
  • Interval Recording: Periodic IN/OUT snapshots (configurable interval)
  • Persistent Storage: Configuration and counts survive reboots

Hardware Requirements

  • Raspberry Pi 5 (4GB+ RAM recommended)
  • Camera Module v3 (Standard or Wide)
  • MicroSD card (32GB+ recommended)

Installation

1. System Dependencies (Pi OS)

sudo apt update
sudo apt install -y python3-pip python3-venv libcamera-dev

2. Create Virtual Environment

cd ~/PiLineCounter
python3 -m venv venv
source venv/bin/activate

3. Install Python Packages

pip install -r requirements.txt

4. Export YOLO Model to NCNN

python -c "from ultralytics import YOLO; YOLO('yolo11n.pt').export(format='ncnn')"

5. Verify Camera

libcamera-hello --list-cameras

Usage

Start the Server

# Live camera
python app.py

# Video file
python app.py --video /path/to/video.mp4

# With interval monitor (default: 5s interval)
python app.py --monitor

# Custom interval
python app.py --monitor --interval 10

Access the web interface at http://<pi-ip>:5000

CLI Flags

Flag Default Description
--video PATH — Use video file instead of camera
--monitor off Show the interval records panel in the interface
--interval N 5 Interval seconds for JSON records

Interface

The whole app lives on one page at http://<pi-ip>:5000:

  • Live view with the counting line and region of interest drawn on top of the stream
  • Drag the line endpoints directly on the video, or type exact pixel coordinates
  • Counters for in, out and net, updating once per second
  • Detection controls — people or vehicles, and a switch to swap which side counts as in
  • Region of interest — draw a box on the video to restrict detection and gain frame rate
  • Interval records table when started with --monitor

Every change is applied and persisted immediately; there is no save button. /config and /monitor redirect to / and remain only so old bookmarks keep working.

API Reference

GET /api/data

Returns current state.

{
  "counts": {"in": 42, "out": 38},
  "line": [427, 0, 427, 480],
  "mode": "person",
  "flip_direction": false,
  "roi": {"enabled": false, "x": 0, "y": 0, "width": 854, "height": 480},
  "fps": 9.8,
  "resolution": [854, 480],
  "monitor": false,
  "interval": 5
}

The interface boots entirely from this endpoint — resolution, current line, mode and settings all come from here, so index.html needs no server-side templating.

POST /api/config

Update line coordinates, detection mode, and/or direction.

curl -X POST http://localhost:5000/api/config \
  -H "Content-Type: application/json" \
  -d '{"line": [427, 0, 427, 480], "mode": "person", "flip_direction": false}'

POST /api/roi

Update Region of Interest.

curl -X POST http://localhost:5000/api/roi \
  -H "Content-Type: application/json" \
  -d '{"enabled": true, "x": 100, "y": 50, "width": 600, "height": 380}'

POST /api/reset

Reset counters to zero.

curl -X POST http://localhost:5000/api/reset

GET /api/records

Get last 50 interval records (requires --monitor).

[
  {"from": "2026-04-01T14:23:00", "to": "2026-04-01T14:23:05", "in": 3, "out": 1},
  ...
]

Configuration

Settings are stored in config.json, created automatically on first run and gitignored:

{
  "line": [427, 0, 427, 480],
  "mode": "person",
  "flip_direction": false,
  "roi": null,
  "counts": {"in": 0, "out": 0}
}
Field Description
line Virtual line coordinates [x1, y1, x2, y2]
mode "person" (class 0) or "vehicle" (classes 2, 3, 5, 7)
flip_direction Swap IN/OUT assignment
roi ROI dict {enabled, x, y, width, height} or null
counts Persistent IN/OUT counters (reset on startup)

Counting Logic

  • Reference Point: Bottom-center of bounding box (foot/wheel position)
  • Direction Detection: Vector cross-product relative to line direction
  • flip_direction: Swaps which side counts as IN vs OUT

Run as Service

To start the counter on boot, create /etc/systemd/system/line-counter.service:

[Unit]
Description=Line Counter - people and vehicle counting service
After=network.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/PiLineCounter
ExecStart=/home/pi/PiLineCounter/venv/bin/python -u /home/pi/PiLineCounter/app.py
Restart=on-failure
RestartSec=5

# config.json is rewritten at runtime, so the app needs write access
ProtectSystem=full
ReadWritePaths=/home/pi/PiLineCounter

[Install]
WantedBy=multi-user.target

Adjust User and the paths if the project lives somewhere else, then:

sudo systemctl daemon-reload
sudo systemctl enable --now line-counter
journalctl -u line-counter -f

Project Structure

PiLineCounter/
├── app.py                 # Flask server: routes, MJPEG stream, REST API
├── camera_processor.py    # Capture, inference and tracking thread
├── counting.py            # Line-crossing geometry
├── tracks.py              # Track memory, with stale entries evicted
├── overlay.py             # What gets drawn on the frame
├── config.py              # config.json persistence
├── index.html             # The entire web interface (no build step, no CDN)
├── requirements.txt
├── pyproject.toml         # pytest and ruff configuration
└── tests/
    ├── conftest.py        # Stubs the camera stack
    ├── test_counting.py   # Crossing rules
    ├── test_tracks.py     # Track memory and eviction
    ├── test_config.py     # Persistence
    └── test_api.py        # HTTP surface

camera_processor.py owns the pipeline; the three modules beside it own one concern each. counting.py decides what crosses the line and in which direction, overlay.py decides what an operator sees, and config.py deals with the file on disk. Only camera_processor.py and overlay.py touch OpenCV, so the counting rules and the persistence can both be tested without a camera.

Development

pip install pytest ruff
pytest              # 47 tests, no camera or model required
ruff check .        # lint
ruff format .       # format

The test suite stubs OpenCV, ultralytics and picamera2 in tests/conftest.py, so it runs anywhere in under a second without the multi-gigabyte inference stack. CI runs the lint and the tests on Python 3.11, the version Raspberry Pi OS ships.

Performance

Metric Value
Resolution 854x480
Frame Rate ~10 FPS (with NCNN inference)
Model YOLOv11n NCNN
Tracker ByteTrack

Troubleshooting

Camera not detected

# Check camera connection
libcamera-hello

# Verify in config.txt
sudo nano /boot/firmware/config.txt
# Ensure: camera_auto_detect=1

Low frame rate

  • Ensure NCNN model is used (yolo11n_ncnn_model/ folder must exist)
  • Re-export if missing: python -c "from ultralytics import YOLO; YOLO('yolo11n.pt').export(format='ncnn')"
  • Reduce resolution in camera_processor.py
  • Ensure adequate cooling for Pi 5

Model not found

# Export YOLOv11n to NCNN format
python -c "from ultralytics import YOLO; YOLO('yolo11n.pt').export(format='ncnn')"
# This creates yolo11n_ncnn_model/ directory

License

MIT

About

Count people and vehicles crossing a line, entirely on a Raspberry Pi 5 — YOLOv11n (NCNN) + ByteTrack with a built-in web interface

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages