> ## Documentation Index
> Fetch the complete documentation index at: https://docs.videodb.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Dashcam Monitoring of Traffic

> Real-time traffic enforcement system detecting violations like no helmet, red lights, and unsafe driving

<a href="https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/real_time_streaming/Roadcam.ipynb" target="_blank">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" noZoom />
</a>

## The Challenge

Traffic violations cause preventable accidents and deaths every day. From riders without helmets to drivers using mobile phones, unsafe behavior on roads puts lives at risk.

Traditional enforcement requires traffic police to manually monitor roads — costly, limited in coverage, and often reactive rather than preventive.

What if AI could automatically detect violations in real-time and generate evidence for enforcement? This is where AI-powered dashcam monitoring comes in.

## What You'll Build

With VideoDB RTStream, you can build an automated traffic violation detection system that:

* Monitors live road feeds continuously
* Detects multiple violation types automatically
* Generates video evidence for enforcement
* Sends real-time alerts to traffic authorities
* Works 24/7 without manual supervision

## Violations Detected

The system monitors for six critical violations:

* **No Helmet** - Two-wheeler riders without proper head protection
* **Mobile Phone Use** - Drivers operating vehicles while using phones
* **Wrong Side Driving** - Vehicles traveling against traffic flow
* **Red Light Violation** - Vehicles crossing intersections during red signals
* **Triple Riding** - More than two people on a single two-wheeler
* **No Seatbelt** - Drivers or passengers without seatbelt protection

## Setup

### Install Dependencies

```bash theme={null}
pip install videodb
```

### Connect to VideoDB

```python theme={null}
import videodb

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

## Implementation

### Step 1: Connect to Your Roadcam Stream

```python theme={null}
rtsp_url = "rtsp://your-dashcam-or-roadcam-url"
roadcam_stream = coll.connect_rtstream(
    name="Traffic Violation Detector",
    url=rtsp_url,
)
```

### Step 2: Index Scenes with Violation Analysis

Create a scene index tuned for capturing traffic violations with focused frame sampling:

```python theme={null}
violation_index = roadcam_stream.index_visuals(
    batch_config={
        "type": "time",
        "value": 7,
        "frame_count": 5,
    },
    prompt="""Analyze this roadcam footage and identify traffic violations by monitoring:
              - Helmet compliance for two-wheelers
              - Mobile phone usage while driving
              - Vehicle positioning on correct lanes
              - Seatbelt usage
              - Occupancy violations
              - Signal compliance

              Focus on the following violations:
              1. NO HELMET: Two-wheeler rider or pillion not wearing a helmet
              2. MOBILE PHONE USE: Driver using mobile phone while operating the vehicle
              3. WRONG SIDE DRIVING: Vehicle traveling against the designated traffic flow
              4. RED LIGHT VIOLATION: Vehicle crossing when traffic signal is red
              5. TRIPLE RIDING: More than two people on a single two-wheeler
              6. NO SEATBELT: Driver or front passenger not wearing seatbelt""",
    name="Traffic_Violation_Detection_Index",
)
```

The `frame_count: 5` captures multiple angles of each violation for clear evidence.

### Step 3: Create Violation Event

```python theme={null}
violation_event_id = conn.create_event(
    event_prompt="""Detect when a traffic rule violation occurs, such as no helmet, mobile phone use,
                    wrong side driving, red light violation, triple riding, or no seatbelt.
                    Your explanation should clearly mention:
                    - Traffic Rule Violated
                    - Vehicle type and color
                    - License plate number if visible
                    - Violation type
                    - Brief description of what was observed""",
    label="traffic_violation",
)
```

### Step 4: Attach Alert for Automated Reporting

```python theme={null}
webhook_url = "https://your-traffic-authority-webhook.com"
violation_alert_id = violation_index.create_alert(violation_event_id, callback_url=webhook_url)
```

## Alert Example

When a violation is detected, the system sends structured evidence:

```json theme={null}
{
  "event_id": "event-violation-no-helmet",
  "label": "traffic_violation",
  "confidence": 0.95,
  "explanation": "Traffic Rule Violated: NO HELMET
                  Vehicle: Orange scooter
                  License Plate: DL 3S CW 4952
                  Violation: Both rider and pillion not wearing helmets
                  Description: The rider and pillion rider on the orange scooter are clearly not wearing helmets, which is a critical safety violation.",
  "timestamp": "2025-05-29T04:57:44.123456+00:00",
  "start_time": "2025-05-29T10:27:20.000000+05:30",
  "end_time": "2025-05-29T10:27:27.000000+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711a0-0fde-7911-b282-25bc0b4ecf65/1748475396000000-1748475407000000.m3u8"
}
```

The structured explanation makes it easy to automatically process violations for enforcement.

## Integration with Enforcement Systems

The alerts can be integrated with existing traffic enforcement workflows:

**Automated Report Generation:**

* Extract vehicle details and license plate
* Generate violation evidence video
* Create standardized enforcement notice
* Route to appropriate authority

**Data-Driven Enforcement:**

* Track violation hotspots
* Identify repeat offenders
* Optimize traffic enforcement resources
* Measure safety improvements

## The Result

This system enables automated enforcement of traffic rules, reducing need for manual traffic police monitoring and improving road safety through instant violation detection and alerts.

Key benefits:

* **24/7 Monitoring** - No breaks, always watching
* **Instant Evidence** - Video proof for every violation
* **Consistent Enforcement** - AI doesn't have bias or fatigue
* **Data Insights** - Analytics on violation patterns and hotspots
* **Deterrent Effect** - Known monitoring discourages violations

<Card icon="notebook" href="https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/real_time_streaming/Roadcam.ipynb" title="Explore the Full Notebook">
  Open the complete implementation with additional configuration and integration examples.
</Card>

## Related Tutorials

<CardGroup cols={2}>
  <Card icon="car" href="/examples-and-tutorials/live-intelligence/traffic-violations" title="Traffic Violations Detection">
    Real-time detection of helmet violations and traffic rule breaking
  </Card>

  <Card icon="map" href="/examples-and-tutorials/live-intelligence/road-monitoring" title="Road Monitoring System">
    Multi-use road monitoring for accidents and congestion
  </Card>
</CardGroup>
