This system detects Personal Protective Equipment (PPE) compliance using computer vision (YOLOv8) and provides a web interface for monitoring violations, viewing statistics, and managing alerts. It includes real-time notifications via Telegram.
This project was refactored to meet industry standards, improve reliability, and provide a professional user interface suitable for demonstration purposes.
- Media Upload: Upload images (JPG, PNG) or videos (MP4, AVI, MOV, WebM) via a web interface.
- PPE Detection: Utilizes a fine-tuned YOLOv8 model to detect various PPE items and identify violations (e.g., missing hard hats, vests, masks based on configured rules).
- Violation Logging: Records detected violations in an SQLite database, including timestamp, violation type, location, severity, and a snapshot image.
- Web Dashboard: Displays key statistics about violations:
- Counts by equipment type
- Counts by severity (High, Medium, Low)
- Counts by location
- Counts by status (Unresolved, Investigating, Resolved)
- Daily violation trend chart (last 30 days)
- Violation Log: A filterable/sortable table view of all recorded violations with links to evidence images and status update capability.
- Telegram Notifications: Sends real-time alerts to a configured Telegram chat when violations are detected (includes violation details and image). Features a cooldown mechanism to prevent notification spam.
- Configurable: Easily configure model paths, database location, Telegram credentials, PPE class mappings, violation rules, and area requirements via a
.envfile.
-
Clone the repository:
git clone <repository-url> cd ppe-detection-project
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
Note:
ultralyticsandopencv-python-headlesscan be large. -
Configure Environment Variables:
- Copy
.env.exampleto.env:cp .env.example .env - Edit
.envand fill in the required values:SECRET_KEY: Generate a strong secret key (python -c 'import os; print(os.urandom(24).hex())').TELEGRAM_BOT_TOKEN: Your Telegram Bot token.TELEGRAM_CHAT_ID: The chat ID where notifications should be sent.MODEL_PATH: Verify the path to your trained.ptfile.PPE_CLASS_MAPPING: Crucially, update this JSON string to match the class IDs and names from thedata.yamlfile used to train your specific YOLOv8 model.VIOLATION_CLASSES: Update this JSON list with the exact class names (from the mapping above) that represent a violation (e.g., "NO-Hardhat").AREA_REQUIREMENTS: Define required PPE for different work areas if needed.- Review other paths if you changed the structure.
- Copy
-
Place the Trained Model:
- Ensure your working trained YOLOv8 model file (e.g.,
last.ptorbest.pt) is placed in themodels/directory and renamed to match theMODEL_PATHin your.envfile (e.g.,models/yolov8s_ppe_custom.pt).
- Ensure your working trained YOLOv8 model file (e.g.,
-
Initialize the Database:
- The database (
violations.db) and schema will be created automatically when you first run the application usingrun.py.
- The database (
-
Activate the virtual environment:
source venv/bin/activate -
Run the Flask development server:
python run.py
-
Access the application: Open your web browser and navigate to
http://127.0.0.1:5000(orhttp://<your-server-ip>:5000).
- Navigate to the Upload page.
- Enter the Location and select the Area Type.
- Choose an image or video file and click Upload and Analyze.
- Wait for processing. You will be redirected to the Violations Log.
- Check the Dashboard for statistics and charts.
- Monitor your configured Telegram chat for real-time violation notifications.
- On the Violations Log page, you can view violation details, see the evidence image, and update the status (Unresolved, Investigating, Resolved).