工业和建筑工地 安全帽-安全带识别数据集 yolo数据集 共2200张 已增强 标注
安全帽-安全带识别数据集 yolo数据集 共2200张 已增强
安全帽检测与安全带识别数据集
图像数量:2,200张
增强后标注数量:
- belt(安全带):3,197个
- head(头部):326个
- helmet(安全帽):3,171个
- person(人员):3,912个
- vest(反光背心):920个
用途:该数据集专为工业和建筑工地的安全监测任务设计,适用于安全帽佩戴、安全带使用以及反光背心穿戴的检测。通过使用YOLO(You Only Look Once)等目标检测模型,可以实现实时的安全装备检测和分类。
数据集特点
- 多类别:涵盖五种主要的安全装备类型:安全带、头部、安全帽、人员和反光背心,分别用不同的标签进行标注。
- 高质量标注:所有标注均为人工标注,并经过数据增强处理,确保了标注的准确性和多样性。
- 实际应用场景:数据来源于真实的工业和建筑工地场景,具有很高的实用价值。
- 数据增强:通过数据增强技术(如旋转、缩放、亮度调整等),增加了数据的多样性和鲁棒性。
- 大规模:包含2,200张高分辨率图像,提供了丰富的训练数据。
应用领域
- 工业安全:实时检测工人是否佩戴安全帽、系好安全带及穿戴反光背心,提高工作现场的安全性。
- 自动化监控:结合自动化系统,实现对施工现场的持续监控和异常报警。
- 合规管理:辅助管理人员快速定位和处理未按规定佩戴安全装备的情况,确保符合安全标准。
- 数据分析:通过数据分析,提供关于安全装备佩戴情况的统计报告,支持决策制定。
获取方式
通常情况下,研究人员可以通过官方提供的链接或相关机构网站下载该数据集。请注意,使用时应遵循相应的许可协议和引用要求。
关键代码示例
1. 下载数据集
假设我们已经有了数据集的下载链接,可以使用 Python 的 requests
库来下载数据集:
import requests
import os# 定义下载链接和保存路径
url = 'http://example.com/path/to/safety_equipment_detection_dataset.zip' # 替换为实际的下载链接
save_path = './safety_equipment_detection_dataset.zip'# 检查是否已经下载过
if not os.path.exists(save_path):print("Downloading dataset...")response = requests.get(url, stream=True)with open(save_path, 'wb') as f:for chunk in response.iter_content(chunk_size=8192):if chunk:f.write(chunk)print("Download complete.")
else:print("Dataset already exists.")# 解压数据集
import zipfile
with zipfile.ZipFile(save_path, 'r') as zip_ref:zip_ref.extractall('./safety_equipment_detection_dataset')
2. 加载和显示图像及其标注
以下是一个加载和显示图像及其标注框的示例:
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import xml.etree.ElementTree as ETdef load_image_and_annotations(image_path, annotation_path):image = Image.open(image_path).convert("RGB")tree = ET.parse(annotation_path)root = tree.getroot()annotations = []for obj in root.findall('object'):name = obj.find('name').textbbox = obj.find('bndbox')xmin = int(bbox.find('xmin').text)ymin = int(bbox.find('ymin').text)xmax = int(bbox.find('xmax').text)ymax = int(bbox.find('ymax').text)annotations.append((name, (xmin, ymin, xmax, ymax)))return image, annotationsdef display_image_with_annotations(image, annotations):fig, ax = plt.subplots(1, figsize=(10, 10))ax.imshow(image)for name, (xmin, ymin, xmax, ymax) in annotations:rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor='red', linewidth=2)ax.add_patch(rect)ax.text(xmin, ymin, name, fontsize=12, color='white', bbox=dict(facecolor='red', alpha=0.5))plt.axis('off')plt.show()# 示例路径
image_path = './safety_equipment_detection_dataset/images/image_0001.jpg'
annotation_path = './safety_equipment_detection_dataset/annotations/image_0001.xml'image, annotations = load_image_and_annotations(image_path, annotation_path)
display_image_with_annotations(image, annotations)
3. 创建 YOLO 格式的数据集
YOLO 模型需要特定格式的数据集,通常包括图像文件和对应的标注文件(.txt 格式)。以下是一个将 XML 标注转换为 YOLO 格式的示例:
import os
import xml.etree.ElementTree as ETdef convert_to_yolo_format(xml_dir, img_dir, yolo_dir, classes):os.makedirs(yolo_dir, exist_ok=True)for xml_file in os.listdir(xml_dir):if not xml_file.endswith('.xml'):continuetree = ET.parse(os.path.join(xml_dir, xml_file))root = tree.getroot()image_name = root.find('filename').textimage_path = os.path.join(img_dir, image_name)image = Image.open(image_path)width, height = image.sizeyolo_file = os.path.splitext(xml_file)[0] + '.txt'yolo_path = os.path.join(yolo_dir, yolo_file)with open(yolo_path, 'w') as f:for obj in root.findall('object'):name = obj.find('name').textclass_id = classes.index(name)bbox = obj.find('bndbox')xmin = int(bbox.find('xmin').text)ymin = int(bbox.find('ymin').text)xmax = int(bbox.find('xmax').text)ymax = int(bbox.find('ymax').text)x_center = (xmin + xmax) / 2.0 / widthy_center = (ymin + ymax) / 2.0 / heightbox_width = (xmax - xmin) / widthbox_height = (ymax - ymin) / heightf.write(f"{class_id} {x_center} {y_center} {box_width} {box_height}\n")# 定义类名
classes = ['belt', 'head', 'helmet', 'person', 'vest']# 转换标注文件
convert_to_yolo_format(xml_dir='./safety_equipment_detection_dataset/annotations',img_dir='./safety_equipment_detection_dataset/images',yolo_dir='./safety_equipment_detection_dataset/yolo_annotations',classes=classes
)
4. 使用 YOLOv5 进行目标检测
以下是一个使用 YOLOv5 进行目标检测的简单示例:
-
安装 YOLOv5:
git clone https://github.com/ultralytics/yolov5 cd yolov5 pip install -r requirements.txt
-
准备配置文件: 在
yolov5/data
目录下创建一个配置文件safety_equipment.yaml
,内容如下:train: ./safety_equipment_detection_dataset/images/train val: ./safety_equipment_detection_dataset/images/val nc: 5 names: ['belt', 'head', 'helmet', 'person', 'vest']
-
划分数据集: 将数据集划分为训练集和验证集(例如,80% 训练集,20% 验证集)。
-
训练模型:
python train.py --img 640 --batch 16 --epochs 50 --data safety_equipment.yaml --weights yolov5s.pt
-
评估模型:
python val.py --img 640 --batch 16 --data safety_equipment.yaml --weights runs/train/exp/weights/best.pt
-
推理和可视化:
from yolov5 import detect# 加载模型 model = torch.hub.load('ultralytics/yolov5', 'custom', path='runs/train/exp/weights/best.pt')# 推理单张图片 results = model(image_path)# 显示结果 results.show()
通过上述步骤,您将拥有一个完整的安全帽检测与安全带识别系统,包括数据集、预训练模型和相关的训练流程。希望这些代码能帮助您更好地利用该数据集!