CoolFace
Apppublic

Nikhil3235/fire-smoke-detection

sourceHugging Faceupdated 3mo agoView on Hugging Face
1likes
App README

๐Ÿ”ฅ 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:


๐Ÿ› ๏ธ 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.pt at 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.json database. Records are preserved across page updates.

โš™๏ธ System Architecture

mermaid
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

bash
git clone https://github.com/Nikhil3235/fire-smoke-detection-.git
cd fire-smoke-detection-

2. Setup Virtual Environment

Windows:

powershell
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
python app.py

macOS / Linux:

bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py

3. 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)

Nikhil3235/fire-smoke-detection ยท CoolFace