Skip to content

yousefalwahami/HazardNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

46 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš— HazardNet

Intelligent V2X Hazard Detection & Voice-Controlled Driving Assistant

Real-time traffic simulation with AI-powered voice assistance and 3D visualization

Python React TypeScript SUMO WebSocket

Features β€’ Quick Start β€’ Architecture β€’ Documentation


🌟 Features

🎀 Voice Assistant

  • Real-time Speech Recognition with AssemblyAI WebSocket API
  • AI-Powered Responses using Google Gemini 2.0
  • Text-to-Speech with ElevenLabs
  • Conversation Memory - Contextual 10-message history
  • Anti-Spam Protection - Prevents request flooding

🚦 Traffic Simulation

  • SUMO Integration - Realistic traffic simulation
  • Multi-Vehicle Support - Handle 100+ vehicles
  • Reinforcement Learning - PPO-trained autonomous agents
  • V2X Communication - Vehicle-to-Everything hazard detection
  • Dynamic Hazard Broadcasting - Real-time hazard sharing

πŸ—ΊοΈ 3D Visualization

  • Real-Time Rendering with React Three Fiber
  • Smooth Camera Tracking - Cinematic vehicle following
  • Route Visualization - Dynamic path display
  • Interactive Map - SUMO network in 3D
  • Hazard Markers - Visual hazard indicators

πŸ”Œ Real-Time Communication

  • WebSocket Server - Low-latency vehicle data streaming
  • Live Updates - 20Hz simulation data broadcast
  • Automatic Reconnection - Robust connection handling
  • JSON Protocol - Structured vehicle & hazard data

πŸš€ Quick Start

Prerequisites

  • Python 3.10+
  • Node.js 18+
  • SUMO 1.24.0 (Download)
  • Git

1️⃣ Clone the Repository

git clone https://github.com/yousefalwahami/hazardnet.git
cd hazardnet

2️⃣ Backend Setup

cd backend

# Create virtual environment
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# Mac/Linux:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Create .env file
copy .env.example .env  # Windows
# cp .env.example .env  # Mac/Linux

# Add your API keys to .env:
# GEMINI_API_KEY=your_gemini_key_here
# ELEVENLABS_API_KEY=your_elevenlabs_key_here

Get API Keys:

# Run the backend server
python app.py

Server runs on http://localhost:5000

3️⃣ Frontend Setup

cd frontend

# Install dependencies
npm install

# Create .env file (optional for AssemblyAI)
echo VITE_ASSEMBLYAI_API_KEY=your_assemblyai_key_here > .env

# Run development server
npm run dev

Frontend runs on http://localhost:5173

4️⃣ Simulation Setup

cd simulation

# Install dependencies (if using separate venv)
pip install -r requirements.txt

# Ensure SUMO_HOME is set
# Windows: set SUMO_HOME=C:\Program Files (x86)\Eclipse\Sumo
# Mac/Linux: export SUMO_HOME=/usr/share/sumo

# Run simulation with WebSocket server
python eval_demo_simple.py

WebSocket server runs on ws://localhost:8765


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         HazardNet System                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  SUMO Simulation β”‚         β”‚   WebSocket      β”‚         β”‚   React Frontend β”‚
β”‚                  β”‚ ──────> β”‚   Port 8765      β”‚ ──────> β”‚                  β”‚
β”‚  eval_demo_      β”‚ Real-   β”‚                  β”‚ Live    β”‚  MapViewer 3D    β”‚
β”‚  simple.py       β”‚ time    β”‚  Vehicle Data    β”‚ Updates β”‚  + Voice UI      β”‚
β”‚                  β”‚ Data    β”‚  Hazard Events   β”‚         β”‚                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        ↓                                                           ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  V2X Hazard      β”‚                                     β”‚  Voice Assistant β”‚
β”‚  Detection       β”‚                                     β”‚                  β”‚
β”‚  - V2E Events    β”‚                                     β”‚  AssemblyAI STT  β”‚
β”‚  - V2V Comms     β”‚                                     β”‚  Gemini AI       β”‚
β”‚  - Broadcasting  β”‚                                     β”‚  ElevenLabs TTS  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                                  ↓
                                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                                         β”‚  Flask Backend   β”‚
                                                         β”‚  Port 5000       β”‚
                                                         β”‚  AI Processing   β”‚
                                                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  User speaks β†’ AssemblyAI β†’ Gemini AI β†’ ElevenLabs β†’ Audio     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  SUMO β†’ TraCI β†’ Python β†’ WebSocket β†’ React β†’ Three.js β†’ GPU    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‚ Project Structure

hazardnet/
β”œβ”€β”€ backend/                    # Python Flask API server
β”‚   β”œβ”€β”€ app.py                 # Main Flask application
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ gemini_service.py  # Gemini AI integration
β”‚   β”‚   └── elevenlabs_service.py  # Text-to-speech
β”‚   └── requirements.txt
β”‚
β”œβ”€β”€ frontend/                   # React + TypeScript frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ MapViewer.tsx  # 3D SUMO visualization
β”‚   β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ driver.tsx # Driver interface
β”‚   β”‚   β”‚   β”‚   └── system.tsx # System view
β”‚   β”‚   β”‚   └── voice/         # Voice UI components
β”‚   β”‚   └── hooks/
β”‚   β”‚       └── useVoiceAssistant.ts  # Voice logic
β”‚   └── package.json
β”‚
β”œβ”€β”€ simulation/                 # SUMO traffic simulation
β”‚   β”œβ”€β”€ eval_demo_simple.py    # Main simulation with WebSocket
β”‚   β”œβ”€β”€ train_ppo_multi.py     # RL training script
β”‚   β”œβ”€β”€ envs/
β”‚   β”‚   └── city_grid_multi_env.py  # Custom SUMO environment
β”‚   └── sim/
β”‚       β”œβ”€β”€ city.net.xml       # SUMO network
β”‚       └── city.sumocfg       # SUMO configuration
β”‚
└── docs/                       # Documentation
    β”œβ”€β”€ VOICE_ASSISTANT_SETUP.md
    β”œβ”€β”€ INTEGRATION_GUIDE.md
    └── ASSEMBLYAI_SETUP.md

🎯 Usage

Driver Interface

  1. Navigate to http://localhost:5173
  2. Toggle to "Driver" view (top-right)
  3. Click the microphone button
  4. Speak your command:
    • "Where is the nearest gas station?"
    • "What's the traffic like ahead?"
    • "Navigate to the hospital"
    • "Show me hazards on my route"

Voice Console Debugging

Open browser DevTools (F12) to see real-time logs:

βœ… AssemblyAI WebSocket connected
🎧 Session started
🎀 Partial: Hello how
βœ… Final: Hello how are you
πŸ“ Transcript received: Hello how are you
πŸ€– Response received: I'm doing well, thanks for asking!

Simulation Controls

In SUMO GUI:

  • Space - Play/Pause
  • D - Delay (slower simulation)
  • A - Accelerate
  • S - Single step

Python console shows:

πŸš— Vehicle count: 45
πŸ“‘ Broadcasting to 1 clients
⚠️  Hazard detected at (123.45, 678.90)

πŸ“š Documentation

Complete Guides

API Endpoints

Backend (Flask)

Method Endpoint Description
GET /health Health check
POST /api/voice/process Process voice command
POST /api/voice/text-to-speech Generate speech audio

WebSocket (Simulation)

Event Description Data Format
connect Client connected -
message Vehicle update { vehicles: [...], hazards: [...], communications: [...] }
disconnect Client disconnected -

Vehicle Data Format

{
  "vehicles": [
    {
      "id": "vehicle_0",
      "x": 123.45,
      "y": 678.90,
      "speed": 10.5,
      "angle": 90.0,
      "type": "passenger",
      "edge": "edge_123",
      "route": [{"x": 130, "y": 680}, ...],
      "isEgo": false
    }
  ],
  "hazards": [
    {
      "type": "obstacle",
      "position": {"x": 100, "y": 200},
      "severity": "high",
      "timestamp": 1234567890
    }
  ],
  "communications": [
    {
      "sender": "vehicle_0",
      "receiver": "vehicle_1",
      "type": "V2V",
      "message": "hazard_ahead"
    }
  ]
}

πŸ› οΈ Advanced Configuration

Simulation Parameters

Edit simulation/eval_demo_simple.py:

# Simulation speed
SIMULATION_STEP_LENGTH = 0.05  # 50ms per step

# Hazard detection range
HAZARD_DETECTION_RADIUS = 50.0  # meters

# V2X communication range
V2X_RANGE = 100.0  # meters

Camera Settings

Edit frontend/src/components/MapViewer.tsx:

// Camera follow distance
const cameraDistance = 6; // units behind vehicle

// Camera height
const cameraHeight = 4; // units above ground

// Smoothing factor (0-1)
const lerpFactor = 0.05; // lower = smoother

Voice Assistant Tuning

Edit frontend/src/hooks/useVoiceAssistant.ts:

// Conversation history length
const MAX_HISTORY = 10; // messages

// Audio sample rate
const SAMPLE_RATE = 16000; // Hz

πŸ› Troubleshooting

WebSocket not connecting
  • Ensure eval_demo_simple.py is running
  • Check port 8765 is not blocked by firewall
  • Verify frontend WebSocket URL: ws://localhost:8765
No vehicles showing in 3D view
  • Check browser console for errors
  • Verify SUMO simulation has spawned vehicles
  • Ensure WebSocket is sending data (check Python console)
Voice assistant not working
  • Grant microphone permissions in browser
  • Check API keys in .env files
  • Verify backend is running on port 5000
  • Open DevTools console to see error messages
SUMO won't start
  • Set SUMO_HOME environment variable:
    • Windows: C:\Program Files (x86)\Eclipse\Sumo
    • Mac: /usr/local/opt/sumo/share/sumo
    • Linux: /usr/share/sumo
  • Add %SUMO_HOME%\bin to PATH
  • Verify installation: sumo --version
Camera jittering in 3D view
  • Already fixed! Uses ref-based Vector3 lerping
  • Adjust lerpFactor in MapViewer.tsx if needed
  • Ensure stable 60 FPS (check GPU usage)

πŸš€ Performance Optimization

For Large-Scale Simulations (200+ vehicles)

  1. Reduce WebSocket broadcast rate:
# In eval_demo_simple.py
time.sleep(0.05)  # 20 Hz instead of 60 Hz
  1. Enable frustum culling:
// In MapViewer.tsx
<mesh frustumCulled={true}>
  1. Use Level of Detail (LOD):
<Lod distances={[0, 20, 50]}>
  <DetailedCar />
  <SimpleCar />
  <DotCar />
</Lod>

πŸ§ͺ Testing

Run Backend Tests

cd backend
pytest tests/

Run Frontend Tests

cd frontend
npm test

Run Simulation Tests

cd simulation
python -m pytest tests/

🀝 Contributing

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

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

πŸ“œ License

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


πŸ™ Acknowledgments

  • SUMO - Traffic simulation platform
  • React Three Fiber - 3D rendering in React
  • AssemblyAI - Real-time speech recognition
  • Google Gemini - AI conversation engine
  • ElevenLabs - Natural text-to-speech

⭐ Star this repo if you find it helpful!

Made with ❀️ and lots of β˜•

About

Smart real-time hazard detection using V2V and V2X to alert autonomous vehicles, enhancing safety on the road with an AI-powered FSD system voice assistant.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors