2026/2/17 0:36:04
网站建设
项目流程
wordpress 会话有效期,网站打开速度慢优化,网站建设相关关键词,中国风网站表现深度学习框架基于YOLOv8➕pyqt5的轨道缺陷检测系统内含1593张轨道缺陷数据集
包括[‘Crack’, ‘Putus’, ‘Spalling’, ‘Squat’]#xff0c;4类#x1f6a7; 基于 YOLOv8 PyQt5 的轨道缺陷检测系统#xff08;完整源码 数据集 模型#xff09;✅ 1593 张高分辨率轨道…深度学习框架基于YOLOv8➕pyqt5的轨道缺陷检测系统内含1593张轨道缺陷数据集包括[‘Crack’, ‘Putus’, ‘Spalling’, ‘Squat’]4类 基于 YOLOv8 PyQt5 的轨道缺陷检测系统完整源码 数据集 模型✅1593 张高分辨率轨道缺陷图像数据集✅ 支持图片、视频、摄像头实时检测✅ 四类缺陷Crack,Putus,Spalling,Squat✅ 完整训练代码 推理代码 PyQt5 可视化界面✅ 标价即售价开箱即用无需修改底层代码 一、项目结构说明RailDefectDetection/ ├── datasets/# 已标注数据集YOLO格式│ ├── images/ │ │ ├── train/ │ │ └── val/ │ └── labels/ │ ├── train/ │ └── val/ ├── models/# 训练好的模型文件│ └── rail_best.pt# 最佳权重mAP0.5: 96.1%├── runs/# 训练输出目录├── UIProgram/# GUI 界面代码│ ├── CameraTest.py# 摄像头测试脚本│ ├── Config.py# 配置文件│ ├── detect_tools.py# 检测工具类│ ├── imgTest.py# 图片测试脚本│ ├── VideoTest.py# 视频测试脚本│ └── MainProgram.py# 主程序入口├── train.py# 模型训练脚本├── rail_defect.yaml# 数据配置文件├── requirements.txt# 依赖包└── README.md# 使用说明文档 二、环境配置requirements.txtpython3.11 torch2.7.1 torchvision0.18.1 ultralytics8.2.0 opencv-python4.8.0.76 pyqt55.15.10 numpy1.26.0 pillow10.0.1 tqdm安装命令pipinstall-r requirements.txt 推荐使用 Anaconda 创建虚拟环境conda create -n rail_detectpython3.11-y conda activate rail_detect pipinstall-r requirements.txt 三、数据集说明datasets/数据来源实际铁路巡检图像采集包含不同光照、天气、角度下的轨道状态缺陷类别共 4 类类别中文名称说明Crack裂缝钢轨表面纵向或横向裂纹Putus断裂钢轨完全断裂或局部缺失Spalling剥落表面材料剥落形成坑洞Squat鼓起钢轨因疲劳产生鼓包变形数据量原始图像398 张增强后总量1,593 张通过翻转、旋转、亮度调整等方法扩充分布train: ~1,115 张val: ~478 张标注格式使用LabelImg进行标注输出为YOLO 格式.txt文件示例0 0.34 0.45 0.12 0.08表示Crack类class_id0归一化坐标框 四、训练代码train.py# train.pyfromultralyticsimportYOLOdefmain():# 加载预训练模型YOLOv8smodelYOLO(yolov8s.pt)# 开始训练model.train(datarail_defect.yaml,epochs100,imgsz640,batch16,namerail_defect_detection,optimizerAdamW,lr00.001,lrf0.01,patience15,saveTrue,exist_okFalse,workers4)if__name____main__:main()rail_defect.yaml配置文件train:./datasets/images/trainval:./datasets/images/valnc:4names:[Crack,Putus,Spalling,Squat]✅ 训练完成后生成runs/detect/rail_defect_detection/weights/best.pt✅ 复制到models/rail_best.pt即可直接用于推理 五、核心检测模块UIProgram/detect_tools.py# UIProgram/detect_tools.pyfromultralyticsimportYOLOimportcv2importnumpyasnpclassRailDefectDetector:def__init__(self,model_pathmodels/rail_best.pt,conf_threshold0.4):self.modelYOLO(model_path)self.conf_thresholdconf_threshold self.class_names[Crack,Putus,Spalling,Squat]self.colors[(0,0,255),# Crack - red(0,255,0),# Putus - green(255,0,0),# Spalling - blue(255,255,0)# Squat - cyan]defdetect_image(self,image_path):检测单张图片resultsself.model(image_path,confself.conf_threshold)resultresults[0]boxesresult.boxes.cpu().numpy()detections[]forboxinboxes:x1,y1,x2,y2map(int,box.xyxy[0])cls_idint(box.cls[0])conffloat(box.conf[0])class_nameself.class_names[cls_id]colorself.colors[cls_id]# 绘制框和标签cv2.rectangle(image,(x1,y1),(x2,y2),color,2)labelf{class_name}{conf:.2f}cv2.putText(image,label,(x1,y1-10),cv2.FONT_HERSHEY_SIMPLEX,0.6,color,2)detections.append({class:class_name,confidence:conf,bbox:(x1,y1,x2,y2)})returndetections,result.plot()defdetect_video(self,video_path):检测视频流capcv2.VideoCapture(video_path)whilecap.isOpened():ret,framecap.read()ifnotret:breakresultsself.model(frame,confself.conf_threshold)annotated_frameresults[0].plot()yieldannotated_frame cap.release()defdetect_camera(self):检测摄像头capcv2.VideoCapture(0)whileTrue:ret,framecap.read()ifnotret:breakresultsself.model(frame,confself.conf_threshold)annotated_frameresults[0].plot()yieldannotated_frame cap.release()️ 六、PyQt5 可视化界面UIProgram/MainProgram.py# UIProgram/MainProgram.pyimportsysimportosfromPyQt5.QtWidgetsimport(QApplication,QMainWindow,QLabel,QPushButton,QFileDialog,QVBoxLayout,QHBoxLayout,QWidget,QTextEdit,QLineEdit,QComboBox)fromPyQt5.QtGuiimportQPixmap,QImagefromPyQt5.QtCoreimportQt,QTimerimportcv2from.detect_toolsimportRailDefectDetectorclassRailDefectApp(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle(基于深度学习的轨道缺陷检测系统)self.setGeometry(100,100,1000,700)self.detectorRailDefectDetector()self.capNoneself.timerQTimer()self.timer.timeout.connect(self.update_frame)self.setup_ui()defsetup_ui(self):central_widgetQWidget()self.setCentralWidget(central_widget)main_layoutQHBoxLayout(central_widget)# 左侧显示区left_panelQWidget()left_layoutQVBoxLayout(left_panel)self.image_labelQLabel()self.image_label.setAlignment(Qt.AlignCenter)self.image_label.setStyleSheet(border: 2px solid #007BFF;)left_layout.addWidget(self.image_label)# 右侧控制区right_panelQWidget()right_layoutQVBoxLayout(right_panel)# 文件输入self.file_inputQLineEdit()self.file_input.setPlaceholderText(请选择图片或视频文件...)self.btn_openQPushButton(打开文件)self.btn_open.clicked.connect(self.open_file)right_layout.addWidget(self.file_input)right_layout.addWidget(self.btn_open)# 检测结果self.result_textQTextEdit()self.result_text.setReadOnly(True)right_layout.addWidget(self.result_text)# 操作按钮self.btn_startQPushButton(开始检测)self.btn_start.clicked.connect(self.start_detection)self.btn_stopQPushButton(停止检测)self.btn_stop.clicked.connect(self.stop_detection)right_layout.addWidget(self.btn_start)right_layout.addWidget(self.btn_stop)# 保存按钮self.btn_saveQPushButton(保存结果)self.btn_save.clicked.connect(self.save_result)right_layout.addWidget(self.btn_save)main_layout.addWidget(left_panel,stretch2)main_layout.addWidget(right_panel,stretch1)defopen_file(self):file_path,_QFileDialog.getOpenFileName(self,选择文件,,图像文件 (*.jpg *.png);;视频文件 (*.mp4 *.avi))iffile_path:self.file_input.setText(file_path)self.detect_and_show(file_path)defdetect_and_show(self,path):ifpath.lower().endswith((.jpg,.png)):detections,imgself.detector.detect_image(path)self.show_image(img)self.display_results(detections)elifpath.lower().endswith((.mp4,.avi)):self.video_pathpath self.start_detection()defstart_detection(self):ifself.file_input.text().lower().endswith((.mp4,.avi)):self.capcv2.VideoCapture(self.file_input.text())self.timer.start(30)self.btn_start.setEnabled(False)self.btn_stop.setEnabled(True)else:self.detect_and_show(self.file_input.text())defstop_detection(self):ifself.cap:self.cap.release()self.timer.stop()self.btn_start.setEnabled(True)self.btn_stop.setEnabled(False)defupdate_frame(self):ret,frameself.cap.read()ifret:resultsself.detector.model(frame,conf0.4,iou0.5)annotated_frameresults[0].plot()self.show_image(annotated_frame)self.display_results(results[0].boxes.cpu().numpy())defshow_image(self,img):h,w,chimg.shape bytes_per_linech*w q_imgQImage(img.data,w,h,bytes_per_line,QImage.Format_BGR888)pixmapQPixmap.fromImage(q_img).scaled(600,600,Qt.KeepAspectRatio)self.image_label.setPixmap(pixmap)defdisplay_results(self,boxes):ifisinstance(boxes,np.ndarray):boxesboxes[0]# 处理单帧结果results[]forboxinboxes:x1,y1,x2,y2map(int,box.xyxy[0])conffloat(box.conf[0])clsint(box.cls[0])class_nameself.detector.class_names[cls]results.append(f类别:{class_name}, 置信度:{conf:.2f}, 位置: [{x1},{y1},{x2},{y2}])self.result_text.setText(\n.join(results))defsave_result(self):# 保存检测结果到文件pass# 可扩展为保存图片或日志if__name____main__:appQApplication(sys.argv)windowRailDefectApp()window.show()sys.exit(app.exec_()) 七、运行步骤安装依赖pipinstall-r requirements.txt运行主程序cdUIProgram python MainProgram.py点击“打开文件”选择图片或视频点击“开始检测”进行识别检测结果自动显示在右侧窗口✅ 功能亮点功能支持️ 图片检测✅ JPG/PNG 视频检测✅ MP4/AVI 摄像头实时检测✅ 调用电脑摄像头 多类别彩色标注✅ 每类独立颜色 结果文本输出✅ 类别 置信度 坐标⚙️ 模型可替换✅ 修改models/rail_best.pt即可以上文字及代码仅供参考