当前位置: 首页 > news >正文

道路积水数据集 路面积水识别数据集 图片数量4524,xml和txt标签都有;公路积水数据集 ✓类别:puddle;

道路积水数据集 路面积水识别数据集 图片数量4524,xml和txt标签都有;公路积水数据集
✓类别:puddle;

✓图片数量4524,xml和txt标签都有;
✓类别:puddle;



好的,下面是一个完整的项目代码和详细配置说明,用于基于YOLOv8进行路面积水识别。该项目包括数据集准备、模型训练、评估和推理,并附带一个简单的Streamlit界面。

完整项目代码

1. 数据集准备与转换

首先,我们需要将数据集从XML格式转换为YOLO格式(如果数据集已经是YOLO格式,则跳过此步骤)。

importxml.etree.ElementTreeasETimportosfrompathlibimportPathdefconvert_xml_to_yolo(xml_dir,output_dir):forxml_fileinPath(xml_dir).glob('*.xml'):tree=ET.parse(xml_file)root=tree.getroot()img_name=root.find('filename').text img_width=int(root.find('size/width').text)img_height=int(root.find('size/height').text)txt_file_path=os.path.join(output_dir,Path(img_name).stem+'.txt')withopen(txt_file_path,'w')astxt_file:forobjinroot.findall('object'):cls=obj.find('name').textifcls!='puddle':continuebbox=obj.find('bndbox')xmin=float(bbox.find('xmin').text)ymin=float(bbox.find('ymin').text)xmax=float(bbox.find('xmax').text)ymax=float(bbox.find('ymax').text)x_center=(xmin+xmax)/2/img_width y_center=(ymin+ymax)/2/img_height width=(xmax-xmin)/img_width height=(ymax-ymin)/img_height txt_file.write(f"0{x_center}{y_center}{width}{height}\n")# 使用示例convert_xml_to_yolo('datasets/puddle_dataset/xml','datasets/puddle_dataset/yolo/labels')
2. 创建数据集配置文件 (data.yaml)

创建一个data.yaml文件来配置数据集路径和类别信息。

3. 分割数据集

为了训练和验证,我们需要将数据集分割成训练集和验证集。

importosimportrandomfrompathlibimportPathimportshutildefsplit_dataset(data_dir,train_ratio=0.8):images=list(Path(data_dir).glob('*.jpg'))random.shuffle(images)num_train=int(len(images)*train_ratio)train_images=images[:num_train]val_images=images[num_train:]train_dir=Path(data_dir).parent/'train'val_dir=Path(data_dir).parent/'val'train_img_dir=train_dir/'images'train_label_dir=train_dir/'labels'val_img_dir=val_dir/'images'val_label_dir=val_dir/'labels'train_img_dir.mkdir(parents=True,exist_ok=True)train_label_dir.mkdir(parents=True,exist_ok=True)val_img_dir.mkdir(parents=True,exist_ok=True)val_label_dir.mkdir(parents=True,exist_ok=True)forimgintrain_images:label_path=img.with_suffix('.txt')shutil.copy(img,train_img_dir/img.name)shutil.copy(label_path,train_label_dir/label_path.name)forimginval_images:label_path=img.with_suffix('.txt')shutil.copy(img,val_img_dir/img.name)shutil.copy(label_path,val_label_dir/label_path.name)# 使用示例split_dataset('./datasets/puddle_dataset/images')
4. 训练脚本

接下来是使用YOLOv8进行训练的脚本。

importtorchfromultralyticsimportYOLO# 设置随机种子以保证可重复性torch.manual_seed(42)# 定义数据集路径dataset_config='data.yaml'# 加载预训练的YOLOv8n模型model=YOLO('yolov8n.pt')# 训练模型results=model.train(data=dataset_config,epochs=100,imgsz=512,batch=16,name='puddle_detection',project='runs/train')# 评估模型metrics=model.val()# 保存最佳模型权重best_model_weights='runs/train/puddle_detection/weights/best.pt'print(f"Best model weights saved to{best_model_weights}")
5. 推理脚本

以下是一个简单的推理脚本,用于测试模型的性能。

fromultralyticsimportYOLOimportcv2importnumpyasnpfromPILimportImage# 加载模型model=YOLO('runs/train/puddle_detection/weights/best.pt')# 图片检测函数defdetect_image(model,image_path,conf_threshold=0.5):results=model.predict(image_path,conf=conf_threshold)[0]annotated_frame=annotate_image(image_path,results,model)returnannotated_frame# 标注图像函数defannotate_image(image_path,results,model):frame=cv2.imread(image_path)forresultinresults.boxes.cpu().numpy():r=result.xyxy[0].astype(int)cls=int(result.cls[0])conf=result.conf[0]label=f"{model.names[cls]}{conf:.2f}"color=(0,255,0)cv2.rectangle(frame,(r[0],r[1]),(r[2],r[3]),color,2)cv2.putText(frame,label,(r[0],r[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,color,2)returnframe# 测试一张图片test_image_path='./datasets/puddle_dataset/images/test_image.jpg'annotated_image=detect_image(model,test_image_path)cv2.imwrite('annotated_test_image.jpg',annotated_image)
6. Streamlit 主界面

最后,我们创建一个简单的Streamlit界面来进行实时推理和显示结果。

importstreamlitasstfromultralyticsimportYOLOimportcv2importnumpyasnpfromPILimportImageimporttempfile# 加载模型@st.cache_resourcedefload_model(weights_path):model=YOLO(weights_path)returnmodel# 图片检测函数defdetect_image(model,image,conf_threshold):results=model.predict(image,conf=conf_threshold)[0]annotated_frame=annotate_image(image,results,model)returnannotated_frame# 视频检测函数defdetect_video(model,video_path,conf_threshold):cap=cv2.VideoCapture(video_path)whilecap.isOpened():ret,frame=cap.read()ifnotret:breakresults=model.predict(frame,conf=conf_threshold)[0]annotated_frame=annotate_image(frame,results,model)yieldannotated_frame cap.release()# 摄像头检测函数defdetect_camera(model,conf_threshold):cap=cv2.VideoCapture(0)whileTrue:ret,frame=cap.read()ifnotret:breakresults=model.predict(frame,conf=conf_threshold)[0]annotated_frame=annotate_image(frame,results,model)yieldannotated_frame cap.release()# 标注图像函数defannotate_image(image,results,model):forresultinresults.boxes.cpu().numpy():r=result.xyxy[0].astype(int)cls=int(result.cls[0])conf=result.conf[0]label=f"{model.names[cls]}{conf:.2f}"color=(0,255,0)cv2.rectangle(image,(r[0],r[1]),(r[2],r[3]),color,2)cv2.putText(image,label,(r[0],r[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,color,2)returnimage# Streamlit 主界面defmain():st.title("Puddle Detection System")# 动态加载模型weights_options=['best_puddle_yolov8.pt']# 添加更多权重文件路径selected_weights=st.sidebar.selectbox("Select Model Weights",weights_options)model=load_model(selected_weights)# 动态调整置信度阈值conf_threshold=st.sidebar.slider("Confidence Threshold",min_value=0.0,max_value=1.0,value=0.5,step=0.01)# 输入方式选择input_type=st.sidebar.radio("Input Type",["Image","Video","Camera"])ifinput_type=="Image":uploaded_file=st.file_uploader("Upload an image...",type=["jpg","jpeg","png"])ifuploaded_fileisnotNone:image=Image.open(uploaded_file)image_np=np.array(image)annotated_image=detect_image(model,image_np,conf_threshold)st.image(annotated_image,channels="BGR",caption="Detected Image")# 统计检测到的物体数量results=model.predict(image_np,conf=conf_threshold)[0]class_counts={}forresultinresults.boxes.cpu().numpy():cls=int(result.cls[0])class_name=model.names[cls]ifclass_nameinclass_counts:class_counts[class_name]+=1else:class_counts[class_name]=1st.subheader("Detection Summary:")forclass_name,countinclass_counts.items():st.write(f"{class_name}:{count}")elifinput_type=="Video":uploaded_file=st.file_uploader("Upload a video...",type=["mp4","avi"])ifuploaded_fileisnotNone:tfile=tempfile.NamedTemporaryFile(delete=False)tfile.write(uploaded_file.read())tfpath=tfile.name cap=cv2.VideoCapture(tfpath)frame_placeholder=st.empty()forannotated_frameindetect_video(model,tfpath,conf_threshold):frame_placeholder.image(annotated_frame,channels="BGR",use_column_width=True)cap.release()os.remove(tfpath)elifinput_type=="Camera":frame_placeholder=st.empty()forannotated_frameindetect_camera(model,conf_threshold):frame_placeholder.image(annotated_frame,channels="BGR",use_column_width=True)if__name__=="__main__":main()

文件结构

puddle_detection/ ├── main.py ├── datasets/ │ └── puddle_dataset/ │ ├── xml/ │ │ ├── image1.xml │ │ ├── image2.xml │ │ └── ... │ ├── yolo/ │ │ ├── labels/ │ │ │ ├── image1.txt │ │ │ ├── image2.txt │ │ │ └── ... │ │ ├── train/ │ │ │ ├── images/ │ │ │ │ ├── image1.jpg │ │ │ │ ├── image2.jpg │ │ │ │ └── ... │ │ │ └── labels/ │ │ │ ├── image1.txt │ │ │ ├── image2.txt │ │ │ └── ... │ │ └── val/ │ │ ├── images/ │ │ │ ├── image1.jpg │ │ │ ├── image2.jpg │ │ │ └── ... │ │ └── labels/ │ │ ├── image1.txt │ │ ├── image2.txt │ │ └── ... │ └── images/ │ ├── image1.jpg │ ├── image2.jpg │ └── ... ├── best_puddle_yolov8.pt └── requirements.txt

安装依赖项

首先,确保你已经安装了所有必要的依赖项。你可以通过以下命令安装:

pipinstall-rrequirements.txt

requirements.txt内容如下:

streamlit==1.25.0 opencv-python torch==2.0 ultralytics

运行步骤总结

  1. 克隆项目仓库(如果有的话):

    gitclone https://github.com/yourusername/puddle_detection.gitcdpuddle_detection
  2. 安装依赖项

    pipinstall-rrequirements.txt
  3. 数据集准备

    • 如果数据集尚未转换,请运行数据集转换脚本。
      python convert_xml_to_yolo.py
    • 如果数据集尚未分割,请运行数据集分割脚本。
      python split_dataset.py
    • 确保数据集目录结构正确。
  4. 训练模型

    python train.py
  5. 评估模型
    在训练脚本中,模型会在训练结束后自动进行评估。

  6. 推理测试

    python inference.py
  7. 运行Streamlit应用

    streamlit run main.py

总结

以上是完整的基于YOLOv8的路面积水识别系统的项目介绍和代码实现。该项目支持图片、视频识别以及本地摄像头识别,并且可以通过UI界面动态调节模型置信度和选择不同的模型权重。希望这些详细的信息和代码能够帮助你顺利实施和优化你的路面积水识别系统。

http://www.zskr.cn/news/1458856.html

相关文章:

  • 第九章:Token 优化与高效省钱配置(重点)
  • 语义内核形式化模型:AI内容生成的统一数学原理与工程实践
  • Vue版Cesium卫星轨道+雷达扫描三维可视化组件(含CZML数据与小程序适配)
  • 气缸驱动并联机器人位姿控制策略【附仿真】
  • DeepSeek V4实测:百万上下文与MoE架构如何重构AI成本模型
  • 深耕车载数字健康场景,守护全维度驾乘安全与体验
  • GBase 8s数据库高可用之—RHAC远程高可用集群详解
  • 别慌!网站突然打不开显示Error 522?手把手教你排查百度云加速与源站的连接问题
  • 第七章:自定义命令、规则与上下文
  • 仓储软件(WMS)值得推荐的选择方向 - 品牌排行榜
  • 利用快马平台快速构建potplayer字幕翻译工具原型
  • 如何快速定位手机号码归属地:三步完成精准查询
  • 合规红线下的智能外呼:如何用RAG+本地化语音模型通过银保监AI外呼备案(附过审配置清单)
  • Determined:一个集成的深度学习训练平台
  • 计算机重装系统出现SYSTEM磁盘?
  • 第十章:最佳实践、完整配置模板与排障
  • 基于深度学习的文本自动摘要系统
  • PHP域名解析与CDN加速技术
  • 推荐一个适合维保公司的报修系统,支持多报修单位独立管理
  • All-in-one数据底座的价值与实践:基于Harness的解读
  • STK COM互联避坑指南:手把手教你用MATLAB创建向量和角度,解决‘名字重复报错’和‘参数设置’难题
  • C#抽象类接口 项目实操选型清单(开发直接对照)
  • C#抽象类 接口 面试 3 道笔试题(含标准答案,面试高频)
  • 第三章:界面操作、会话管理与内置命令
  • C#抽象类 接口一页纸速记(面试随身背诵)
  • 利用快马平台快速原型设计,十分钟搭建探长u盘修复工具界面demo
  • STM32 Bootloader跳转App总进HardFault?一个PSP/MSP模式切换的坑我帮你踩了
  • WeChatExporter:三步永久保存你的微信聊天记录,告别数据丢失的烦恼
  • STM32驱动TM1616数码管避坑指南:时序调试与硬件连接那些事儿
  • ai辅助开发:为内容平台添加智能标签提取功能(灵感源于ao3)