Skip to content

Enhances construction site safety using YOLO for object detection, identifying hazards like workers without helmets or safety vests, and proximity to machinery or vehicles. HDBSCAN clusters safety cone coordinates to create monitored zones. Post-processing algorithms improve detection accuracy.

License

Notifications You must be signed in to change notification settings

yihong1120/Construction-Hazard-Detection

🇬🇧 English | 🇹🇼 繁體中文

AI-Driven Construction Safety Banner



"Construction-Hazard-Detection" is an AI-powered tool designed to enhance safety on construction sites. By leveraging the YOLO model and self-developed algorithms for object detection, it identifies potential hazards such as:

  • Workers without helmets
  • Workers without safety vests
  • Workers near machinery or vehicles
  • Workers in restricted areas, restricted areas will be automatically generated by computing and clustering the coordinates of safety cones.
  • Machineries or vehicles near utility poles

Post-processing algorithms further improve detection accuracy. The system is built for real-time deployment, offering instant analysis and alerts for identified hazards.

Additionally, the system integrates AI recognition results in real-time via a web interface. It can send notifications and real-time on-site images through messaging apps like LINE, Messenger, WeChat, and Telegram for prompt alerts and reminders. The system also supports multiple languages, enabling users to receive notifications and interact with the interface in their preferred language. Supported languages include:

  • 🇹🇼 Traditional Chinese (Taiwan)
  • 🇨🇳 Simplified Chinese (Mainland China)
  • 🇫🇷 French
  • 🇬🇧 English
  • 🇹🇭 Thai
  • 🇻🇳 Vietnamese
  • 🇮🇩 Indonesian

This multi-language support makes the system accessible to a global audience, improving usability across different regions.



Hazard Detection Diagram

Contents

Hazard Detection Examples

Below are examples of real-time hazard detection by the system:

Workers without helmets or safety vests

Workers without Helmets or Safety Vests

Workers near machinery or vehicles

Workers near Machinery or Vehicles

Workers in restricted areas

Workers in Restricted Areas

Quick Start (Recommended)

Before running the application, prepare your stream configuration file config/configuration.json (see below for an example), and complete the following quick start and environment variable setup steps.

[
  {
    "video_url": "https://cctv1.kctmc.nat.gov.tw/6e559e58/",
    "site": "Kaohsiung",
    "stream_name": "Test",
    "model_key": "yolo11n",
    "notifications": {
      "line_token_1": "language_1",
      "line_token_2": "language_2"
    },
    "detect_with_server": true,
    "expire_date": "2024-12-31T23:59:59",
    "detection_items": {
      "detect_no_safety_vest_or_helmet": true,
      "detect_near_machinery_or_vehicle": true,
      "detect_in_restricted_area": true
    },
    "work_start_hour": 0,
    "work_end_hour": 24,
    "store_in_redis": true
  }
]

✅ Startup Steps (Complete in Order)

  1. Install Docker (Required) Please install [Docker Desktop / Docker Engine] on your computer first.

  2. Start Redis with Docker

    docker run -d --name my-redis -p 6379:6379 \
    redis:latest redis-server --requirepass "password"
  3. Start MySQL with Docker (Windows / Linux)

    docker run -d --name my-mysql -p 3306:3306 \
    -e MYSQL_ROOT_PASSWORD=ChangeMe! \
    -e MYSQL_DATABASE=construction_hazard_detection \
    -e MYSQL_USER=username \
    -e MYSQL_PASSWORD=password \
    mysql:latest \
    --default-authentication-plugin=mysql_native_password \
    --character-set-server=utf8mb4 \
    --collation-server=utf8mb4_unicode_ci
  4. Import Database Schema (First Time Only)

  • Windows PowerShell / CMD:

    type .\scripts\init.sql | docker exec -i my-mysql \
    mysql -u username -p"password" --default-character-set=utf8mb4 construction_hazard_detection
  • Linux / macOS:

    cat ./scripts/init.sql | docker exec -i my-mysql \
    mysql -u username -p"password" --default-character-set=utf8mb4 construction_hazard_detection
  1. Install Required Python Packages:

    pip install -r requirements.txt
  2. Start Each API (from project root, use separate terminals for each)

  • Data Management (DB Management)

    uvicorn examples.db_management.app:app --host 127.0.0.1 --port 8005 --workers 2
  • Notification Server (FCM / Local Notification)

    uvicorn examples.local_notification_server.app:app --host 127.0.0.1 --port 8003 --workers 2
  • Streaming Web Backend

    uvicorn examples.streaming_web.backend.app:app --host 127.0.0.1 --port 8800 --workers 2
  • Violation Records API

    uvicorn examples.violation_records.app:app --host 127.0.0.1 --port 8002 --workers 2
  • YOLO Detection API

    uvicorn examples.YOLO_server_api.backend.app:app --host 127.0.0.1 --port 8000 --workers 2
  1. Start the Main Program (Two Modes)
  • Database Mode (Default) main.py will poll the stream_configs table and dynamically start/restart/stop stream subprocesses.

    python main.py
    # Optional: adjust polling interval (default 10 seconds)
    python main.py --poll 5
  • JSON Mode (File-based Multi-stream Config) Use --config to specify the JSON file path; the main program will start a subprocess for each stream config in the array.

    python main.py --config config/configuration.json

To stop: Press Ctrl + C to gracefully terminate all subprocesses and release resources. .env will be loaded automatically at startup (python-dotenv).

  1. Frontend Page (Cloud Demo) Open: https://visionnaire-cda17.web.app Login: user / password

After login, you can modify API endpoints in the web settings page.

Database Configuration and Management

The system uses MySQL to store stream, site, violation, and user data. Database name: construction_hazard_detection

Main tables:

  • stream_configs: Stream settings (URL, site, model, notifications, detection items, work hours, etc.)
  • sites: Site information
  • users: System users
  • violations: Violation records

See scripts/init.sql for full SQL schema. Please complete the "Import Database Schema" step above before first run.

Environment Variables

Create a .env file in the project root as follows (adjust as needed):

# Database
DATABASE_URL='mysql+asyncmy://username:[email protected]/construction_hazard_detection'

# API Credentials
API_USERNAME='user'
API_PASSWORD='password'

# API URLs
DETECT_API_URL="http://127.0.0.1:8000"
FCM_API_URL="http://127.0.0.1:8003"
VIOLATION_RECORD_API_URL="http://127.0.0.1:8002"
STREAMING_API_URL="http://127.0.0.1:8800"
DB_MANAGEMENT_API_URL="http://127.0.0.1:8005"

# Redis
REDIS_HOST='127.0.0.1'
REDIS_PORT=6379
REDIS_PASSWORD='password'

# Firebase
FIREBASE_CRED_PATH='examples/local_notification_server/your-firebase-adminsdk.json'
project_id='your-project-id'

# (Optional) LINE / Cloudinary credentials, add if integrated:
# LINE_CHANNEL_ACCESS_TOKEN='YOUR_LINE_CHANNEL_ACCESS_TOKEN'
# CLOUDINARY_CLOUD_NAME='YOUR_CLOUDINARY_CLOUD_NAME'
# CLOUDINARY_API_KEY='YOUR_CLOUD_API_KEY'
# CLOUDINARY_API_SECRET='YOUR_CLOUD_API_SECRET'

Note

  • If services are deployed on different hosts or containers, change 127.0.0.1 to the accessible address.
  • MySQL and Redis credentials must match your Docker environment variables (e.g., MYSQL_USER, MYSQL_PASSWORD, --requirepass).
  • Be sure to complete the scripts/init.sql import before first run.

Additional Information

  • The system logs are available within the Docker container and can be accessed for debugging purposes.
  • The output images with detections (if enabled) will be saved to the specified output path.
  • Notifications will be sent through LINE messaging API during the specified hours if hazards are detected.

Notes

  • Ensure that the Dockerfile is present in the root directory of the project and is properly configured as per your application's requirements.

For more information on Docker usage and commands, refer to the Docker documentation.

Dataset Information

The primary dataset for training this model is the Construction Site Safety Image Dataset from Roboflow. We have enriched this dataset with additional annotations and made it openly accessible on Roboflow. The enhanced dataset can be found here: Construction Hazard Detection on Roboflow. This dataset includes the following labels:

  • 0: 'Hardhat'
  • 1: 'Mask'
  • 2: 'NO-Hardhat'
  • 3: 'NO-Mask'
  • 4: 'NO-Safety Vest'
  • 5: 'Person'
  • 6: 'Safety Cone'
  • 7: 'Safety Vest'
  • 8: 'Machinery'
  • 9: 'Utility Pole'
  • 10: 'Vehicle'

Models for detection

Model size
(pixels)
mAPval
50
mAPval
50-95
params
(M)
FLOPs
(B)
YOLO11n 640 58.0 34.2 2.6 6.5
YOLO11s 640 70.1 44.8 9.4 21.6
YOLO11m 640 73.3 42.6 20.1 68.0
YOLO11l 640 77.3 54.6 25.3 86.9
YOLO11x 640 82.0 61.7 56.9 194.9

Our comprehensive dataset ensures that the model is well-equipped to identify a wide range of potential hazards commonly found in construction environments.

License

This project is licensed under the AGPL-3.0 License.

About

Enhances construction site safety using YOLO for object detection, identifying hazards like workers without helmets or safety vests, and proximity to machinery or vehicles. HDBSCAN clusters safety cone coordinates to create monitored zones. Post-processing algorithms improve detection accuracy.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published