RADRS Technical Docs
RADRS - Core Documentation
The Real-Time Accident Detection and Response System (RADRS) is an AI-driven, multi-tier pipeline automating traffic collision monitoring, incident logging, and emergency responder dispatch.
System Overview
RADRS replaces manual incident identification. When collisions occur, the latency to dispatch is minimized using three integrated components:
- Python AI Edge Agent: Interfaces with local CCTV IP streams, executing Ultralytics YOLOv8 object classification and proximity analytics inside a bitwise ROI (Region of Interest) mask.
- Express API Gateway Server: Receives event logs, captures base64 screenshots, commits metrics to MongoDB, and automatically searches responder contacts.
- React Operator Dashboard: Polling console displaying live collision tables, screenshots, and communication links to local responder crews.
01 // YOLOv8 Python Edge Agent
The Edge Agent processes real-time frame feeds. First, it isolates the road coordinates using a bitwise mask to prevent false detections from overhead signs or peripheral movements.
detection_pipeline.py
import cv2
from ultralytics import YOLO
import math
model = YOLO("yolov8n.pt")
mask = cv2.imread("mask/basavangudi.png", cv2.IMREAD_GRAYSCALE)
def process_frame(frame, mask_file):
# Bitwise masking overlays roadway coordinates
masked_frame = cv2.bitwise_and(frame, frame, mask=mask_file)
results = model(masked_frame, conf=0.40)
vehicles = []
for box in results[0].boxes:
if box.cls == 2: # Car class in COCO dataset
x1, y1, x2, y2 = box.xyxy[0]
cx, cy = int((x1 + x2)/2), int((y1 + y2)/2)
vehicles.append((cx, cy))
# Calculate Euclidean distance between every pair of vehicles
for i in range(len(vehicles)):
for j in range(i + 1, len(vehicles)):
dist = math.sqrt((vehicles[i][0] - vehicles[j][0])**2 + (vehicles[i][1] - vehicles[j][1])**2)
if dist < 100:
# Proximity breach flagged!
return True, frame
return False, frame02 // Node.js Express Server
The Node.js server serves as the API gateway. It accepts base64 data payloads, saves incident screenshots, logs details to MongoDB database schemas, and triggers SMTP routing.
server.js
const express = require("express");
const mongoose = require("mongoose");
const nodemailer = require("nodemailer");
const Incident = require("./models/Incident");
const app = express();
app.use(express.json({ limit: "50mb" })); // Support large base64 image strings
app.post("/api/incidents", async (req, res) => {
const { location, image, severity } = req.body;
try {
const newIncident = new Incident({
location,
image, // Base64 Data URL string
severity,
timestamp: new Date()
});
await newIncident.save();
// Trigger automated alerting
await dispatchAlert(newIncident);
res.status(201).json({ success: true, incidentId: newIncident._id });
} catch (err) {
res.status(500).json({ error: err.message });
}
});03 // Incident Database Mongoose Schema
Mongoose schemas define the structure of logged collisions and ambulance responder records:
models/Incident.js
const mongoose = require("mongoose");
const IncidentSchema = new mongoose.Schema({
location: { type: String, required: true },
image: { type: String, required: true }, // Base64 collision frame
severity: { type: String, enum: ["LOW", "MEDIUM", "HIGH", "CRITICAL"], default: "HIGH" },
timestamp: { type: Date, default: Date.now },
ambulanceRequested: { type: Boolean, default: false }
});
module.exports = mongoose.model("Incident", IncidentSchema);04 // Nodemailer SMTP Alerts
When an incident is saved, Nodemailer automatically queries ambulance crews registered near the incident location and dispatches immediate notification emails.
dispatch_system.js
const nodemailer = require("nodemailer");
const AmbulanceDriver = require("./models/AmbulanceDriver");
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.SMTP_EMAIL,
pass: process.env.SMTP_PASSWORD
}
});
async function dispatchAlert(incident) {
// Query driver registered under matching location
const drivers = await AmbulanceDriver.find({ location: incident.location });
for (let driver of drivers) {
const mailOptions = {
from: process.env.SMTP_EMAIL,
to: driver.email,
subject: `🚨 EMERGENCY CRITICAL ALERT: Collision at ${incident.location}`,
html: `
<h2>COLLISION FLAG DETECTED BY RADRS AI</h2>
<p><strong>Location:</strong> ${incident.location}</p>
<p><strong>Time:</strong> ${incident.timestamp}</p>
<p><strong>Severity:</strong> ${incident.severity}</p>
<hr />
<p><a href="http://localhost:3000/docs#operator-console">View Incident Dashboard</a></p>
`
};
await transporter.sendMail(mailOptions);
}
}05 // Operator Console React Polling
The React operator dashboard polls the Express GET endpoint every 5 seconds to keep the incident interface up-to-date in real time.
IncidentList.tsx
import { useState, useEffect } from "react";
import axios from "axios";
export function IncidentList() {
const [incidents, setIncidents] = useState([]);
useEffect(() => {
const fetchIncidents = async () => {
try {
const response = await axios.get("http://localhost:5000/api/incidents");
setIncidents(response.data);
} catch (err) {
console.error("Failed to poll incidents", err);
}
};
fetchIncidents();
const interval = setInterval(fetchIncidents, 5000); // 5 second polling interval
return () => clearInterval(interval);
}, []);
return (
<div className="grid gap-6">
{incidents.map((incident: any) => (
<div key={incident._id} className="border border-red-500/20 bg-red-500/5 p-4 rounded-xl">
<h4>Incident: {incident.location}</h4>
<p>Severity: {incident.severity}</p>
<img src={incident.image} alt="Accident snapshot" className="w-full max-w-md rounded" />
</div>
))}
</div>
);
}