The backend serves as the central processing engine of the AI Traffic Management System.
It is responsible for:
- Video ingestion and validation
- Vehicle detection using YOLOv4
- Traffic signal optimization via a C++ Genetic Algorithm
- Reinforcement Learning refinement
- Serving results through a RESTful Flask API
- Persisting logs and analytics data
The backend coordinates communication between the perception layer (vision) and the decision layer (optimization).
backend/
│
├── app.py # Flask application entry point
├── Algo.cpp # Genetic Algorithm implementation (C++)
├── Algo # Compiled optimizer binary
├── yolov4.py # YOLO detection wrapper
├── rl_agent.py # Reinforcement Learning refinement module
├── uploads/ # Temporary uploaded videos
├── outputs/ # Processed results
├── data/ # CSV analytics and logs
├── requirements.txt # Python dependencies
└── download.sh # YOLO weight downloader
- Python 3.10+
- GCC compiler with OpenMP support
- YOLOv4 weight files
Important
Ensure GCC supports -fopenmp for parallel optimization execution.
Navigate to backend directory:
cd backendInstall Python dependencies:
pip install -r requirements.txtCompile the C++ Genetic Algorithm:
g++ -std=c++17 -O3 -Wall -Wextra -fopenmp -o Algo Algo.cppDownload YOLO weights:
bash download.shStart the Flask server:
python app.pyBackend will be accessible at:
http://localhost:5000
Uploads four traffic lane videos for optimization.
- Content-Type:
multipart/form-data - Key:
videos - Expected: 4 video files (one per lane)
{
"lane_1": 30,
"lane_2": 20,
"lane_3": 25,
"lane_4": 15
}Each value represents optimized green-light duration (in seconds).
Checks system readiness and operational status.
200 OK→ Backend is alive and dependencies are available- Confirms:
- YOLO weights exist
- C++ optimizer binary exists
- Flask server running
Note
This endpoint is primarily used to verify whether the backend service is alive and ready to process requests.
Returns aggregated analytics data from CSV logs.
- Aggregated performance metrics
- Historical optimization statistics
- System efficiency summaries
- Model: YOLOv4
- Framework: OpenCV (DNN)
- Process frames from uploaded videos
- Detect and classify vehicles
- Count vehicles per lane
- Generate density vector for optimization
Parallel frame processing is used where possible to maximize throughput.
- Language: C++17
- Executable:
Algo - Parallelization: OpenMP
The compiled C++ binary receives vehicle density values as input and performs a Genetic Algorithm simulation.
- Faster execution compared to interpreted Python
- Saves Compiled time.[cuz , here the main thing is performance]
- Reduced computation time for large population simulations
- Efficient multi-threaded execution using OpenMP
- Direct CPU-level optimization
The optimizer:
- Evolves candidate signal timing configurations
- Minimizes total vehicle waiting time
- Returns the best candidate solution
Important
The binary executable is used to reduce computation overhead and improve optimization speed.
The RL module (rl_agent.py) refines the GA output.
- Prevent starvation of low-density lanes
- Adjust timings based on:
- Historical optimization logs
- Time-of-day patterns
- Traffic distribution trends
The RL agent ensures fairness and long-term adaptability.
Optimization results are stored inside:
backend/data/
results.csv→ Logs individual optimization runsanalytics.csv→ Aggregated performance metrics
Important
For production-scale deployment, consider replacing CSV storage with a database system.
- C++ optimizer uses multi-threading via OpenMP.
- YOLO inference may benefit from GPU acceleration.
- Large video uploads may impact memory usage.
- Consider asynchronous processing for scalability.
- Live camera (RTSP) integration is currently under development.
- GPU acceleration support depends on deployment configuration.
- CSV-based logging is not suitable for distributed scaling.
Note
Live camera mode algorithm improvements are actively being developed.
- Async job queue (Celery / Redis)
- GPU-accelerated YOLO inference
- Database-backed analytics
- Distributed optimization across multiple intersections
- Advanced RL policy learning