Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

30 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

title AI Traffic Moderator
emoji ๐Ÿšฆ
colorFrom red
colorTo green
sdk docker
app_port 7860
pinned false

AI Traffic Control System ๐Ÿšฆ

A modern AI-powered traffic signal control system that uses YOLOv8 for real-time vehicle detection and dynamically manages traffic signals. Built with React, Flask, OpenCV, and Ultralytics YOLO.

Traffic Control System React Flask Python

โœจ Features

  • ๐ŸŽฏ Real-time Vehicle Detection - YOLOv8 powered detection for cars, motorcycles, buses, and trucks
  • ๐Ÿšฆ Dynamic Traffic Signal Control - AI-based signal timing based on vehicle density
  • ๐Ÿ“Š Live Analytics Dashboard - Real-time vehicle count graphs and statistics
  • ๐Ÿ—บ๏ธ Interactive Map View - Traffic camera location visualization with Leaflet
  • ๐Ÿ“น Live Video Streaming - Real-time camera feed with vehicle annotations
  • ๐ŸŽจ Modern UI - Professional React-based interface with smooth animations
  • ๐Ÿ“ฑ Responsive Design - Works seamlessly on desktop and mobile devices

๐Ÿ› ๏ธ Tech Stack

Frontend

  • React 18 - Modern UI framework
  • React Router - Client-side routing
  • Chart.js - Real-time data visualization
  • React Leaflet - Interactive maps
  • Axios - HTTP client
  • Vite - Fast build tool

Backend

  • Flask - Python web framework
  • OpenCV - Video processing
  • YOLOv8 (Ultralytics) - Object detection
  • PyTorch - Deep learning framework

๐Ÿ“‹ Prerequisites

  • Python 3.8+
  • Node.js 16+ and npm
  • Webcam (for live detection)
  • Git

๐Ÿš€ Installation & Setup

1. Clone the Repository

git clone https://github.com/your-username/AI-Traffic-Moderator.git
cd AI-Traffic-Moderator

2. Install Python Dependencies

pip install -r requirements.txt

3. Install Node.js Dependencies

npm install

๐ŸŽฎ Running the Application

You need to run both the backend and frontend servers:

Terminal 1: Start Flask Backend

python app.py

The backend will run on http://localhost:5000

Terminal 2: Start React Frontend

npm run dev

The frontend will run on http://localhost:3000

Access the Application

Open your browser and navigate to:

http://localhost:3000

Login: Use any username and password (demo authentication)

๐Ÿ“ Project Structure

AI-Traffic-Moderator/
โ”œโ”€โ”€ src/                          # React frontend source
โ”‚   โ”œโ”€โ”€ components/               # Reusable components
โ”‚   โ”‚   โ”œโ”€โ”€ Header.jsx           # Navigation header
โ”‚   โ”‚   โ””โ”€โ”€ TrafficLight.jsx     # Traffic signal component
โ”‚   โ”œโ”€โ”€ pages/                   # Page components
โ”‚   โ”‚   โ”œโ”€โ”€ Login.jsx            # Login page
โ”‚   โ”‚   โ”œโ”€โ”€ Dashboard.jsx        # Main dashboard
โ”‚   โ”‚   โ””โ”€โ”€ MapView.jsx          # Map view page
โ”‚   โ”œโ”€โ”€ App.jsx                  # Main app component
โ”‚   โ””โ”€โ”€ main.jsx                 # Entry point
โ”œโ”€โ”€ models/                      # AI models
โ”‚   โ””โ”€โ”€ yolov8n.pt              # YOLOv8 nano model
โ”œโ”€โ”€ Wireless type/               # Future wireless implementation
โ”œโ”€โ”€ app.py                       # Flask backend server
โ”œโ”€โ”€ requirements.txt             # Python dependencies
โ”œโ”€โ”€ package.json                 # Node.js dependencies
โ”œโ”€โ”€ vite.config.js              # Vite configuration
โ””โ”€โ”€ README.md                    # This file

๐ŸŽฏ How It Works

  1. Video Capture - Captures live video feed from webcam
  2. Vehicle Detection - YOLOv8 processes each frame to detect vehicles
  3. Traffic Analysis - Counts vehicles and determines traffic density
  4. Signal Control - Adjusts traffic signal timing dynamically:
    • Red: Low traffic (< 5 vehicles) - 15 seconds
    • Yellow: Medium traffic (5-9 vehicles) - 10 seconds
    • Green: High traffic (โ‰ฅ 10 vehicles) - 15 seconds
  5. Real-time Updates - Frontend polls backend every 5 seconds for updates
  6. Data Visualization - Displays live graphs and statistics

๐Ÿš— Supported Vehicle Classes

  • ๐Ÿš— Car
  • ๐Ÿ๏ธ Motorcycle
  • ๐ŸšŒ Bus
  • ๐Ÿš› Truck

๐Ÿ“Š Dashboard Features

Live Camera Feed

  • Real-time video stream with vehicle detection boxes
  • Vehicle labels and bounding boxes
  • Traffic signal status overlay

Traffic Statistics

  • Current vehicle count
  • System status indicator
  • Real-time updates

Analytics Graph

  • Vehicle count over time (last 20 data points)
  • Interactive Chart.js visualization
  • 5-second update interval

Map View

  • Interactive map showing camera location
  • Powered by OpenStreetMap and Leaflet
  • Marker with location details

๐Ÿ”ง Configuration

Backend Configuration (app.py)

# Camera settings
cap = cv2.VideoCapture(0)  # Change 0 to camera index

# Vehicle classes (COCO dataset)
VEHICLE_CLASSES = {2: 'car', 3: 'motorcycle', 5: 'bus', 7: 'truck'}

# Traffic signal timing
traffic_state = {
    "signal": "red",
    "timer": 15,
    "vehicle_count": 0
}

Frontend Configuration (vite.config.js)

server: {
  port: 3000,
  proxy: {
    '/video_feed': 'http://localhost:5000',
    '/traffic_status': 'http://localhost:5000'
  }
}

๐Ÿ› Troubleshooting

PyTorch 2.6 Model Loading Issue

If you encounter _pickle.UnpicklingError, the code includes a fix:

# Monkey patch torch.load to use weights_only=False
original_load = torch.load
def patched_load(*args, **kwargs):
    kwargs['weights_only'] = False
    return original_load(*args, **kwargs)
torch.load = patched_load

Camera Not Working

  • Check if camera is being used by another application
  • Verify camera permissions in Windows settings
  • Try changing camera index in app.py: cv2.VideoCapture(1) or cv2.VideoCapture(2)

Port Already in Use

  • Frontend: Change port in vite.config.js
  • Backend: Change port in app.py: app.run(port=5001)

๐Ÿš€ Building for Production

npm run build

The production build will be in the dist/ folder.

๐Ÿ”ฎ Future Enhancements

  • Multi-camera intersection support
  • Emergency vehicle prioritization
  • Cloud-based deployment
  • Historical data analytics
  • Mobile app integration
  • Wireless sensor integration (see Wireless type/ folder)

๐Ÿ“ License

This project is for educational and research purposes.

๐Ÿ‘จโ€๐Ÿ’ป Author

Manaswin Sripatnala

๐Ÿค 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

๐Ÿ™ Acknowledgments


โญ Star this repository if you find it helpful!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages