Real-time helmet detection system using YOLOv8 that monitors live camera feeds, logs violations, captures snapshots, and dispatches automated email alerts to site managers.
- YOLOv8 processes every webcam frame to detect
person,worker,helmet, andhardhatclasses - For each detected person, it checks if a helmet bounding box overlaps the head region (top 40% of the person box)
- If no helmet is found on a person → violation is triggered:
- Snapshot is saved to
images/violations/ - OCR runs on the person crop to read a chest/suit ID
- Email alert with the snapshot is sent to the manager (throttled to once every 20 seconds)
- Snapshot is saved to
| Component | Tool |
|---|---|
| Object detection | YOLOv8 (Ultralytics) — model/best.pt |
| Vision processing | OpenCV |
| Web server | Flask |
| Chest number OCR | EasyOCR / custom (ocr_utils.py) |
| Email alerts | Python smtplib + Gmail SMTP |
| Frontend | Vanilla HTML / CSS / JS |
Helmet-Detection/
├── app.py # Flask server + detection pipeline
├── email_utils.py # Gmail SMTP alert sender
├── ocr_utils.py # Chest number OCR extractor
├── cleanup.py # Auto-delete old violation images
├── model/
│ └── best.pt # Fine-tuned YOLOv8 helmet model
├── images/violations/ # Auto-saved violation snapshots
├── logs/violations.csv # Violation event log
├── templates/index.html # Professional dashboard UI
├── static/style.css # Custom styling
├── Dockerfile # Containerization config
├── render.yaml # Deployment specification
├── .env.example # Environment variables template
└── requirements.txt # Project dependencies
pip install -r requirements.txtCopy .env.example to .env and set your credentials:
cp .env.example .envpython app.pyhttp://localhost:5000
| Feature | Details |
|---|---|
| Live detection feed | YOLOv8 bounding boxes with confidence % |
| Stats bar | Uptime · Helmets detected · Violations · Persons tracked |
| Event log | Timestamped, filterable (All / Violations / Safe) |
| Violation gallery | Last 6 snapshots, click to enlarge |
| Alert banner | Flashes on new violation |
Configure credentials in email_utils.py:
EMAIL_ADDRESS = 'your@gmail.com'
EMAIL_PASSWORD = 'your_app_password' # Gmail App Password (not your real password)
MANAGER_EMAIL = 'manager@company.com'Generate a Gmail App Password: Google Account → Security → 2-Step Verification → App Passwords
- Base:
yolov8n.pt(YOLOv8 Nano) - Fine-tuned on: helmet / hardhat / person / worker dataset (
data.yaml) - Inference threshold: 0.5 confidence
- Helmet-on-head check: helmet box must be within the top 40% of the person box
| Constant | Default | Description |
|---|---|---|
MODEL_PATH |
model/best.pt |
Path to YOLO weights |
MANAGER_EMAIL |
gmail address | Alert recipient |
VIOLATION_FOLDER |
images/violations |
Snapshot save path |
| Email throttle | 20 seconds | Min gap between email alerts |
| Confidence threshold | 0.5 | Min detection confidence |
- Python 3.10+
- Webcam or RTSP camera
- Gmail account with App Password enabled (for email alerts)
Harsh Ramesh Nerkar
B.Tech CSE 2026 | AI/ML Engineer
Email: harshrameshnerkar@gmail.com