Nikhil3235/fire-smoke-detection
๐ฅ FireVision AI โ Intelligent Fire & Smoke Detection System
Python | Flask | OpenCV | PyTorch | Docker | Twilio | HuggingFace
An enterprise-grade, real-time safety monitoring system powered by a custom-trained YOLOv8m deep learning model. Achieving 99.2% mAP accuracy, this system is designed to detect fire and smoke hazards in real-time, compute human presence, filter false alarms, and automatically trigger emergency response protocols.
๐ Live Demo
Experience the project live:
- ๐ Deployed AI Space (Hugging Face): https://huggingface.co/spaces/Nikhil3235/fire-smoke-detection
- โจ Direct Live Workspace (Full Screen): https://nikhil3235-fire-smoke-detection.hf.space/live
๐ ๏ธ Developer Skills Showcased
- Real-time Edge Vision Pipelines: Ultra-low latency camera capture streams utilizing OpenCV and client-side interpolation (LERP) rendering at 60 FPS.
- Spatio-Temporal Tracking: Real-time object tracking over sequential frames to differentiate static items (printed photos) from live moving humans.
- Geometrical Anti-Spoofing Heuristics: Bounding box intersection filters to ignore false positives like orange shirts, skin tones, and fire videos shown on mobile screens.
- Asynchronous Networking: Strict ping-pong API frames rendering queue limiting concurrent requests to 1, completely preventing Hugging Face server backlog.
- Automated Emergency Infrastructure: Real-time Twilio SMS alerts and automated server-side PDF safety reports.
โก Geometrical Mechanics & Math (Anti-Spoofing)
To achieve zero false positives in real-world environments, FireVision AI calculates geometrical overlaps and frame variance statistics in real-time:
1. Face/Orange Shirt Overlap Filter (Heuristic 1)
Custom fire models often trigger false positives on warm skin colors (faces) or orange clothing. The system checks the Intersection over Area (IoA) with standard YOLOv8 COCO person detections:
$$\text{IoA}{\text{person}} = \frac{\text{Area}(B{\text{fire}} \cap B{\text{person}})}{\text{Area}(B{\text{fire}})}$$
If $\text{IoA}_{\text{person}} > 0.30$, the fire detection box is discarded as a shirt/face false alarm.
2. Screen Video Spoofing Filter (Heuristic 2)
To ignore fire videos played on mobile screens/monitors, the system calculates the overlap with COCO screen-like detections (tv, laptop, cell phone):
$$\text{IoA}{\text{screen}} = \frac{\text{Area}(B{\text{fire}} \cap B{\text{screen}})}{\text{Area}(B{\text{fire}})}$$
If $\text{IoA}_{\text{screen}} > 0.50$, it is labeled as "Fake Fire (Screen)" and does not trigger emergency sirens.
3. Printed Photo Frame Suppression (Heuristic 3)
To prevent static photo frames hung on walls from being counted as trapped humans, the system tracks coordinates $\{Bt = [x{1,t}, y{1,t}, x{2,t}, y{2,t}]\}{t=1}^N$ over $N$ consecutive frames:
$$\sigmax = \text{std}(\{x{1,t}\}), \quad \sigmay = \text{std}(\{y{1,t}\}), \quad \sigmaw = \text{std}(\{wt\}), \quad \sigmah = \text{std}(\{ht\})$$
$$\text{Variance Score} = \max(\sigmax, \sigmay, \sigmaw, \sigmah)$$
If $\text{Variance Score} < 1.2\text{ pixels}$ over $N \ge 8$ frames, the object is classified as a static "Photo Frame" (gray box) rather than a "Living Person" (green box).
๐ Key Features
- Resilient Camera constraints fallback: Dynamically fallbacks constraints configuration when facingMode
'environment'is blocked on desktop browsers. - 100% Precision Person Model: Runs
yolov8n.ptat full resolution (imgsz=640) with lower base confidence (0.25) for instant (0.1s) multi-person detection. - Dual-Model Startup Cold-Start Warmup: Initializes PyTorch engine with dummy tensor matrices on server startup to eliminate the initial 30-second YOLO latency.
- Resilient OpenCV Video Grab Throttling: Speeds up custom video processing by utilizing
camera.grab()for small skips, maintaining 1.0x playback speed without video lag. - Audio Siren Alarm: Built-in siren warning using Web Audio API with zero external audio assets, fully isolated from race conditions during stream stops.
- Persistent Safety Incident Logs: Automatically appends date, time, fire%, smoke%, and people count to a server-side
history.jsondatabase. Records are preserved across page updates.
โ๏ธ System Architecture
graph TD
A[Client Web Browser] -->|WebSocket / HTTP Request| B(Flask Web Server)
B --> C{Active Source?}
C -->|Webcam Frame| D[Webcam API Endpoint]
C -->|Uploaded Video / Image| E[OpenCV VideoCapture / imread]
D --> F[process_frame pipeline]
E --> F[process_frame pipeline]
F --> G[1. Brightness sensor filter < 15]
G --> H[2. YOLOv8n COCO Person Model imgsz=640]
H --> I[3. YOLOv8m Custom Fire/Smoke Model imgsz=320]
I --> J[4. Apply Anti-Spoofing & Static Filters]
J --> K[Update Server JSON History]
J --> L[Trigger Twilio SMS Alerts]
J --> M[Yield JPEG Stream / Stats JSON]
M --> A๐ Project Structure
fire-smoke-detection/
โโโ app.py # Flask server backend & AI pipeline
โโโ requirements.txt # Python library dependencies
โโโ Dockerfile # Hugging Face Spaces deployment config
โโโ README.md # System documentation (This file)
โโโ index.html # Main landing / marketing page
โโโ live.html # Live AI Monitoring Workspace dashboard
โโโ script.js # LERP rendering loop, logs, & webcam WebRTC
โโโ style.css # Custom glassmorphic styling
โโโ data.yaml # Custom dataset path configurations
โโโ train.py # Custom model training script
โโโ detect.py # Standalone prediction tester script
โโโ models/
โ โโโ best.pt # Custom trained fire/smoke YOLOv8m weights
โโโ static/
โ โโโ sample-fire.mp4 # Local demo forest fire video
โ โโโ sample-smoke.mp4 # Local demo smoke hazard video
โ โโโ uploads/ # Directory for user uploaded videos
โ โโโ alerts/ # Automatically saved hazard snapshots
โโโ history.json # Server-side persistent log databaseโ๏ธ Installation & Local Setup
1. Clone Repository
git clone https://github.com/Nikhil3235/fire-smoke-detection-.git
cd fire-smoke-detection-2. Setup Virtual Environment
Windows:
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
python app.pymacOS / Linux:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py3. Open Web App
Open: http://localhost:7860 or http://localhost:7860/live in your browser.
๐ License
This project is for educational and portfolio demonstration purposes.
Made with โค๏ธ by Nikhil Mali (Computer Vision & AI Engineer)
