Skip to content

bigalex95/Smart-Counter

Repository files navigation

πŸš— Smart-Counter: Edge AI Traffic Analytics

License: MIT C++ Python Docker

High-performance people counting system with C++ (OpenCV, ONNX Runtime) + YOLOv8. Designed for edge devices with real-time analytics dashboard.

Most "AI video projects" stop at a notebook demo. Smart-Counter is built for production deployment β€” optimized C++ inference engine, persistent analytics storage, and Dockerized deployment.


πŸš€ Key Features

  • ⚑ Real-Time Detection – YOLOv8 on GPU using ONNX Runtime (C++) achieving ~100 FPS on RTX 3060
  • 🎯 Bi-Directional Counting – Tracks both entry (IN) and exit (OUT) flows with virtual counting lines
  • πŸ’Ύ Data Persistence – SQLite database logs all analytics with automatic drift protection
  • πŸ“Š Live Dashboard – Streamlit-based real-time visualization with historical analytics
  • 🐳 Fully Dockerized – One-command deployment with Docker Compose (CPU/GPU support)
  • πŸ”§ Production Ready – Modular architecture, error handling, and comprehensive logging

πŸ›  Tech Stack

Component Technology
Core Engine C++17, OpenCV 4.8+
AI Inference ONNX Runtime (CUDA Execution Provider)
Detection Model YOLOv8 (Ultralytics)
Tracking Custom Centroid Tracker with state memory
Database SQLite3 with analytics schema
Dashboard Python 3.9+, Streamlit, Pandas
Build System CMake 3.10+
DevOps Docker, Docker Compose

πŸ— Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Video Source β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     C++ Detector (YOLO + Tracker)       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Detect β”‚β†’ β”‚  Track  β”‚β†’ β”‚  Count  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  SQLite DB  β”‚  ← Persistent Analytics
    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Python Dashboard β”‚  ← Real-time Visualization
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Detailed documentation: docs/ARCHITECTURE.md | docs/BI_DIRECTIONAL_COUNTING.md


⚑ Quick Start

Prerequisites

  • Docker & Docker Compose
  • NVIDIA GPU + NVIDIA Container Toolkit (for GPU acceleration)
  • Linux (Ubuntu 20.04+, or similar)

🐳 Docker Deployment (Recommended)

# 1. Clone repository
git clone https://github.com/bigalex95/Smart-Counter.git
cd Smart-Counter

# 2. Allow X11 forwarding (for visualization)
xhost +local:docker

# 3. Build and run
docker compose up --build

The dashboard will be available at http://localhost:8501

GPU Support: See docs/DOCKER_COMPOSE_GPU.md
Configuration: Edit .env file or use environment variables

πŸ”§ Native Build (Development)

For development or edge deployment without Docker:

# 1. Build C++ engine
./scripts/build.sh

# 2. Run detector
./build/SmartCounter --model models/yolov8s.onnx \
                     --input data/videos/video.mp4 \
                     --db logs/analytics.db

# Or use combined script
./scripts/build_and_run.sh

Available scripts:

  • build.sh – Build C++ project
  • run.sh – Run the detector
  • build_and_run.sh – Build and run in one step
  • check_cuda.sh – Check CUDA availability
  • test_database.sh – Test database connection

See docs/CLI_USAGE.md for all CLI options.


πŸ“‚ Project Structure

Smart-Counter/
β”œβ”€β”€ src/                    # C++ source code (Detector, Tracker, Database)
β”‚   β”œβ”€β”€ main.cpp            # Main application entry
β”‚   β”œβ”€β”€ detector.cpp        # YOLO inference engine
β”‚   β”œβ”€β”€ tracker.cpp         # Centroid tracking algorithm
β”‚   └── database.cpp        # SQLite analytics logger
β”œβ”€β”€ include/                # C++ headers
β”œβ”€β”€ dashboard/              # Python Streamlit analytics dashboard
β”‚   β”œβ”€β”€ app.py              # Real-time dashboard UI
β”‚   └── Dockerfile          # Dashboard container
β”œβ”€β”€ python/                 # Python utilities
β”‚   β”œβ”€β”€ prototype.py        # Python prototype (testing)
β”‚   └── convert.py          # ONNX model conversion
β”œβ”€β”€ scripts/                # Build and deployment automation
β”œβ”€β”€ models/                 # ONNX models (YOLOv8)
β”œβ”€β”€ data/                   # Videos and output
β”œβ”€β”€ logs/                   # SQLite database (analytics.db)
β”œβ”€β”€ docs/                   # Comprehensive documentation
β”œβ”€β”€ docker-compose.yml      # Multi-container orchestration
β”œβ”€β”€ Dockerfile              # C++ backend container
└── CMakeLists.txt          # Build configuration

🎯 Usage Examples

Docker Compose Deployment

# Run with custom video and settings
MODEL_PATH=models/yolov8s.onnx \
INPUT_VIDEO=data/videos/my_video.mp4 \
HEADLESS_MODE=true \
docker compose up

Standalone C++ Detector

./build/SmartCounter \
  --model models/yolov8s.onnx \
  --input data/videos/video.mp4 \
  --output data/output/result.mp4 \
  --db logs/analytics.db \
  --headless \
  --loop

Python Dashboard (Standalone)

cd dashboard
streamlit run app.py -- --db ../logs/analytics.db

Python Prototype (Testing)

source venv/bin/activate
python python/prototype.py

πŸ”§ Configuration

Environment Variables (Docker)

MODEL_PATH=models/yolov8s.onnx     # Model path
INPUT_VIDEO=data/videos/video.mp4  # Input video
OUTPUT_VIDEO=data/output/out.mp4   # Output video
DB_PATH=logs/analytics.db          # Database path
HEADLESS_MODE=true                 # No GUI display
LOOP_VIDEO=true                    # Loop video playback
USE_CPU=false                      # Force CPU inference

CLI Arguments (Native)

./build/SmartCounter --help

Options:
  --model PATH      Path to ONNX model
  --input PATH      Input video file
  --output PATH     Output video file (optional)
  --db PATH         SQLite database path
  --headless        Run without GUI
  --loop            Loop video playback
  --cpu             Use CPU instead of GPU

See docs/CLI_USAGE.md for advanced configuration.


πŸ“Š Performance Benchmarks

Component Performance
Inference (GPU) ~100 FPS (YOLOv8s)
Full Pipeline ~50-80 FPS
Latency < 20ms per frame
Memory ~2GB GPU / ~500MB CPU

Tested on NVIDIA GeForce RTX 3060 Laptop GPU with YOLOv8s

Model Comparison

Model Speed Accuracy Recommended For
yolov8n ⚑⚑⚑⚑⚑ ⭐⭐⭐ Edge devices, high FPS
yolov8s ⚑⚑⚑⚑ ⭐⭐⭐⭐ Balanced (default)
yolov8m ⚑⚑⚑ ⭐⭐⭐⭐⭐ Higher accuracy
yolov8l ⚑⚑ ⭐⭐⭐⭐⭐⭐ Maximum accuracy

πŸš€ Deployment Options

🐳 Docker (Production)

# CPU-only deployment
docker compose up

# GPU deployment
docker compose -f docker-compose-gpu.yml up

🌐 Cloud Platforms

  • AWS: ECS/EKS with GPU instances
  • GCP: Cloud Run / GKE with T4/V100
  • Azure: Container Instances with GPU

πŸ”Œ Edge Devices

  • NVIDIA Jetson (Nano, Xavier, Orin) – Optimized for edge AI
  • Intel NUC – CPU inference mode
  • Custom hardware – Via ONNX Runtime compatibility

See docs/DEPLOYMENT.md for detailed deployment guides.


🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments

  • Ultralytics for YOLOv8
  • Microsoft for ONNX Runtime
  • OpenCV community
  • All open-source contributors

πŸ“« Contact

Alibek Erkabayev - @bigalex95

Project Link: https://github.com/bigalex95/Smart-Counter


πŸ“š Documentation


🎯 What Makes This Different?

Most computer vision projects are:

  • ❌ Python-only (slow, not production-ready)
  • ❌ No tracking (just detection)
  • ❌ No persistence (analytics lost on restart)
  • ❌ No deployment story (hard to run)

Smart-Counter is:

  • βœ… Production C++ – Optimized for real-world performance
  • βœ… Complete Pipeline – Detection β†’ Tracking β†’ Counting β†’ Analytics
  • βœ… Data Persistence – SQLite with automatic logging
  • βœ… Deploy Anywhere – Docker, cloud, edge devices

βœ… Current Implementation

What's Working Now:

  • βœ… C++ Inference Engine – YOLOv8 ONNX Runtime with GPU/CPU support
  • βœ… Custom Centroid Tracker – Simple, fast tracking algorithm
  • βœ… Bi-Directional Counting – Tracks IN/OUT flows across counting line
  • βœ… SQLite Database – Persistent analytics storage with drift protection
  • βœ… Streamlit Dashboard – Real-time visualization and historical data
  • βœ… Docker Deployment – Multi-container setup with docker-compose
  • βœ… CLI Interface – Full command-line control with multiple options
  • βœ… Video Recording – Output processed video with annotations

🚧 Roadmap (Coming Soon)

Planned Improvements:

  • πŸ”œ Advanced Tracking – Replace simple tracker with BoTSORT/ByteTrack
  • πŸ”œ Multi-Zone Support – Define multiple counting zones
  • πŸ”œ Heatmap Generation – Visualize traffic patterns
  • πŸ”œ REST API – HTTP API for integration with other systems
  • πŸ”œ WebSocket Streaming – Real-time video feed to dashboard
  • πŸ”œ Model Optimization – TensorRT support for even faster inference
  • πŸ”œ Multi-Camera Support – Process multiple video streams
  • πŸ”œ Alert System – Notifications for crowd thresholds
  • πŸ”œ Time-Series Analytics – Advanced statistical analysis
  • πŸ”œ Cloud Storage Integration – S3/GCS for video archival

Contributions welcome! See Contributing section.


Built with ❀️ for production ML deployment

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published