OptiTraffic is a microservices-based smart traffic control demo powered by real-time Kafka streaming, deep learning (LSTM), and CityFlow traffic simulation. It features secure JWT-based APIs and a React-based dashboard for visualization and monitoring.
docker/ → Docker Compose setup & API Gateway
microservices/ → Flask-based microservices
├── login_service/ → Issues JWT tokens for authentication
├── traffic_signal/ → LSTM-based green light predictions
├── traffic_monitoring/ → Aggregates and analyzes traffic data
├── notification/ → Sends traffic alerts via email
├── simulator/ → WebSocket service for frontend replay
cityflow_simulation/ → CityFlow runner producing Kafka messages
ai_model/ → LSTM training and preprocessing scripts
frontend/ → React dashboard to monitor traffic
-
Install Docker and Docker Compose.
-
Launch all services from the
docker/directory:docker-compose up --build
-
Access the frontend at http://localhost:3000.
-
Use the login form to get a JWT (credentials are in
microservices/login_service/login_api.py). -
The API Gateway listens on port
8080and proxies requests to internal services.
- Path:
microservices/login_service/login_api.py - Purpose: Issues JWT tokens on login.
- Endpoint:
@app.route('/api/login/', methods=['POST']) def login(): ...
- Path:
microservices/traffic_signal/traffic_signal_predictor.py - Purpose: Predicts green-light road using LSTM or heuristic based on real-time Kafka traffic snapshots.
- Logic:
if current_mode == 'auto' and lstm_model is not None and len(snapshot_buffer) == SEQUENCE_LENGTH: green_light_road = predict_green_light_with_lstm(snapshot_buffer) else: green_light_road = heuristic_predict_traffic_light(summary)
- Path:
microservices/traffic_monitoring/traffic_monitoring.py - Purpose: Builds analytics like average speed, density, and green-light distribution from raw and predicted data.
- Path:
microservices/notification/notification.py - Purpose: Subscribes to traffic alerts via Kafka and sends email notifications for accidents or congestion.
- Path:
microservices/simulator/simulator.py - Purpose: Hosts WebSocket endpoints for replaying simulation data in the frontend dashboard.
LSTM model training and preprocessing scripts live in ai_model/:
-
Preprocessing:
Converts simulation logs to NumPy arrayspython preprocess_data.py
-
Training:
Trains a Sequential LSTM model for green-light predictionmodel = Sequential([ LSTM(32, input_shape=(sequence_length, input_dim), return_sequences=False), Dense(16, activation='relu'), Dense(num_roads, activation='softmax') ]) model.fit(X_train, y_train_cat, epochs=20, batch_size=32, validation_data=(X_test, y_test_cat))
-
The final model (
traffic_signal_model.h5) is consumed bytraffic_signal.
- Microservices: Flask + REST
- Message Queue: Kafka
- Simulation Engine: CityFlow
- ML: LSTM (Keras + TensorFlow)
- Authentication: JWT
- Frontend: React
- DevOps: Docker, Docker Compose
- Security: API Gateway with token validation
- Realtime: WebSocket streaming for simulation