Construction-Hazard-Detection[Edit section][Copy link]
The Construction-Hazard-Detection repository provides a system for automatically detecting and monitoring safety hazards on construction sites using AI-powered object detection and real-time video analysis. It helps construction managers and safety officers identify potential risks and ensure compliance with safety regulations.
The core functionality of the system is built around the YOLOv8 object detection model, which is used to identify various objects and potential hazards in construction site video streams. The main components of the system include:
• Object detection and hazard identification: The DangerDetector
class analyzes detected objects to identify potential hazards such as workers without safety gear, workers in controlled areas, or workers in close proximity to machinery. For more details on the hazard detection logic, see the Hazard Detection Logic section.
• Real-time video processing: The StreamCapture
and LiveStreamDetector
classes handle the capture and processing of video streams from construction sites. The system can process multiple streams simultaneously and adapt to varying internet speeds. More information can be found in the Real-Time Video Stream Processing section.
• Notification system: When hazards are detected, the system can send alerts through various messaging platforms including LINE, Facebook Messenger, Telegram, and WeChat. This functionality is implemented in separate notifier classes for each platform. The Notification System section provides more details on this feature.
• Web-based monitoring interface: A Flask-based web application allows users to view live video streams and detection results in real-time. The implementation details are covered in the Web-Based Monitoring Interface section.
The system also includes tools for data augmentation, model training, and evaluation:
• The …/YOLOv8_data_augmentation
directory contains scripts for applying various image augmentation techniques to enhance the training dataset.
• The …/YOLOv8_train
directory provides scripts for training, validating, and exporting YOLOv8 models.
• The …/YOLOv8_evaluation
directory contains tools for evaluating model performance using metrics such as Average Precision and mean Average Precision (mAP).
Key design choices in the implementation include:
-
Modular architecture: The system is divided into separate components for video capture, object detection, hazard analysis, and notification, allowing for easy maintenance and extensibility.
-
Flexibility in deployment: The system can be run in a Docker environment or a Python environment, with instructions provided for both setups.
-
Configurable video streams: Users can specify details of video streams, including URL, image name, label, and machine learning model, through a YAML configuration file.
-
Automated model management: The system includes functionality for downloading pre-trained models and periodically updating them to ensure the latest versions are used.
For developers looking to extend or customize the system, the …/YOLOv8_server_api
directory provides a Flask-based API for object detection, which can be integrated into other applications or services.
Object Detection and Hazard Identification[Edit section][Copy link]
References: src
, examples/YOLOv8_evaluation
, examples/YOLOv8_train
The YOLOModelHandler
class in …/train.py
serves as the central component for managing YOLO (You Only Look Once) models. It provides methods for:
YOLOv8 Model Training and Evaluation[Edit section][Copy link]
References: examples/YOLOv8_train
, examples/YOLOv8_evaluation
The YOLOModelHandler
class in …/train.py
manages the training and evaluation of YOLOv8 models for construction hazard detection. Key functionalities include:
Real-time Object Detection and Tracking[Edit section][Copy link]
References: src
The LiveStreamDetector
class in …/live_stream_detection.py
handles real-time object detection and tracking using YOLOv8 with SAHI (Sliced and Hierarchical Inference). Key features include:
Real-Time Video Stream Processing[Edit section][Copy link]
References: src/stream_capture.py
, src/live_stream_detection.py
, src/live_stream_tracker.py
The StreamCapture
class in …/stream_capture.py
handles real-time video stream processing. Key features include:
Visualization of Safety Measures[Edit section][Copy link]
References: src/drawing_manager.py
The DrawingManager
class in …/drawing_manager.py
is responsible for the visual representation of safety measures on video frames. It provides a set of methods to enhance the visibility of potential hazards and safety equipment, which is crucial for quick identification and response by safety personnel monitoring construction sites.
Localization of Warning Messages[Edit section][Copy link]
References: src/danger_detector.py
, src/lang_config.py
The DangerDetector
class is equipped with a get_text()
method that retrieves localized warning messages based on the current language setting. This method is crucial for providing clear communication to users in their preferred language, enhancing the usability of the hazard detection system across different regions. The localization process involves the following steps:
Real-Time Monitoring and Notification[Edit section][Copy link]
References: src
, examples/streaming_web
The LiveStreamDetector
class in …/live_stream_tracker.py
handles real-time video stream processing and object detection. Key functionality includes:
Stream Capture and Processing[Edit section][Copy link]
References: src/stream_capture.py
The StreamCapture
class in …/stream_capture.py
handles real-time video stream capture and processing. Key features include:
WebSocket Communication and Event Handling[Edit section][Copy link]
References: examples/streaming_web/static/js/label.js
In the Construction-Hazard-Detection project, the …/label.js
file plays a crucial role in managing real-time communication between the server and the client's web interface. The implementation leverages the WebSocket protocol to facilitate a continuous stream of data, which is essential for the live monitoring of construction sites.
Redis Integration for Data Management[Edit section][Copy link]
References: examples/streaming_web/app.py
Redis is utilized for managing and distributing captured stream data in the construction hazard detection system. The Redis connection is configured using environment variables for the host, port, and password, allowing for flexible deployment across different environments.
Read moreData Augmentation and Model Training[Edit section][Copy link]
References: examples/YOLOv8_data_augmentation
, examples/YOLOv8_train
The DataAugmentation
class in …/data_augmentation.py
handles image augmentation for enhancing YOLOv8 model training datasets. Key features include:
Data Augmentation Techniques[Edit section][Copy link]
References: examples/YOLOv8_data_augmentation/data_augmentation.py
The DataAugmentation
class in …/data_augmentation.py
implements various image augmentation techniques to enhance the training dataset. Key features include:
Bounding Box Visualization[Edit section][Copy link]
References: examples/YOLOv8_data_augmentation/visualise_bounding_boxes.py
The BoundingBoxVisualiser
class in …/visualise_bounding_boxes.py
provides functionality to visualize bounding boxes on augmented images. This tool is crucial for verifying the correctness of data augmentation processes. Key features include:
YOLOv8 Model Training[Edit section][Copy link]
References: examples/YOLOv8_train/train.py
The YOLOModelHandler
class in …/train.py
manages YOLOv8 model operations:
API and Server Implementation[Edit section][Copy link]
References: examples/YOLOv8_server_api
, examples/user_management
The server-side API is implemented using Flask, with the main application logic in …/app.py
. This file initializes the Flask application, configures JWT authentication, and registers blueprints for different functionalities.
Object Detection API[Edit section][Copy link]
References: examples/YOLOv8_server_api/detection.py
The detection_blueprint
Flask blueprint handles object detection requests. Key components include:
Label Processing[Edit section][Copy link]
References: examples/YOLOv8_server_api/detection.py
The process_labels()
function handles the processing of detection labels to remove overlapping and completely contained labels for specific categories, particularly "Hardhat" and "Safety Vest". This processing is crucial for improving the accuracy of object detection results. The main steps in label processing are:
Helper Functions[Edit section][Copy link]
References: examples/YOLOv8_server_api/detection.py
Several helper functions in …/detection.py
facilitate image processing, object detection, and result compilation:
User Management[Edit section][Copy link]
References: examples/user_management
User management functionality is implemented in the …/user_management
directory. The core components include:
Authentication[Edit section][Copy link]
References: examples/YOLOv8_server_api/auth.py
The authentication process is implemented in the …/auth.py
file. The create_token()
function, mapped to the /token
endpoint, handles the core authentication logic:
Database Configuration[Edit section][Copy link]
References: examples/YOLOv8_server_api/app.py
, examples/user_management/app.py
The database configuration is handled flexibly using environment variables and SQLAlchemy. In …/app.py
, the database URI is set through the Config
class, allowing for easy configuration changes. The db.init_app(app)
call initializes the SQLAlchemy database with the Flask application.
Integration with Messaging Platforms[Edit section][Copy link]
References: src
, examples/line_chatbot
The system integrates with various messaging platforms through dedicated notifier classes:
Read moreNotifier Classes[Edit section][Copy link]
References: src/notifiers
The …/notifiers
directory contains classes for sending notifications through various messaging platforms. The __init__.py
file imports and exposes these classes, making them easily accessible throughout the project.
Notification Sending[Edit section][Copy link]
References: src/notifiers
The TelegramNotifier
, LineNotifier
, MessengerNotifier
, WeChatNotifier
, and BroadcastNotifier
classes handle sending notifications through their respective platforms. Each notifier class provides a send_notification()
method that accepts a message and an optional image.
LINE Chatbot Integration[Edit section][Copy link]
References: examples/line_chatbot
The LINE chatbot integration is implemented in …/line_bot.py
. The chatbot handles various types of messages and events: