Skip to content

shiven424/OptiTraffic-AI-Microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OptiTraffic AI Microservices

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.


🧱 Repository Structure

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  

🚀 Quick Start

  1. Install Docker and Docker Compose.

  2. Launch all services from the docker/ directory:

    docker-compose up --build
  3. Access the frontend at http://localhost:3000.

  4. Use the login form to get a JWT (credentials are in microservices/login_service/login_api.py).

  5. The API Gateway listens on port 8080 and proxies requests to internal services.


🔐 Microservice Overview

🟢 login_service

  • Path: microservices/login_service/login_api.py
  • Purpose: Issues JWT tokens on login.
  • Endpoint:
    @app.route('/api/login/', methods=['POST'])
    def login():
        ...

🚦 traffic_signal

  • 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)

📊 traffic_monitoring

  • Path: microservices/traffic_monitoring/traffic_monitoring.py
  • Purpose: Builds analytics like average speed, density, and green-light distribution from raw and predicted data.

📧 notification

  • Path: microservices/notification/notification.py
  • Purpose: Subscribes to traffic alerts via Kafka and sends email notifications for accidents or congestion.

🔁 simulator

  • Path: microservices/simulator/simulator.py
  • Purpose: Hosts WebSocket endpoints for replaying simulation data in the frontend dashboard.

🧠 AI Model

LSTM model training and preprocessing scripts live in ai_model/:

🧪 Preprocessing & Training

  • Preprocessing:
    Converts simulation logs to NumPy arrays

    python preprocess_data.py
  • Training:
    Trains a Sequential LSTM model for green-light prediction

    model = 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 by traffic_signal.


🛡️ Tech Stack

  • 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

About

OptiTraffic AI Microservices is a Docker-based system that uses Kafka streams and a trained LSTM model to predict optimal traffic light phases. The stack includes multiple microservices (login, traffic signal prediction, monitoring, notifications, simulator), a React frontend, and an API gateway.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors