> ## 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.

# Flash Flood Early Warning System

> AI-powered real-time detection of flash floods for emergency early warning

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/real_time_streaming/Flash_Flood_Detection.ipynb)

## The Story

The stunning Arizona deserts, known for their dry riverbeds and scenic beauty, hide a deadly risk. During the summer monsoon, sudden torrential rains can trigger flash floods in these seemingly harmless dry zones — with little or no warning.

Conventional alert systems relying on rain gauges or weather satellites often fail to deliver timely, location-specific warnings. By the time a danger alert is sent, it might already be too late.

But we have a smarter way. With VideoDB RTStream, we can install real-time cameras near flood-prone areas and let AI continuously monitor the visuals. As soon as the AI detects signs of a flash flood — like a sudden surge of water through dry land — it can instantly send alerts, giving local authorities and tourists precious moments to act.

## What You'll Build

With VideoDB RTStream, you can build a two-layer monitoring system that:

* Continuously monitors dry riverbeds and surrounding areas
* Detects flash floods immediately upon occurrence
* Identifies heavy rainfall as early warning signals
* Alerts for people in distress requiring rescue
* Sends real-time notifications to emergency services

## 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 the Flood Monitoring Stream

```python theme={null}
rtsp_url = "rtsp://samples.rts.videodb.io:8554/floods"
flood_stream = coll.connect_rtstream(
    name="Flood Detection Stream",
    url=rtsp_url,
    store=True,
)
```

### Step 2: Create Primary Index - Flash Flood Detection

Create the primary scene index focused on immediate flood detection with more frequent analysis:

```python theme={null}
flood_understanding = flood_stream.understand(
    segmentation={"type": "time", "window": "5s"},
    analyzers=[{
        "type": "vlm",
        "name": "flood_scene",
        "sampling": {"frame_count": 3},
        "config": {"prompt": "Monitor the dry riverbed and surrounding area. If moving water is detected across the land, identify it as a flash flood and describe the scene."},
    }],
    store=True,
)
flood_scene_output = flood_understanding.outputs.get("flood_scene")

flood_scene_index = flood_stream.index(
    source=flood_scene_output,
    name="Flash_Flood_Detection_Index",
    use_for=["semantic"],
)
```

The `frame_count: 3` captures 3 frames every 5 seconds for detailed water movement analysis.

### Step 3: Define the Flash Flood Event

Create the first-stage event:

```python theme={null}
# Flash Flood Event
flood_event_id = conn.create_event(
    event_prompt="Detect a flash flood - sudden water surge across the riverbed.",
    label="flash_flood",
)
```

### Step 4: Attach the Flash Flood Alert

```python theme={null}
import asyncio

webhook_url = "https://your-webhook-url.com"
ws_wrapper = conn.connect_websocket()
ws = await ws_wrapper.connect()

flood_alert_id = flood_scene_index.create_alert(
    flood_event_id,
    callback_url=webhook_url,
    ws_connection_id=ws.connection_id,
)
```

### Finish the First Monitoring Stage

Disable its alert, stop its index and source understanding, and close its WebSocket before starting the independent riverbed-monitoring lesson.

```python theme={null}
flood_scene_index.disable_alert(flood_alert_id)
flood_scene_index.stop()
flood_understanding.stop()
await ws_wrapper.close()
```

### Step 5: Create the Riverbed Monitoring Index

This independent stage analyzes five-second windows with three frames for rainfall and rescue signals.

```python theme={null}
riverbed_monitoring_understanding = flood_stream.understand(
    segmentation={"type": "time", "window": "5s"},
    analyzers=[{
        "type": "vlm",
        "name": "riverbed_scene",
        "sampling": {"frame_count": 3},
        "config": {"prompt": "Monitor the dry riverbed and surrounding area. If any person is detected, output 'Person detected, rescue needed'. Else if rainfall is detected, output 'Rainfall detected'. Otherwise, output 'No significant events detected.'"},
    }],
    store=True,
)
riverbed_scene_output = riverbed_monitoring_understanding.outputs.get("riverbed_scene")

early_warning_index = flood_stream.index(
    source=riverbed_scene_output,
    name="Riverbed_Monitoring_Index",
    use_for=["semantic"],
)
```

### Step 6: Attach Riverbed Alerts

```python theme={null}
rainfall_event_id = conn.create_event(
    event_prompt="Detect heavy rainfall - potential precursor to flooding.",
    label="heavy_rainfall",
)
rescue_event_id = conn.create_event(
    event_prompt="Detect a person in distress or stuck - rescue needed.",
    label="person_rescue_needed",
)

ws_wrapper = conn.connect_websocket()
ws = await ws_wrapper.connect()
rainfall_alert_id = early_warning_index.create_alert(
    rainfall_event_id,
    callback_url=webhook_url,
    ws_connection_id=ws.connection_id,
)
rescue_alert_id = early_warning_index.create_alert(
    rescue_event_id,
    callback_url=webhook_url,
    ws_connection_id=ws.connection_id,
)
```

## Alert Payloads

### Flash Flood Alert (Critical Priority)

When a flood is detected, the system sends an immediate alert with the exact location and video:

```json theme={null}
{
  "event_id": "event-flood-detection",
  "label": "flash_flood",
  "confidence": 0.95,
  "explanation": "Flash flood detected! Water is flowing rapidly and forcefully through the riverbed. The water appears muddy, carrying sediment and debris. Multiple people visible on rocky banks.",
  "timestamp": "2025-05-29T07:17:51.123456+00:00",
  "start_time": "2025-05-29T07:17:51.000000+05:30",
  "end_time": "2025-05-29T07:17:56.000000+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711a0-0fde-7911-b282-25bc0b4ecf65/1748475471000000-1748475476000000.m3u8"
}
```

### Heavy Rainfall Alert (Warning Priority)

Early warning alerts help authorities respond proactively:

```json theme={null}
{
  "event_id": "event-rainfall-warning",
  "label": "heavy_rainfall",
  "confidence": 0.85,
  "explanation": "Heavy rainfall detected in the monitoring area. Conditions favorable for flash flooding.",
  "timestamp": "2025-05-29T07:10:00.000000+00:00",
  "start_time": "2025-05-29T07:10:00.000000+05:30",
  "end_time": "2025-05-29T07:10:15.000000+05:30",
  "stream_url": "https://rt.stream.videodb.io/manifests/rts-019711a0-0fde-7911-b282-25bc0b4ecf65/1748475000000000-1748475015000000.m3u8"
}
```

## The Result

With this system in place, communities, tourists, and local authorities in Arizona's desert regions can receive immediate alerts when a dangerous flash flood occurs — gaining critical seconds to take cover, clear routes, or initiate rescues.

The two-layer system ensures:

* **Real-time flood detection** for immediate response
* **Early rainfall warnings** for proactive preparation
* **Rescue alerts** for people in distress

> [card] [Explore the Full Notebook](https://colab.research.google.com/github/video-db/videodb-cookbook/blob/main/real_time_streaming/Flash_Flood_Detection.ipynb)
>
> Open the complete implementation with advanced monitoring features and configuration options.

## Related Tutorials

> [card] [Baby Crib Monitoring with AI](https://docs.videodb.io/examples-and-tutorials/live-intelligence/baby-crib-monitoring.md)
>
> Real-time safety monitoring for infants with instant alerts

> [card] [Property Intrusion Detection](https://docs.videodb.io/examples-and-tutorials/live-intelligence/intrusion-detection.md)
>
> Intelligent security system with tiered threat detection


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.