Skip to content

harshit-033/trafic_analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SmartFlow β€” Intelligent Traffic Monitoring System 🚦

SmartFlow Dashboard


SmartFlow is a real-time, computer-vision-powered smart traffic signal system. It uses YOLO object detection on live video feeds to count and classify vehicles, dynamically computes adaptive green-light timings based on actual traffic load, and presents everything through a professional glassmorphic dashboard. The system is fully self-healing β€” edge scripts monitor themselves, restart on failure, and send SMS alerts if a camera or node goes offline.


🌟 Current Status

Prototype β€” Feature Complete

Component Status
YOLO Inference Engine βœ… Working
FastAPI Backend + MongoDB βœ… Working
Adaptive Signal Timing Algorithm βœ… Working
React / Vite Dashboard βœ… Working
Heartbeat + Watchdog Edge Services βœ… Working
SMS Alerts via Twilio βœ… Working
Per-Lane ROI Detection πŸ”œ Planned
Multi-Junction Coordination πŸ”œ Planned

✨ Key Features

  • πŸ€– Real-Time Edge AI β€” YOLOv8/YOLO11 detects and classifies vehicles (car, bus, truck, bike, pedestrian) from video or RTSP camera feeds every frame.
  • 🚦 Adaptive Signal Timing β€” An algorithm weighs vehicle types by size/impact and calculates optimal green-phase durations per approach, replacing fixed-interval timers.
  • πŸ“Š Live Dashboard β€” A dark-themed, glassmorphic React web app with animated donut charts, area graphs, and bar charts β€” all polling live backend data every 5 seconds.
  • πŸ’“ Edge Health Monitoring β€” A heartbeat script reports CPU, memory, FPS, and camera status to the backend every 10 seconds.
  • πŸ• Watchdog Recovery β€” Automatically detects crashed inference or heartbeat processes and restarts them (up to 5 attempts before raising an alert).
  • πŸ“² SMS Alerts β€” Twilio integration sends an SMS instantly if a junction goes offline or a critical failure is detected.

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Edge Device (Camera)                   β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚   β”‚ inference3.py  β”‚   β”‚heartbeat.pyβ”‚   β”‚ watchdog.py β”‚ β”‚
β”‚   β”‚ (YOLO + OpenCV)β”‚   β”‚(Health HB) β”‚   β”‚(Auto-Restartβ”‚ β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚ POST /detections  β”‚ POST /heartbeat β”‚ POST /alert
            β–Ό                  β–Ό                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚             FastAPI Backend (src/backend/main.py)         β”‚
β”‚   β€’ Stores detections, heartbeats, alerts in MongoDB      β”‚
β”‚   β€’ Runs adaptive signal timing algorithm                 β”‚
β”‚   β€’ Serves REST API on http://localhost:8001              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚ Polls every 5s
                               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        React/Vite Dashboard (frontend/)                   β”‚
β”‚   β€’ http://localhost:5173                                 β”‚
β”‚   β€’ Shows live counts, timing, health, alerts, charts     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Getting Started

Prerequisites

Make sure the following are installed on your machine:

Tool Version Purpose
Git any Clone the repository
Python 3.10+ Backend & edge scripts
Node.js + npm 18+ Frontend dashboard
Docker + Docker Compose any Run MongoDB

Step 1 β€” Clone the Repository

git clone https://github.com/your-username/SmartFlow.git
cd SmartFlow

Step 2 β€” Configure Environment Variables

Copy the example env file and fill in your values:

cp .env.example .env

Edit .env with your settings:

MONGO_URI=mongodb://admin:admin123@localhost:27017/
TWILIO_SID=your_twilio_sid
TWILIO_AUTH=your_twilio_auth_token
TWILIO_FROM=+1xxxxxxxxxx
TWILIO_TO=+91xxxxxxxxxx

⚠️ If you don't have Twilio credentials, the system still works fully β€” SMS alerts will just be silently skipped.

Step 3 β€” Install Python Dependencies

# Create and activate a virtual environment
python -m venv venv

# Windows
venv\Scripts\activate

# macOS / Linux
source venv/bin/activate

# Install all dependencies
pip install -r requirements.txt

Step 4 β€” Install Frontend Dependencies

cd frontend
npm install
cd ..

πŸ–₯️ Running the Project

Start each component in its own terminal. Always activate the virtual environment (venv\Scripts\activate) before running Python scripts.

Terminal 1 β€” Database

docker compose -f infra/docker-compose.yml up -d

Terminal 2 β€” Backend API

python -m uvicorn src.backend.main:app --port 8001 --reload

API docs are available at http://localhost:8001/docs

Terminal 3 β€” Frontend Dashboard

cd frontend
npm run dev

πŸ‘‰ Open http://localhost:5173 in your browser.

Terminal 4 β€” AI Inference

python src/edge/inference3.py

Terminal 5 β€” Heartbeat Monitor

python src/edge/heartbeat.py

Terminal 6 β€” Watchdog

python src/edge/watchdog.py

πŸ“‚ Project Structure

SmartFlow/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ backend/           # FastAPI server, API routes, timing algorithm
β”‚   β”‚   β”œβ”€β”€ main.py        # Main application entry point
β”‚   β”‚   └── sms_utils.py   # Twilio SMS integration
β”‚   β”œβ”€β”€ edge/              # Hardware-side runtime scripts
β”‚   β”‚   β”œβ”€β”€ inference3.py  # YOLO inference + frame processing
β”‚   β”‚   β”œβ”€β”€ heartbeat.py   # System health reporter
β”‚   β”‚   └── watchdog.py    # Process monitor + auto-restart
β”‚   └── dashboard/         # (Legacy) Original Streamlit dashboard
β”œβ”€β”€ frontend/              # React + Vite dashboard (NEW)
β”‚   └── src/
β”‚       β”œβ”€β”€ App.jsx        # Main app with routing & live data polling
β”‚       β”œβ”€β”€ api.js         # Backend API client (fetch wrappers)
β”‚       └── index.css      # Glassmorphic dark theme styles
β”œβ”€β”€ infra/
β”‚   └── docker-compose.yml # MongoDB container setup
β”œβ”€β”€ models/                # Place your trained .pt model weights here
β”œβ”€β”€ data/                  # Sample video files for the inference engine
β”œβ”€β”€ ai/                    # Training experiments and datasets
β”œβ”€β”€ .env.example           # Environment variable template
β”œβ”€β”€ requirements.txt       # Python dependencies
└── Readme.md

πŸ”‘ Core Files Needed to Run (Pre-Trained Model)

If the model is already trained, you only need these to run the full system:

βœ… src/backend/
βœ… src/edge/
βœ… frontend/
βœ… infra/docker-compose.yml
βœ… models/best.pt           ← your trained YOLO model weights
βœ… data/sample2.mp4         ← camera feed video
βœ… .env
βœ… requirements.txt

The ai/ and experiments/ directories are only needed for research, re-training, or extending the model.


πŸ› οΈ Tech Stack

Layer Technology
Object Detection YOLO11 / YOLOv8 (Ultralytics)
Video Processing OpenCV
Backend FastAPI + Uvicorn
Database MongoDB 6 (via Docker)
Dashboard React 18 + Vite
Charts Recharts
Icons Lucide React
Alerting Twilio SMS
Monitoring psutil

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors