Skip to main content
Open In Colab

The Story

Road accidents happen every single day — and many lives are lost not because of the severity of the crash itself, but because victims don’t receive timely aid and medical attention. Often, there’s no one around to report an incident, or bystanders take too long to respond — either from shock, panic, or the overwhelming nature of witnessing an accident. But in those critical moments, even a few seconds can make the difference between life and death. This is where AI can help. With VideoDB RTStream, we can deploy cameras at accident-prone locations and let AI constantly monitor live video streams. As soon as an accident occurs, AI will detect it and instantly send alerts to nearby emergency services or traffic authorities.

What You’ll Build

In this guide, we’ll build a comprehensive road monitoring system that addresses three critical traffic challenges:
  • Accident Detection - Instant alerts for vehicle collisions
  • Violation Monitoring - Real-time detection of traffic rule breaking
  • Congestion Detection - Early warning for traffic jams

Setup

Install Dependencies

pip install videodb

Connect to VideoDB

import videodb

api_key = "your_api_key"
conn = videodb.connect(api_key=api_key)
coll = conn.get_collection()

Implementation

Part 1: Accident Detection at Toll Plaza

Step 1: Connect to Toll Plaza Stream
accident_stream = coll.connect_rtstream(
    name="Toll Plaza Accident Monitor",
    url="rtsp://samples.rts.videodb.io:8554/accident",
)
Step 2: Index for Accident Detection
accident_index = accident_stream.index_visuals(
    batch_config={
        "type": "time",
        "value": 10,
        "frame_count": 2,
    },
    prompt="""Monitor the toll plaza road carefully. Detect if a vehicle collides, crashes,
              or a person falls. Describe the situation clearly if an accident occurs.""",
    name="Accident_Detection_Index",
)
Step 3: Create Accident Event & Alert
accident_event_id = conn.create_event(
    event_prompt="Detect if an accident or vehicle collision takes place.",
    label="road_accident",
)

webhook_url = "https://your-webhook-url.com"
accident_alert_id = accident_index.create_alert(accident_event_id, callback_url=webhook_url)

Part 2: Traffic Violation Detection at Toll Booth

Step 4: Create Violation Index on Same Stream Create a separate index on the same stream with faster analysis:
violation_index = accident_stream.index_visuals(
    batch_config={
        "type": "time",
        "value": 3,
        "frame_count": 2,
    },
    prompt="""Monitor the toll plaza carefully. Detect if any vehicle breaks traffic rules —
              for example, skipping the toll booth, crossing without stopping, driving in the
              wrong lane, or ignoring the barrier. Describe such violations clearly.""",
    name="Toll_Violation_Index",
)
The faster batch config (value: 3) ensures violations aren’t missed. Step 5: Create Violation Event & Alert
violation_event_id = conn.create_event(
    event_prompt="Detect if a vehicle breaks traffic rules at the toll plaza.",
    label="toll_rule_violation",
)

violation_alert_id = violation_index.create_alert(violation_event_id, callback_url=webhook_url)

Part 3: Traffic Congestion Detection

Step 6: Connect to Highway Stream
congestion_stream = coll.connect_rtstream(
    name="Highway Traffic Monitor",
    url="rtsp://3.6.198.206:8554/traffic",
)
Step 7: Index for Congestion
congestion_index = congestion_stream.index_visuals(
    batch_config={
        "type": "time",
        "value": 5,
        "frame_count": 3,
    },
    prompt="""Monitor the lanes of vehicles over several video frames. If the whole lane of
              cars consistently moves very slowly or stops, classify the situation as 'traffic
              congestion detected.' Otherwise, classify it as 'regular traffic flow'.""",
    name="Traffic_Congestion_Index",
)
Step 8: Create Congestion Event & Alert
congestion_event_id = conn.create_event(
    event_prompt="Detect if traffic congestion or jam is forming.",
    label="traffic_congestion",
)

congestion_alert_id = congestion_index.create_alert(congestion_event_id, callback_url=webhook_url)

Alert Examples

Accident Alert (Critical)

{
  "event_id": "event-accident-001",
  "label": "road_accident",
  "confidence": 0.96,
  "explanation": "Vehicle collision detected at toll plaza. Two vehicles involved with visible impact.",
  "timestamp": "2025-05-29T10:15:33.123456+00:00",
  "start_time": "2025-05-29T10:15:33.000000+05:30",
  "end_time": "2025-05-29T10:15:43.000000+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711a0-0fde-7911-b282-25bc0b4ecf65/1748485533000000-1748485543000000.m3u8"
}

Violation Alert (Warning)

{
  "event_id": "event-violation-001",
  "label": "toll_rule_violation",
  "confidence": 0.90,
  "explanation": "Vehicle crossing toll plaza without stopping, ignoring barrier.",
  "timestamp": "2025-05-29T10:20:15.123456+00:00",
  "start_time": "2025-05-29T10:20:15.000000+05:30",
  "end_time": "2025-05-29T10:20:18.000000+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711a0-0fde-7911-b282-25bc0b4ecf65/1748485815000000-1748485818000000.m3u8"
}

Congestion Alert (Advisory)

{
  "event_id": "event-congestion-001",
  "label": "traffic_congestion",
  "confidence": 0.95,
  "explanation": "High vehicle density and slow movement observed across multiple lanes, indicating traffic congestion.",
  "timestamp": "2025-05-29T13:41:20.123456+00:00",
  "start_time": "2025-05-29T13:41:08.000000+05:30",
  "end_time": "2025-05-29T13:41:13.000000+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711a0-0fde-7911-b282-25bc0b4ecf65/1748504468000000-1748504473000000.m3u8"
}

The Result

With these systems in place, we built a smart AI-powered road monitoring system addressing two major challenges: Accident Detection at toll plazas — instantly spotting crashes and alerting emergency services without delay. Traffic Congestion Detection on busy highways — catching early signs of jams so authorities can act before things spiral. Together, these tools show how AI video monitoring can make roads safer, traffic smoother, and emergency responses faster — all in real time.

Explore the Full Notebook

Open the complete implementation with multi-stream monitoring and advanced configuration.