This project is an AI-powered Helmet Detection System built using PyTorch (Faster R-CNN) and deployed via a Flask web application. It detects whether a person is wearing a helmet in:
- ๐ท Uploaded images
- ๐ฅ Live webcam stream
The system identifies:
- Person
- Head
- Helmet
Then applies geometric logic to determine whether a helmet is properly worn.
- โ Real-time helmet detection using webcam
- โ Image upload inference
- โ High-accuracy detection using Faster R-CNN
- โ Custom logic for helmet-head matching
- โ GPU acceleration (CUDA + AMP support)
- โ Adjustable thresholds via environment variables
- Python 3.11
- Flask (Web server)
- OpenCV (Image processing)
- NumPy (Numerical operations)
- PyTorch
- Torchvision (Faster R-CNN ResNet50 FPN)
- Faster R-CNN (Object Detection)
- Custom-trained on helmet dataset
- Image uploaded OR webcam frame captured
- Image is resized using letterbox technique
- Converted to tensor and normalized
- Faster R-CNN predicts:
- Bounding boxes
- Class labels
- Confidence scores
- Apply class-specific thresholds:
- Person โ low threshold
- Head โ medium threshold
- Helmet โ high threshold
For each detected head:
- Check overlap with helmet
- Ensure helmet is:
- On top region of head
- Proper size relative to head
- Sufficient intersection area
- ๐ข Green box โ Helmet worn
- ๐ด Red box โ No helmet
git clone https://github.com/your-username/helmet-detection.git
cd helmet-detectionpython -m venv venv
venv\Scripts\activate # Windowspip install -r requirements.txt- If you have a dedicated GPU (NVIDIA dGPU), install the CUDA-enabled version of PyTorch for much faster training and inference:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121- If you do not have a GPU, install the regular CPU version:
pip install torch torchvision๐ You can verify GPU support using:
import torch
print(torch.cuda.is_available())import torch
print(torch.cuda.is_available())Put your trained model inside:
models/
โโโ best_model.pth
OR
โโโ final_model.pth
OR
โโโ fasterrcnn_scripted.pt
You can donload the model from Here
flask runhttp://localhost:5000
- Click upload button
- Select image
- View detection results
- Open live feed
- Real-time helmet detection
You can customize behavior:
set THR_HEAD=0.3
set THR_HELMET=0.75
set IMG_SIZE_CAM=320
set IMG_SIZE_UPLOAD=640- โก ~6 minutes per training epoch (RTX 4060)
- ๐ฏ High accuracy with custom dataset (~5000 images)
- ๐ฅ Optimized with mixed precision (AMP)
The app automatically loads:
best_model.pthfinal_model.pthfasterrcnn_scripted.pt
- Ensure file exists in
/models
- Change camera index:
CAM_INDEX = 1- Ensure PyTorch GPU version installed
- ๐ฑ Mobile app integration
- โ๏ธ Cloud deployment
- ๐ Dashboard analytics
- ๐ฏ YOLOv8 integration for faster inference
Developed as part of an AI/ML project for helmet safety detection.
Give it a โญ on GitHub!