工地扬尘检测数据集,3800张图片,支持yolo8+voc格式 智慧工地粉尘检测数据集
工地扬尘检测数据集3800张图片支持yolo8voc格式图片都是工地现场拍摄标注了“dust”这个类别适合做扬尘检测模型训练用工地扬尘检测数据集 YOLOv8 训练全套方案一、数据集基础信息1. 数据集概况总数量3800 张工地现场实拍图像标注类别单类别dust扬尘标注格式YOLO VOC 双格式兼容适用场景工地扬尘识别、环境扬尘监测模型训练推荐划分比例训练集:验证集:测试集 7:2:12. 目录结构标准双格式construction_dust_dataset/ ├── JPEGImages/ # 原图VOC/YOLO共用图片 ├── Annotations/ # VOC格式 .xml 标注文件 ├── labels/ # YOLO格式 .txt 标注文件 ├── ImageSets/ │ └── Main/ │ ├── train.txt │ ├── val.txt │ └── test.txt └── dust_yolov8.yaml # YOLOv8 数据集配置文件3. 标注格式说明YOLO 格式labels/ 下 .txt格式类别id x y w h坐标归一化单类别固定0 0.421 0.356 0.512 0.4890dust扬尘VOC 格式Annotations/ 下 .xml核心节点示例objectnamedust/nameposeUnspecified/posetruncated0/truncateddifficult0/difficultbndboxxmin120/xminymin90/yminxmax450/xmaxymax320/ymax/bndbox/object二、数据集配置文件dust_yolov8.yaml放置在数据集根目录YOLOv8 训练入口配置# 数据集根路径path:./construction_dust_datasettrain:images/trainval:images/valtest:images/test# 类别数 类别名nc:1names:0:dust说明若已提前划分好images/train/images/val/labels/train/labels/val直接使用上述配置。三、环境依赖安装# 核心依赖pipinstallultralytics opencv-python numpy tqdm lxml四、数据集划分脚本3800张 7:2:1 自动划分功能自动拆分图片标签生成train/val/test文件夹适配 YOLO 标准目录。新建split_dataset.pyimportosimportrandomimportshutil# 配置参数img_dir./construction_dust_dataset/JPEGImageslabel_dir./construction_dust_dataset/labelssave_root./construction_dust_datasettrain_ratio0.7val_ratio0.2test_ratio0.1# 创建目录defmake_dir(path):ifnotos.path.exists(path):os.makedirs(path)img_trainos.path.join(save_root,images/train)img_valos.path.join(save_root,images/val)img_testos.path.join(save_root,images/test)label_trainos.path.join(save_root,labels/train)label_valos.path.join(save_root,labels/val)label_testos.path.join(save_root,labels/test)fordin[img_train,img_val,img_test,label_train,label_val,label_test]:make_dir(d)# 获取所有图片img_list[fforfinos.listdir(img_dir)iff.endswith((.jpg,.png,.jpeg))]random.shuffle(img_list)totallen(img_list)# 计算划分数量train_numint(total*train_ratio)val_numint(total*val_ratio)train_setimg_list[:train_num]val_setimg_list[train_num:train_numval_num]test_setimg_list[train_numval_num:]# 复制文件defcopy_file(file_list,dst_img,dst_label):forfileinfile_list:nameos.path.splitext(file)[0]# 复制图片shutil.copy(os.path.join(img_dir,file),os.path.join(dst_img,file))# 复制标签txt_filename.txtifos.path.exists(os.path.join(label_dir,txt_file)):shutil.copy(os.path.join(label_dir,txt_file),os.path.join(dst_label,txt_file))copy_file(train_set,img_train,label_train)copy_file(val_set,img_val,label_val)copy_file(test_set,img_test,label_test)print(f划分完成)print(f训练集{len(train_set)}张)print(f验证集{len(val_set)}张)print(f测试集{len(test_set)}张)运行后自动生成 YOLO 标准images/labels分级目录。五、VOC ↔ YOLO 格式互转脚本双格式兼容1. VOC XML 转 YOLO TXTvoc2yolo.pyimportosimportxml.etree.ElementTreeasET classes[dust]# 仅扬尘一类xml_path./construction_dust_dataset/Annotationssave_txt_path./construction_dust_dataset/labelsimg_w1920img_h1080ifnotos.path.exists(save_txt_path):os.makedirs(save_txt_path)defconvert_xml_to_yolo(xml_file):treeET.parse(xml_file)roottree.getroot()sizeroot.find(size)wint(size.find(width).text)hint(size.find(height).text)txt_nameos.path.basename(xml_file).replace(.xml,.txt)txt_pathos.path.join(save_txt_path,txt_name)withopen(txt_path,w,encodingutf-8)asf:forobjinroot.iter(object):cls_nameobj.find(name).textifcls_namenotinclasses:continuecls_idclasses.index(cls_name)bndobj.find(bndbox)x1float(bnd.find(xmin).text)y1float(bnd.find(ymin).text)x2float(bnd.find(xmax).text)y2float(bnd.find(ymax).text)# 归一化坐标x(x1x2)/2.0/w y(y1y2)/2.0/h bw(x2-x1)/w bh(y2-y1)/h f.write(f{cls_id}{x:.6f}{y:.6f}{bw:.6f}{bh:.6f}\n)forxmlinos.listdir(xml_path):ifxml.endswith(.xml):convert_xml_to_yolo(os.path.join(xml_path,xml))print(VOC转YOLO完成)2. YOLO TXT 转 VOC XML备用yolo2voc.pyimportosfromPILimportImage classes[dust]img_path./construction_dust_dataset/JPEGImagestxt_path./construction_dust_dataset/labelssave_xml_path./construction_dust_dataset/Annotationsifnotos.path.exists(save_xml_path):os.makedirs(save_xml_path)defconvert_yolo_to_xml(txt_file,img_file):imgImage.open(img_file)w,himg.size nameos.path.basename(txt_file).replace(.txt,)xml_contentfannotation folderJPEGImages/folder filename{os.path.basename(img_file)}/filename size width{w}/width height{h}/height depth3/depth /size withopen(txt_file,r)asf:linesf.readlines()forlineinlines:lineline.strip()ifnotline:continuecls_id,x,y,bw,bhmap(float,line.split())cls_nameclasses[int(cls_id)]# 反归一化x1int((x-bw/2)*w)y1int((y-bh/2)*h)x2int((xbw/2)*w)y2int((ybh/2)*h)xml_contentf object name{cls_name}/name poseUnspecified/pose truncated0/truncated difficult0/difficult bndbox xmin{x1}/xmin ymin{y1}/ymin xmax{x2}/xmax ymax{y2}/ymax /bndbox /object xml_content/annotationxml_pathos.path.join(save_xml_path,name.xml)withopen(xml_path,w,encodingutf-8)asf:f.write(xml_content)fortxtinos.listdir(txt_path):iftxt.endswith(.txt):nametxt.replace(.txt,)forsufin[.jpg,.png,.jpeg]:img_fullos.path.join(img_path,namesuf)ifos.path.exists(img_full):convert_yolo_to_xml(os.path.join(txt_path,txt),img_full)breakprint(YOLO转VOC完成)六、YOLOv8 训练代码train_dust.py适配工地扬尘单类别任务参数针对户外复杂场景、雾气/扬尘特征优化fromultralyticsimportYOLOdeftrain_construction_dust():# 加载预训练模型n轻量 / s均衡 / m高精度按需选择modelYOLO(yolov8s.pt)# 训练参数resultsmodel.train(data./construction_dust_dataset/dust_yolov8.yaml,epochs120,# 训练轮数imgsz640,# 输入尺寸batch16,# 显存不足改为 8/4device0,# CPU运行改为 devicecpuworkers4,patience20,# 早停pretrainedTrue,optimizerAdam,lr00.001,lrf0.01,warmup_epochs5,mosaic0.8,# 降低马赛克适配自然扬尘场景mixup0.1,copy_paste0.0,hsv_h0.015,hsv_s0.7,hsv_v0.4,projectruns/dust_train,nameyolov8_dust,exist_okTrue,# 扬尘为大面积模糊目标调低置信过滤conf0.25)print(训练结束最优模型路径,results.save_dir/weights/best.pt)if__name____main__:train_construction_dust()七、推理测试代码predict_dust.py图片/文件夹批量检测fromultralyticsimportYOLO# 加载训练好的模型modelYOLO(./runs/dust_train/yolov8_dust/weights/best.pt)# 1. 单张图片推理# res model(test.jpg, saveTrue)# 2. 文件夹批量推理resmodel(source./test_images,saveTrue,conf0.3)print(推理完成结果已保存)八、完整使用流程整理 3800 张图片放入JPEGImagesVOC 标注放入Annotations运行voc2yolo.py生成 YOLO 标签运行split_dataset.py按 7:2:1 划分数据集确认dust_yolov8.yaml路径无误执行train_dust.py开始训练训练完成后用predict_dust.py测试效果。九、调优建议工地扬尘场景专属漏检严重调高imgsz800降低推理/训练conf0.2~0.3误检多增大patience、适当减少数据增强mosaic0.5显存不足减小batch使用yolov8n.pt轻量模型光线复杂强光/阴天保留默认 HSV 色域增强提升泛化能力。

相关新闻