Real-time industrial safety monitoring system using computer vision. Detects safety violations, PPE compliance, falls, and restricted zone intrusions in live video streams.
- 🎥 Real-time Video Processing - Webcam, IP cameras, RTSP streams, video files
- 👤 Person Detection - YOLOv8-based detection with confidence scoring
- 🦺 PPE Detection - Helmet, vest, gloves, masks, goggles
- 📍 Zone Management - Custom restricted areas with intrusion alerts
- 👥 Fall Detection - Automatic fall detection using motion & aspect ratio analysis
- 🔔 Smart Alerts - Configurable alerts with cooldown periods & logging
- 📹 Video Recording - Auto-record violations
- 📊 Live Statistics - FPS counter, frame count, violation tracking
- 🎨 Visual Annotations - Real-time frame annotations with status indicators
- Python 3.8+
- OpenCV - Video processing
- PyTorch - Deep learning
- YOLOv8 - Object detection
- Streamlit - Dashboard
- Loguru - Logging
git clone https://github.com/Ayesha037/safety_monitor.git
cd safety_monitor# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python -m venv venv
source venv/bin/activatepip install -r requirements.txt# For NVIDIA GPU (replace cu118 with your CUDA version)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118# Webcam (default)
python main.py
# IP Camera / RTSP Stream
python main.py --source "rtsp://user:pass@192.168.1.100:554/stream"
# Video File
python main.py --source "video.mp4"
# Headless Mode (no display)
python main.py --no-display
# Enable Recording
python main.py --record| Key | Action |
|---|---|
| Q | Quit |
| S | Screenshot |
Edit config/config.yaml:
system:
name: "Safety Monitor"
version: "1.3.0"
log_level: "INFO"
video:
source: "0" # 0=webcam, URL, or video path
width: 1280
height: 720
fps_target: 30
resize_factor: 1.0 # 0.5 = 50% resolution (faster)
frame_skip: 0 # Skip frames for speed (0=process all)
model:
detector: "models/yolov8n.pt"
device: "cuda" # cuda or cpu
confidence_threshold: 0.5
nms_iou_threshold: 0.45
max_detections: 100
ppe:
enabled: true
person_class_id: 0
ppe_model_path: "models/ppe_classifier.pt"
required_ppe: ["helmet", "vest"]
confidence_threshold: 0.7
fall_detection:
enabled: true
aspect_ratio_threshold: 0.5
velocity_frames: 5
velocity_threshold: 50
min_area_fraction: 0.01
zones:
enabled: true
zones:
- name: "Restricted Area"
type: "polygon"
points: [[100, 100], [500, 100], [500, 400], [100, 400]]
color: [0, 0, 255]
alert_on_intrusion: true
alerts:
enabled: true
cooldown_seconds: 5
log_path: "logs/alerts.log"
recording:
enabled: true
output_path: "logs/videos/violations.mp4"
codec: "mp4v"
display:
show_fps: truesafety_monitor/
├── config/ # Configuration management
├── models/ # Detection & classification models
│ ├── detector.py # YOLOv8 object detector
│ ├── ppe_classifier.py # PPE detection
│ ├── fall_detector.py # Fall detection engine
│ └── safety_pipeline.py # Main processing pipeline
├── utils/ # Utility modules
│ ├── video_capture.py # Video input handling
│ ├── zone_manager.py # Zone management & intrusion
│ ├── alert_manager.py # Alert system
│ ├── drawing.py # Frame annotation utilities
│ └── logger.py # Logging setup
├── dashboard/ # Streamlit dashboard (optional)
├── logs/ # Output: alerts, screenshots, videos
├── main.py # Main entry point
├── requirements.txt # Python dependencies
└── yolov8n.pt # Pre-trained YOLOv8 model
Detects persons in frames using YOLOv8.
detector = ObjectDetector(
model_path="models/yolov8n.pt",
device="cuda",
conf_threshold=0.5
)
detections = detector.detect(frame)Classifies PPE items for each detected person.
ppe = PPEClassifier(
model_path="models/ppe_classifier.pt",
conf_threshold=0.7
)
ppe_status = ppe.classify(frame, detections)Detects falls using motion analysis.
fall_detector = FallDetector(
aspect_ratio_threshold=0.5,
velocity_frames=5
)
is_falling = fall_detector.detect(detection, prev_detection)Orchestrates all detectors and returns comprehensive results.
pipeline = SafetyPipeline(
detector=detector,
ppe=ppe,
fall=fall_detector,
zone_mgr=zone_mgr,
alert_mgr=alert_mgr
)
result = pipeline.process(frame)video:
resize_factor: 0.5 # Process at 50% resolution
frame_skip: 2 # Process every 3rd frame
model:
confidence_threshold: 0.4 # Lower = more detections (slower)video:
resize_factor: 1.0 # Full resolution
frame_skip: 0 # Process all frames
model:
confidence_threshold: 0.7 # Higher = fewer false positivesCUDA Out of Memory
python main.py --resize-factor 0.5
# Or switch to CPU
export TORCH_DEVICE=cpu
python main.pyNo Detections
- Lower confidence threshold in
config.yaml - Ensure camera/video source is working
- Check lighting conditions
Low FPS
- Reduce resolution:
resize_factor: 0.5 - Skip frames:
frame_skip: 2 - Use CPU-only if GPU memory is limited
Camera Not Found
# Test camera
python -c "import cv2; cap = cv2.VideoCapture(0); print(cap.isOpened())"=== Safety Monitor v1.3.0 starting ===
System ready. Press Q to quit, S to screenshot.
[FRAME 0001] Persons: 3 | Violations: 1 | Zone Intrusions: 0 | FPS: 28.5
[ALERT] PPE Violation: Person #2 missing helmet
[ALERT] Zone intrusion in Restricted Area
Screenshot saved: logs/screenshot_1686754323.jpg
logs/safety_monitor.log- Main system loglogs/alerts.log- Alert eventslogs/screenshots/- Captured frameslogs/videos/- Recorded violations
FROM nvidia/cuda:11.8.0-runtime-ubuntu22.04
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]docker build -t safety-monitor .
docker run --gpus all -it safety-monitor# Copy service file
sudo cp safety_monitor.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable safety_monitor
sudo systemctl start safety_monitor
# Check status
sudo systemctl status safety_monitor
journalctl -u safety_monitor -f- Fork the repository
- Create feature branch:
git checkout -b feature/new-feature - Commit changes:
git commit -m 'Add new feature' - Push to branch:
git push origin feature/new-feature - Open Pull Request
- Web-based dashboard
- REST API
- Multi-camera support
- Custom model training
- Mobile app
MIT License - See LICENSE file for details
- 📋 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
- OpenCV - Computer vision library
- Ultralytics - YOLOv8 framework
- PyTorch - Deep learning framework
⭐ If this project helped you, please star the repository!
Made with ❤️ for workplace safety