Skip to content

stealthinator45/PPE-Helmet-Detection

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช– Helmet Detection System using Faster R-CNN

๐Ÿ“Œ Project Overview

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.


๐Ÿš€ Features

  • โœ… 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

๐Ÿง  Technologies Used

๐Ÿ”น Backend

  • Python 3.11
  • Flask (Web server)
  • OpenCV (Image processing)
  • NumPy (Numerical operations)

๐Ÿ”น Deep Learning

  • PyTorch
  • Torchvision (Faster R-CNN ResNet50 FPN)

๐Ÿ”น Model

  • Faster R-CNN (Object Detection)
  • Custom-trained on helmet dataset

โš™๏ธ Working Principle

Step 1: Input

  • Image uploaded OR webcam frame captured

Step 2: Preprocessing

  • Image is resized using letterbox technique
  • Converted to tensor and normalized

Step 3: Model Inference

  • Faster R-CNN predicts:
    • Bounding boxes
    • Class labels
    • Confidence scores

Step 4: Filtering

  • Apply class-specific thresholds:
    • Person โ†’ low threshold
    • Head โ†’ medium threshold
    • Helmet โ†’ high threshold

Step 5: Helmet Detection Logic

For each detected head:

  • Check overlap with helmet
  • Ensure helmet is:
    • On top region of head
    • Proper size relative to head
    • Sufficient intersection area

Step 6: Output

  • ๐ŸŸข Green box โ†’ Helmet worn
  • ๐Ÿ”ด Red box โ†’ No helmet

๐Ÿ› ๏ธ Installation Guide

1๏ธโƒฃ Clone Repository

git clone https://github.com/your-username/helmet-detection.git
cd helmet-detection

2๏ธโƒฃ Create Virtual Environment (Recommended)

python -m venv venv
venv\Scripts\activate   # Windows

3๏ธโƒฃ Install Dependencies

pip install -r requirements.txt

โš ๏ธ Important Note (PyTorch Installation)

  • 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())

4๏ธโƒฃ Verify GPU (Optional)

import torch
print(torch.cuda.is_available())

โ–ถ๏ธ Running the Application

Step 1: Place Model

Put your trained model inside:

models/
 โ”œโ”€โ”€ best_model.pth
 OR
 โ”œโ”€โ”€ final_model.pth
 OR
 โ”œโ”€โ”€ fasterrcnn_scripted.pt

You can donload the model from Here


Step 2: Run Flask App

flask run

Step 3: Open in Browser

http://localhost:5000

๐ŸŽฅ Usage

๐Ÿ“ท Upload Image

  • Click upload button
  • Select image
  • View detection results

๐ŸŽฅ Webcam Detection

  • Open live feed
  • Real-time helmet detection

โš™๏ธ Environment Variables (Optional)

You can customize behavior:

set THR_HEAD=0.3
set THR_HELMET=0.75
set IMG_SIZE_CAM=320
set IMG_SIZE_UPLOAD=640

๐Ÿš€ Performance

  • โšก ~6 minutes per training epoch (RTX 4060)
  • ๐ŸŽฏ High accuracy with custom dataset (~5000 images)
  • ๐Ÿ”ฅ Optimized with mixed precision (AMP)

๐Ÿงฉ Model Loading Priority

The app automatically loads:

  1. best_model.pth
  2. final_model.pth
  3. fasterrcnn_scripted.pt

โ— Troubleshooting

โŒ Model not loading

  • Ensure file exists in /models

โŒ Webcam not working

  • Change camera index:
CAM_INDEX = 1

โŒ CUDA not used

  • Ensure PyTorch GPU version installed

๐Ÿ”ฎ Future Improvements

  • ๐Ÿ“ฑ Mobile app integration
  • โ˜๏ธ Cloud deployment
  • ๐Ÿ“Š Dashboard analytics
  • ๐ŸŽฏ YOLOv8 integration for faster inference

๐Ÿ‘จโ€๐Ÿ’ป Author

Developed as part of an AI/ML project for helmet safety detection.


โญ If you like this project

Give it a โญ on GitHub!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 86.5%
  • HTML 13.5%