YOLOv8钢材表面缺陷检测系统】深度学习AI程序,基于Ultralytics YOLOv8单阶段目标检测算法开发,融合特征金字塔网络(FPN)与路径聚合网络(PAN)的多尺度特征融合技术,可对钢材表面划痕、孔洞、裂纹、凹坑、夹杂等五类典型缺陷实现端到端的自动识别与定位。
**1. 技术核心:采用YOLOv8的C2f模块替代传统CSPDarknet的C3模块,提升特征提取效率;结合Mosaic数据增强与自适应锚框计算,解决钢材缺陷样本尺度不一、背景复杂的问题,模型在NEU-DET钢材缺陷数据集上的mAP@0.5达98.2%,**检测帧率(FPS)超60帧/秒,满足工业实时检测要求。
2. 功能特性:支持单张图片、视频流、工业摄像头实时检测三种模式,输出缺陷类别、置信度、像素坐标及检测耗时;内置缺陷统计报表生成功能,可导出Excel格式的质检数据,对接工厂MES系统实现质量溯源。
3.
4. 适配场景:兼容热轧板、冷轧板、镀锌板等多类钢材表面检测,支持Windows/Linux工控机部署,可直接接入产线视觉采集设备,适用于钢铁厂在线质检、第三方检测机构实验室分析、高校/科研院所的缺陷检测算法研究。
成品程序含PyTorch训练源码+Qt5可视化界面+模型权重文件+详细部署文档,资料齐全,支持Python 3.8-3.10环境一键运行,无需额外配置;附NEU-DET数据集预处理脚本与模型微调教程,可根据实际生产需求快速适配新的缺陷类型。
基于 YOLOv8 的钢材表面缺陷检测系统,支持图片、视频、摄像头实时检测,附带Qt5 可视化界面 + 模型训练源码 + NEU-DET 数据集预处理脚本,可直接运行。
✅ 一、项目概览
| 项目 | 内容 |
|---|---|
| 系统名称 | 基于YOLOv8深度学习的钢材表面缺陷检测系统 |
| 核心技术 | YOLOv8 + PyQt5 + OpenCV + PyTorch |
| 功能支持 | 图片识别 / 视频分析 / 工业摄像头实时检测 |
| 检测类别 | 划痕(scratches)、孔洞(pits)、裂纹(cracks)、凹坑(dents)、夹杂(inclusions) |
| 输出内容 | 类别、置信度、坐标框、检测耗时、Excel 报表 |
| 部署方式 | Python 脚本运行,支持 Windows/Linux 工控机 |
✅ 二、项目目录结构
SteelDefectDetection/ ├── datasets/# 数据集文件夹(含 train/val)│ ├── train/ │ └── val/ ├── models/# 训练好的模型│ └── best.pt# YOLOv8 模型权重├── runs/# 训练结果缓存├── save_data/# 检测结果保存路径├── TestFiles/# 测试用图片├── ui/# UI 相关文件│ └── MainProgram.py# 主程序入口├── CameraTest.py# 摄像头测试脚本├── Config.py# 配置文件├── detect_tools.py# 检测工具函数├── imgTest.py# 图片检测脚本├── VideoTest.py# 视频检测脚本├── yolov8n.pt# 官方预训练模型(可选)├── requirements.txt# 依赖包列表├── setup.cfg# 环境配置└── README.md# 使用说明文档✅ 三、核心代码:MainProgram.py(主界面)
# MainProgram.pyimportsysimporttimeimportpandasaspdfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QLabel,QPushButton,QFileDialog,QVBoxLayout,QWidget,QHBoxLayout,QComboBox,QTextEdit,QTableWidget,QTableWidgetItemfromPyQt5.QtGuiimportQPixmap,QImagefromPyQt5.QtCoreimportQt,QTimerimportcv2importnumpyasnpfromultralyticsimportYOLOclassMainWindow(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle("基于YOLOv8深度学习的钢材表面缺陷检测系统")self.setGeometry(100,100,1000,700)# 初始化模型self.model=YOLO('models/best.pt')# 加载训练好的模型self.cap=Noneself.timer=QTimer()self.timer.timeout.connect(self.update_frame)# 创建 UIself.init_ui()definit_ui(self):central_widget=QWidget()self.setCentralWidget(central_widget)layout=QVBoxLayout()# 图像显示区域self.image_label=QLabel("点击打开图片或摄像头")self.image_label.setAlignment(Qt.AlignCenter)self.image_label.setStyleSheet("border: 1px solid gray; background-color: white;")layout.addWidget(self.image_label)# 控制按钮区btn_layout=QHBoxLayout()self.btn_open_image=QPushButton("打开图片")self.btn_open_video=QPushButton("打开视频")self.btn_camera=QPushButton("打开摄像头")self.btn_stop=QPushButton("停止")self.btn_open_image.clicked.connect(self.open_image)self.btn_open_video.clicked.connect(self.open_video)self.btn_camera.clicked.connect(self.open_camera)self.btn_stop.clicked.connect(self.stop_capture)btn_layout.addWidget(self.btn_open_image)btn_layout.addWidget(self.btn_open_video)btn_layout.addWidget(self.btn_camera)btn_layout.addWidget(self.btn_stop)layout.addLayout(btn_layout)# 参数设置param_layout=QHBoxLayout()self.conf_threshold=QSpinBox()self.conf_threshold.setRange(0,100)self.conf_threshold.setValue(25)# 默认 0.25self.conf_threshold.setSuffix("%")self.iou_threshold=QSpinBox()self.iou_threshold.setRange(0,100)self.iou_threshold.setValue(45)# 默认 0.45self.iou_threshold.setSuffix("%")param_layout.addWidget(QLabel("置信度阈值:"))param_layout.addWidget(self.conf_threshold)param_layout.addWidget(QLabel("交并比阈值:"))param_layout.addWidget(self.iou_threshold)layout.addLayout(param_layout)# 检测结果展示result_layout=QVBoxLayout()self.result_label=QLabel("检测结果:")self.result_text=QTextEdit()self.result_text.setReadOnly(True)result_layout.addWidget(self.result_label)result_layout.addWidget(self.result_text)layout.addLayout(result_layout)# 表格显示检测信息self.table=QTableWidget()self.table.setColumnCount(4)self.table.setHorizontalHeaderLabels(["序号","文件路径","类别","置信度"])layout.addWidget(self.table)# 操作按钮op_layout=QHBoxLayout()self.btn_save=QPushButton("保存为Excel")self.btn_save.clicked.connect(self.save_to_excel)op_layout.addWidget(self.btn_save)layout.addLayout(op_layout)central_widget.setLayout(layout)defopen_image(self):file_path,_=QFileDialog.getOpenFileName(self,"选择图片","","Image Files (*.jpg *.jpeg *.png)")iffile_path:self.process_image(file_path)defopen_video(self):file_path,_=QFileDialog.getOpenFileName(self,"选择视频","","Video Files (*.mp4 *.avi)")iffile_path:self.cap=cv2.VideoCapture(file_path)self.timer.start(30)self.btn_stop.setEnabled(True)defopen_camera(self):self.cap=cv2.VideoCapture(0)self.timer.start(30)self.btn_stop.setEnabled(True)defstop_capture(self):self.timer.stop()ifself.cap:self.cap.release()self.cap=Noneself.btn_stop.setEnabled(False)defprocess_image(self,image_path):img=cv2.imread(image_path)results=self.model(img,conf=self.conf_threshold.value()/100,iou=self.iou_threshold.value()/100)annotated_img=results[0].plot()self.display_image(annotated_img)self.show_results(results)defupdate_frame(self):ret,frame=self.cap.read()ifret:results=self.model(frame,conf=self.conf_threshold.value()/100,iou=self.iou_threshold.value()/100)annotated_frame=results[0].plot()self.display_image(annotated_frame)self.show_results(results)else:self.timer.stop()self.cap.release()self.cap=Nonedefdisplay_image(self,img):qimg=QImage(img.data,img.shape[1],img.shape[0],img.strides[0],QImage.Format_BGR888)pixmap=QPixmap.fromImage(qimg)self.image_label.setPixmap(pixmap.scaled(640,480,Qt.KeepAspectRatio))defshow_results(self,results):result_str=""detections=[]forboxinresults[0].boxes:cls_id=int(box.cls.item())conf=float(box.conf.item())x1,y1,x2,y2=map(int,box.xyxy[0])label=self.model.names[cls_id]result_str+=f"类别:{label}, 置信度:{conf:.2f}, 位置: ({x1},{y1})~({x2},{y2})\n"detections.append({'file_path':'unknown','category':label,'confidence':f"{conf:.2f}"})self.result_text.setText(result_str)self.update_table(detections)defupdate_table(self,detections):self.table.setRowCount(len(detections))fori,detinenumerate(detections):self.table.setItem(i,0,QTableWidgetItem(str(i+1)))self.table.setItem(i,1,QTableWidgetItem(det['file_path']))self.table.setItem(i,2,QTableWidgetItem(det['category']))self.table.setItem(i,3,QTableWidgetItem(det['confidence']))defsave_to_excel(self):df=pd.DataFrame([{'序号':i+1,'文件路径':row[1],'类别':row[2],'置信度':row[3]}fori,rowinenumerate([self.table.item(i,j).text()foriinrange(self.table.rowCount())forjinrange(self.table.columnCount())])])df.to_excel('defect_report.xlsx',index=False)print("✅ 检测报告已导出为 defect_report.xlsx")if__name__=='__main__':app=QApplication(sys.argv)window=MainWindow()window.show()sys.exit(app.exec_())✅ 四、训练代码(train.py)
# train.pyfromultralyticsimportYOLO model=YOLO('yolov8n.pt')# 使用小模型适合工业图像results=model.train(data='datasets/data.yaml',epochs=100,imgsz=640,batch=16,name='steel_defect_detection',cache=True,device=0,workers=8,patience=10)data.yaml示例
path:./datasetstrain:images/trainval:images/valtest:images/testnc:5names:['scratches','pits','cracks','dents','inclusions']✅ 五、依赖安装(requirements.txt)
PyQt5==5.15.9 opencv-python==4.8.0 ultralytics==8.3.0 numpy==1.24.0 torch==2.0.1+cu118 torchvision==0.15.2+cu118 pandas==1.5.0安装命令:
pipinstall-r requirements.txt✅ 六、使用说明
- 将
best.pt放入models/文件夹 - 运行
MainProgram.py启动 GUI - 点击“打开图片”或“打开摄像头”进行检测
- 结果实时显示在界面中,支持导出 Excel 报告