SmartTraffic AI System

An advanced Edge/IoT Big Data solution designed for real-time traffic monitoring, analysis, and decision support. By leveraging state-of-the-art Computer Vision (YOLOv8) and distributed processing patterns, the system transforms raw video streams into actionable insights.

Real-time Processing

Process 30+ simultaneous RTSP streams with < 100ms latency per frame.

Big Data Ready

Handling High Volume, Velocity, and Variety of traffic data on edge infrastructure.

System Metrics

3.5M+
Data Points
37
Active Streams
~1GB
Metadata Storage
99.9%
Uptime

Architecture Workflow

The system follows a pipelined architecture: Acquisition → Processing → Storage → Distribution. The diagram below illustrates the data flow from camera sensors to the end-user dashboard.

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1e293b', 'edgeLabelBackground':'#0f172a', 'tertiaryColor': '#1e293b', 'fontFamily': 'Inter'}}}%% graph TD subgraph Acquisition ["1. Data Acquisition"] A[fa:fa-video CCTV Sources] -->|RTSP/HTTP| B(Camera Agents) end subgraph Processing ["2. Edge Processing"] B -->|Frame Buffer| C{YOLOv8 Engine} C -->|Inference| D[Object Tracking] D -->|Classification| E[Vehicle Counting] end subgraph Storage ["3. Persistence Layer"] E -->|Batch Write| F[(SQLite / Data Lake)] E -->|Update| G[In-Memory Cache] end subgraph Presentation ["4. Visualization"] G --> H[API Gateway] F -->|Analytics Query| H H -->|JSON| I[Dashboard UI] I -->|Map & Charts| J[User] end style A fill:#1d4ed8,stroke:#3b82f6,stroke-width:2px,color:#fff style C fill:#7e22ce,stroke:#a855f7,stroke-width:2px,color:#fff style F fill:#059669,stroke:#10b981,stroke-width:2px,color:#fff style I fill:#be123c,stroke:#f43f5e,stroke-width:2px,color:#fff

Core Components

Struktur modul utama yang membangun sistem dari akuisisi data hingga penyajian.

Camera Agent

Multi-thread capture, YOLO inference, counting, stabilisasi stream.

File: app/services/camera.py

API & Views

Endpoint /api/stats, /api/history, /api/predict_traffic, /api/reset_data, /metrics, /export/csv serta halaman Dashboard & Docs.

File: app/routes.py

Data Management

Load/save stats & config, backfill, generate history, rolling window, Data Lake export.

File: app/utils.py

Database Layer

SQLite schema, batch insert, history query, prediksi berbasis DOW/Hour, agregasi lifetime.

File: app/database.py

Global State

global_stats, daftar kamera, agen aktif, lock untuk thread-safety.

File: app/globals.py

Frontend Dashboard

Peta, routing realistis, kartu statistik, editor marker.

File: app/templates/dashboard.html

Documentation UI

Sidebar, flowchart arsitektur & prediksi, 4Vs Big Data.

File: app/templates/documentation.html

Config & Models

Konfigurasi kamera & ROI, file statistik JSON, model YOLO.

Files: app/config.py, data/cctv_config.json, data/traffic_stats.json, models/yolov8l.pt

Study Case: Traffic Efficiency Analysis

This section maps the system's capabilities to the standard traffic management framework (Analysis, Identify, Design, Implementation, Evaluation).

1. Analysis & Comparison

Comparison of traffic monitoring methodologies.

Feature SmartTraffic AI (Edge) Traditional Loops Manual Survey
Cost Low (Existing CCTV) High (Road Works) Medium (Labor)
Real-time Data Yes (< 1s Latency) Yes No (Post-processing)
Classification Deep Learning (Car/Motor) Limited (Length based) High Accuracy

2. Identify

The system automatically identifies:

  • Vehicle Types (Car vs Motorcycle) using YOLOv8 models.
  • Traffic Density Levels (Smooth, Heavy, Critical).
  • Camera Operational Status (Online/Offline detection).

3. Design

Mobile-first dashboard design with Glassmorphism UI. The system uses a distributed event-driven architecture to handle high-velocity data streams from multiple sensors.

4. Implementation

Deployed on Linux Edge Server (Ubuntu) running:

  • Backend: Python Flask + OpenCV + Multi-threading.
  • AI Engine: Ultralytics YOLOv8.
  • Database: SQLite (Metadata) + JSON (Time-series).

5. Evaluation

Current Performance
37 Streams

Simultaneous processing

Data Volume
3.5M+ Rows

Historical data points

Big Data & Predictive Analytics

SmartTraffic AI leverages the 4Vs of Big Data to transform raw video streams into actionable predictive insights. The system moves beyond simple monitoring to provide "Decision Support" capabilities.

1. Volume (Scale)

Storing over 3.5 Million+ historical data points. The system accumulates granular traffic logs (vehicle counts, types, timestamps) for every active camera.

2. Velocity (Speed)

Processes 37 concurrent video streams in real-time (< 500ms latency). Data is ingested, analyzed, and visualized instantly on the dashboard.

3. Variety (Complexity)

Transforms unstructured data (CCTV Video Feeds) into structured metadata (JSON/SQL). Handles diverse inputs from various IP camera models.

4. Value (Insight)

The ultimate goal. Converts data into Traffic Predictions and routing recommendations to optimize city traffic flow.

Prediction Algorithm

The prediction engine uses a Historical Pattern Replay algorithm. It does not just guess; it calculates the Average Hourly Volume for a specific Day of Week and Hour based on all available historical data.

Example: To predict traffic for next "Monday at 08:00 AM", the system averages the traffic volume of every previous Monday at 08:00 AM recorded in the database.

Difference: Real-time vs Prediction

  • Real-time Status: Based on Visual Density (0-100 score) from the camera feed right now.
  • Prediction: Based on Historical Volume (Total Vehicles/Hour) calculated from past data.
%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1e293b', 'edgeLabelBackground':'#0f172a', 'tertiaryColor': '#1e293b', 'fontFamily': 'Inter', 'fontSize': '16px'}}}%% graph TD A([User Target Time]) -->|Extract Day & Hour| B[Query History] B -->|Filter| C[(Big Data Lake)] subgraph Analytics ["Analytics Engine"] C -->|Sum per Date| D[Hourly Totals] D -->|Calculate Mean| E[Average Volume] E --> F{Threshold Logic} end F -->|< 500 veh/hr| G[LANCAR] F -->|500-1000 veh/hr| H[PADAT] F -->|> 1000 veh/hr| I[MACET] style A fill:#3b82f6,stroke:#2563eb,color:#fff,stroke-width:2px style C fill:#10b981,stroke:#059669,color:#fff,stroke-width:2px style E fill:#8b5cf6,stroke:#7c3aed,color:#fff,stroke-width:2px style G fill:#22c55e,stroke:#16a34a,color:#fff,stroke-width:2px style H fill:#eab308,stroke:#ca8a04,color:#fff,stroke-width:2px style I fill:#ef4444,stroke:#dc2626,color:#fff,stroke-width:2px
// Python Implementation (Logic)
def predict_traffic(camera_id, target_date):
    # 1. Extract Temporal Features
    # SQLite: 0=Sunday, 1=Monday... 6=Saturday
    day_of_week = target_date.strftime('%w') 
    target_hour = target_date.hour

    # 2. Query Big Data Lake (Aggregation)
    # "Calculate the average volume for this specific Day and Hour across all history"
    query = """
        SELECT AVG(hourly_total) 
        FROM (
            SELECT SUM(new_count) as hourly_total
            FROM traffic_history
            WHERE camera_id = ? AND dow = ? AND hour = ?
            GROUP BY date(timestamp)
        )
    """
    
    # 3. Apply Threshold Logic (Volume based)
    avg_volume = execute(query)
    
    if avg_volume > 2000: return "MACET TOTAL" # > 2000 vehicles/hour
    if avg_volume > 1000: return "MACET"       # > 1000 vehicles/hour
    if avg_volume > 500:  return "PADAT LANCAR"# > 500 vehicles/hour
    return "LANCAR"                            # < 500 vehicles/hour
                        

Key Features

Deep Learning Classification

Differentiates between Cars and Motorcycles with high precision using custom-trained YOLO models optimized for Indonesian traffic conditions.

Interactive Map Editor

Admin-only interface for managing camera locations via drag-and-drop. Includes secure authentication and real-time coordinate updates.

Data Backfill Engine

Intelligent historical data synthesis to fill gaps in records, ensuring continuous trend analysis even after system downtimes.

API Reference

The system exposes a RESTful API for data consumption and integration.

GET /api/stats

Retrieves real-time traffic statistics for all active cameras.

Response Example
{
  "status": "success",
  "sources": {
    "cam_1": {
      "name": "Simpang Dago",
      "current_count": 45,
      "accumulated_count": 1250,
      "status": "online"
    }
  }
}
POST /api/edit_camera Admin

Updates camera configuration including geolocation coordinates.

Payload Example
{
  "username": "admin",
  "password": "...",
  "id": "cam_1",
  "lat": -6.914744,
  "lng": 107.609810
}

Data Models

Core data structures used for persistence and configuration.

Traffic History (SQL)

Column Type Description
id INTEGER Primary Key
camera_id TEXT Source Identifier
timestamp REAL Unix Timestamp
total_count INTEGER Aggregate Volume

Camera Config (JSON)

"cam_id": {
  "name": "Location Name",
  "source": "rtsp://...",
  "lat": -6.9175,
  "lng": 107.6191,
  "roi": [0, 0, 1920, 1080]
}