
# Predicting Crowd Crushes Before They Happen: How We Built SwarmSight on AWS
Crowd disasters are among the most heartbreaking urban tragedies of our time. From the Hillsborough stadium disaster (1989) and the Love Parade stampede (2010) to the Itaewon Halloween crowd crush (2022), these catastrophes share a devastating truth: **they are almost entirely preventable if detected early.**
Predicting Crowd Crushes Before They Happen: How We Built SwarmSight on AWS
By Keshav Gupta | Built with AWS CDK, Amazon EC2, Amazon DynamoDB, Amazon S3, Amazon SNS, and OpenCV
ο»Ώ
Crowd disasters are among the most heartbreaking urban tragedies of our time. From the Hillsborough stadium disaster (1989) and the Love Parade stampede (2010) to the Itaewon Halloween crowd crush (2022), these catastrophes share a devastating truth: they are almost entirely preventable if detected early.
Yet, traditional crowd monitoring fails at the exact moment it is needed most.
To address this critical public safety challenge, we architected, built, and deployed SwarmSightβan aerial drone crowd safety intelligence platform running live on AWS that detects crowd compression precursors 5β15 minutes before fatal crushing forces emerge.
Here is how we built it, why the physics of crowd dynamics drove our architecture, and how AWS enabled us to go from concept to a 24/7 live platform.
ο»Ώ
1. The Core Insight: Why "Counting Heads" Fails
When most developers approach crowd safety with computer vision, their initial instinct is straightforward:
- Run a YOLO/Faster R-CNN person detector.
- Count the bounding boxes.
- If the count exceeds a threshold, sound the alarm.
In real-world crowd safety, this approach fails for two fundamental reasons:
A. Occlusion Collapses Bounding Box Detectors
In dense crowds exceeding 4 to 8 people per square meter, human bodies overlap almost completely. Heads and shoulders occlude each other, causing bounding box detectors to miss over 60% of the crowd precisely when density becomes dangerous.
B. High Density Does NOT Equal Danger (The "Packed Concert" Dilemma)
Consider a music festival audience or a stadium crowd during a match. People are packed tightly, shoulder-to-shoulder, yet they are completely safe. They are swaying to the music, cheering, taking drinks, and shifting weight.
If your algorithm triggers an alert based solely on high density, security teams will suffer from severe alarm fatigue and ignore notifications when an actual emergency unfolds.
ο»Ώ
2. The Physics of Crush Precursors: Micro-Motion Variance
Real crowd crushes are physical fluid phenomena. Grounded in John Fruinβs Level-of-Service framework and Dr. G. Keith Stillβs crowd science research, safe crowds and crushing crowds exhibit completely distinct motion signatures:
- Safe Packed Crowd (Healthy Jitter): Even when packed, individuals have freedom of movement. Micro-movements (swaying, arm shifts, head turns) generate high optical flow variance: $$\sigma^2 > 0.40$$
- Crush Precursor (Variance Collapse): As a corridor bottlenecks or incoming inflows overpower exits, personal physical space collapses. People are compressed together so tightly that individual movement becomes impossible. Micro-motion variance drops to near zero: $$\sigma^2 < 0.25$$
SAFE PACKED CROWD CRUSH PRECURSOR
βββββββββββββββββββββββββββ βββββββββββββββββββββββββββ
β π§ β π§ β π§ β π§ β β π§-π§-π§-π§-π§-π§ β
β β β β β β Bottleneck / β | | | | | | | | | | β
β π§ β π§ β π§ β π§ β Over-crowding β π§-π§-π§-π§-π§-π§ β
β β ββββββββββββββΊ β β
β High Micro-Motion Jitterβ β Zero Personal Space β
β Flow Variance: ΟΒ² > 0.4 β β Flow Variance: ΟΒ² < 0.2 β
β Status: GREEN (Normal) β β Status: RED (Precursor) β
βββββββββββββββββββββββββββ βββββββββββββββββββββββββββBy fusing spatial density with Farneback Dense Optical Flow across an 8Γ6 spatial grid (48 zones), SwarmSight detects this variance collapse 5 to 15 minutes before physical crush forces injure anyone, giving incident commanders precious time to divert inflows, open emergency gates, and manage egress.
ο»Ώ
3. Architecture: The 5-Resource AWS Blueprint
We instituted a strict constraint: keep the operational footprint lean, robust, reproducible, and strictly least-privilege.
Using AWS CDK (TypeScript), we provisioned the complete infrastructure cleanly across five core AWS services in the Β
ap-south-1Β (Mumbai) region: DRONE VIDEO INPUT
(Safe vs. Bottleneck Feeds)
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β AWS EC2 (t3.large) β
β Ubuntu 22.04 LTS (GP3) β
β β
β βββββββββββββββββββββββββββββββββ β
β β systemd Daemon β β
β β FastAPI + Uvicorn (Port 8000)β β
β β β β
β β β’ Farneback Optical Flow β β
β β β’ 48-Zone Physics Engine β β
β β β’ Stateful Alert Manager β β
β β β’ Sub-150ms WebSocket Server β β
β βββββββββββββββββ¬ββββββββββββββββ β
ββββββββββββββββββββΌβββββββββββββββββββ
β
ββββββββββββββββββββββββββΌβββββββββββββββββββββββββ
βΌ βΌ βΌ
ββββββββββββββββββββββββββ βββββββββββββββββββββ βββββββββββββββββββββββββββ
β AWS S3 Bucket β β AWS DynamoDB β β AWS SNS Topic β
β swarmsight-demo-kesha β β swarmsight_alerts β β swarmsight-alerts β
β β β β β β
β β’ Private Storage β β β’ On-Demand W/R β β β’ Emergency Email / SMS β
β β’ SSE-S3 Encryption β β β’ 7-Day Auto TTL β β β’ 1-Hour Anti-Spam β
β β’ Video Assets β β β’ Audit Trail β β Cooldown β
ββββββββββββββββββββββββββ βββββββββββββββββββββ βββββββββββββββββββββββββββ
β² β² β²
β β β
ββββββββββββββββ IAM Instance Profile βββββββββββββ
(Zero Hardcoded Credentials)Why These Specific AWS Services?
- Amazon EC2 (Β
t3.largeΒ, Ubuntu 22.04 LTS): Optical flow algorithms require maintaining temporal state between consecutive video frames ($frame_{t}$ and $frame_{t-1}$). We packaged the entire application stack into production Docker containers fronted by an Nginx reverse proxy on port 8000, enabling seamless zero-downtime port-swap deployments (Β8001Ββ Β8002Β) without ever shifting the public URL or Elastic IP.
- AWS IAM Instance Profile (Β
swarmsight-ec2-roleΒ): In strict accordance with cloud security best practices, zero credentials or API keys exist on disk. The EC2 instance assumes an IAM role via IMDSv2 (session-token-enforced), eliminating SSRF credential exfiltration vectors and strictly scoped to least privilege across the project's named S3 bucket, DynamoDB table, and SNS topic.
- Amazon DynamoDB (Β
swarmsight_alertsΒ): Stores real-time alert logs, zone breakdowns, and telemetry with an automated 7-day Time-To-Live (TTL) attribute (ΒttlΒ), preventing storage bloat while maintaining an audit trail for incident analysis.
- Amazon SNS (Β
swarmsight-alertsΒ): Sends immediate push notifications and emails to event commanders when ORANGE or RED thresholds are breached.
- Amazon S3 (Β
swarmsight-demo-keshaΒ): Private, encrypted object storage for video clips, models, and captured frame snapshots.
ο»Ώ
4. Key Implementation Highlights
1. Optical Flow & Micro-Motion Variance (Β analytics.pyΒ )
Here is the core logic calculating micro-motion variance and directional coherence across the spatial grid:
# Compute Farneback dense optical flow between consecutive grayscale frames
flow = cv2.calcOpticalFlowFarneback(
prev_gray, gray, None,
pyr_scale=0.5, levels=3, winsize=15,
iterations=3, poly_n=5, poly_sigma=1.2, flags=0
)
# Extract horizontal and vertical motion vectors
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
# For each sector in the 8x6 grid:
# 1. Update rolling window of mean magnitude
window.append(cell_mag.mean())
# 2. Compute micro-motion variance
variance = float(np.var(window))
# 3. Detect precursor: High density + variance collapse
if density > 0.65 and variance < 0.25:
risk_level = "RED" # Precursor to crowd crush!2. Predictive Forecasting Engine (Β forecasting.pyΒ )
To shift from reactive detection to anticipatory intervention, we built an in-memory rolling time-series engine that tracks per-zone trajectories and computes linear rate-of-change ($\frac{\Delta \text{Density}}{\Delta t}$):
# Linear regression over rolling 30s density observations
slope, _ = np.polyfit(timestamps, densities, deg=1)
rate_per_min = slope * 60.0 * 100.0
# Project exact seconds until critical compression threshold (0.60)
if slope > 0.003 and density < 0.60:
seconds_to_critical = int((0.60 - density) / slope)
forecast_label = f"β± ~{seconds_to_critical}s to critical"3. Live Camera Ingestion with Instant Fallback (Β main.pyΒ )
Rather than only replaying pre-recorded clips, SwarmSight lets anyone point their webcam or phone camera at a room:
- Uses browser Β
navigator.mediaDevices.getUserMediaΒto capture frames at 8 FPS.
- Streams JPEG frames to FastAPI over the active WebSocket and through Β
/api/live_frameΒ.
- If the camera stream disconnects or latency spikes, the system instantly falls back to the high-density aerial video loop with zero dashboard disruption.
4. Deep Density CNN Integration (CSRNet)
We implemented the CSRNet dilated convolutional neural network architecture (VGG-16 frontend + dilated conv backend) directly in Β
app/analytics.pyΒ . When model weights are loaded, it computes continuous density maps across all 48 zones; otherwise, it seamlessly falls back to our calibrated high-frequency edge density proxy.5. Eliminating Alert Storms (Β alerting.pyΒ )
In an emergency, an algorithm triggering an email every 100 milliseconds will quickly lock up commander mailboxes and hit AWS SNS quota limits. We implemented Stateful Incident Aggregation with a strict cooldown:
class AlertManager:
def __init__(self, table_name, topic_arn, region="ap-south-1"):
self.sns_cooldown_seconds = 3600.0 # 1 hour anti-spam cooldown
self.last_sns_time = {}
def evaluate_and_dispatch(self, telemetry):
alerting_zones = [z for z in telemetry["zones"] if z["level"] in ("ORANGE", "RED")]
if not alerting_zones:
return []
# 1. Persist audit record to DynamoDB with 7-day TTL
self._write_to_dynamodb(incident_record)
# 2. Dispatch SNS only if cooldown has elapsed
now = time.time()
if now - self.last_sns_time.get("incident", 0) > self.sns_cooldown_seconds:
self._publish_sns(incident_record)
self.last_sns_time["incident"] = nowο»Ώ
5. Live Demonstration & Verification
The platform is running 24/7 on AWS EC2 inside Docker, fronted by Nginx:
π Live Console URL: Β https://ec2-13-235-100-182.ap-south-1.compute.amazonaws.com:8000Β (auto-redirects from HTTP)
π₯ Video Walkthrough & Demo: Β https://youtu.be/0BqONuvBe3IΒ
What You Experience in the Redesigned Mission Control Console:
- Interactive Hero Banner: Prompt inviting evaluators to explore pre-recorded aerial scenarios or tap Β
π₯ Try With Your Live CameraΒto test with their phone or webcam.
- Preset Scenario Switcher:
- Drone Alpha (Safe Crowd): Shows high-density festival crowd with natural swaying jitter ($Var > 0.50$, remains GREEN).
- Drone Bravo (Compression Precursor): Real drone footage of a bottleneck corridor where variance collapses ($Var < 0.25$), triggering immediate ORANGE / RED crush warnings.
- Anticipatory Trajectory & Countdown: Displays live countdowns (e.g. Β
β± ~24s to criticalΒ) and dynamic trajectory badges.
- Interactive Spatial Zone Inspector: Hover or tap any 1 of 48 grid sectors to inspect live density %, flow velocity, micro-motion variance, and rate of change ($dD/dt$).
- Explainability Audit Feed: Real-time log explaining the exact physics that triggered each alert, verified against DynamoDB persistence and SNS dispatches.
ο»Ώ
6. What We Learned & What's Next
Building and deploying SwarmSight taught us three critical lessons:
- Physical Grounding Beats Generic ML: Generic person-counters fail where lives are on the line. Modeling the underlying physics of personal space and motion variance solved the false alarm problem.
- Zero-Downtime Operations via Port Swapping: Wrapping the service in Docker behind Nginx allowed us to ship predictive forecasting, live camera ingestion, and UI overhauls with zero downtime on the same public IP.
- Responsible AI Must Be Built-in: SwarmSight requires no facial recognition, discards video frames after memory analysis, and provides transparent explainability metrics so human commanders remain in full control.
ο»Ώ
π Try It Out & Contribute
- Video Demo: Β YouTube (https://youtu.be/0BqONuvBe3I)Β
- GitHub Repository: Β github.com/keshav-gupta01/swarmsightΒ
Have questions about our optical flow algorithm, CDK stack, or crowd safety engineering? Leave a comment below!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article