使用深度学习目标检测框架yolov8训练使用太阳能电池板航拍红外缺陷故障目标检测数据集来识别’热点’, ‘PID效应’, ‘阴影’, '串联故障’并构建基于深度学习的太阳能电池板红外缺陷检测系统![]()
如何训练光伏巡检太阳能电池板航拍红外缺陷故障目标检测数据集
如何训练光伏巡检太阳能电池板航拍红外缺陷故障目标检测数据集
2274 张 4类
names: [‘Hotspot’, ‘PID’, ‘Shadow’, ‘String’]
名称:[‘热点’, ‘PID效应’, ‘阴影’, ‘串联故障’]
8:1:1比例划分
train:1819张,val:227张,test:228张
标注文件为YOLO适用的txt格式。
可以直接用于yolo模型训练。如何训练光伏巡检太阳能电池板航拍红外缺陷故障目标检测数据集
如何使用Yolov8训练训练光伏巡检太阳能电池板航拍红外缺陷故障目标检测数据集
以下代码仅供参考!
光伏巡检太阳能电池板航拍红外缺陷故障目标检测数据集,并且标注文件已经转换为YOLO适用的TXT格式,我们可以直接进行模型训练、评估以及可视化。以下是详细的步骤:
环境准备:
- 安装必要的库。
数据集组织:
- 确认数据集的结构和路径。
模型定义与训练:
- 使用YOLOv8进行目标检测。
评估与可视化:
- 评估模型性能。
- 可视化结果。
环境准备
首先,我们需要安装必要的库。您可以使用以下命令来设置环境:
pipinstallultralytics opencv-python-headless pandas scikit-learn数据集组织
假设您的数据集已经按照YOLO格式组织好,并且包含以下文件夹结构:
datasets/solar_panel_inspection/ ├── images/ │ ├── train/ │ │ ├── image0.jpg │ │ ├── image1.jpg │ │ └── ... │ ├── val/ │ │ ├── image0.jpg │ │ ├── image1.jpg │ │ └── ... │ └── test/ │ ├── image0.jpg │ ├── image1.jpg │ └── ... └── labels/ ├── train/ │ ├── image0.txt │ ├── image1.txt │ └── ... ├── val/ │ ├── image0.txt │ ├── image1.txt │ └── ... └── test/ ├── image0.txt ├── image1.txt └── ...并且每个图像都有一个对应的TXT标签文件。
创建YAML配置文件
确保您有一个正确的YAML配置文件solar_panel_inspection.yaml来描述数据集。以下是示例配置文件:
[<title="YOLOv8 Configuration File for Solar Panel Inspection Object Detection">]train:../datasets/solar_panel_inspection/images/trainval:../datasets/solar_panel_inspection/images/valtest:../datasets/solar_panel_inspection/images/testnc:4names:['Hotspot','PID','Shadow','String']将上述内容保存为datasets/solar_panel_inspection/solar_panel_inspection.yaml。
模型定义与训练
我们将使用YOLOv8进行目标检测。以下是训练脚本train_detection.py:
[<title="Training Script for Solar Panel Inspection Object Detection using YOLOv8">]fromultralyticsimportYOLO# Load a modelmodel=YOLO('yolov8n.pt')# load a pretrained model (recommended for training)# Train the modelresults=model.train(data='../datasets/solar_panel_inspection/solar_panel_inspection.yaml',epochs=50,imgsz=640,batch=16,project='../runs/train',name='solar_panel_inspection_detection')# Evaluate the modelmetrics=model.val()results=model.export(format='onnx')# export the trained model to ONNX format评估与可视化
使用YOLOv8自带的评估脚本来评估目标检测模型。
[<title="Evaluation Script for Solar Panel Inspection Object Detection using YOLOv8">]fromultralyticsimportYOLO# Load the best modelbest_model=YOLO('../runs/train/solar_panel_inspection_detection/weights/best.pt')# Evaluate the model on the validation datasetmetrics=best_model.val(data='../datasets/solar_panel_inspection/solar_panel_inspection.yaml',conf=0.5,iou=0.45)print(metrics)用户界面
我们将使用 PyQt5 创建一个简单的 GUI 来加载和运行模型进行实时预测。以下是用户界面脚本ui.py:
[<title="PyQt5 Main Window for Solar Panel Inspection Object Detection">]importsysimportcv2importnumpyasnpfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QLabel,QPushButton,QVBoxLayout,QWidget,QFileDialogfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQt,QTimerfromultralyticsimportYOLO# Load modeldetection_model=YOLO('../runs/train/solar_panel_inspection_detection/weights/best.pt')classMainWindow(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle("光伏巡检图像缺陷检测系统")self.setGeometry(100,100,800,600)self.initUI()definitUI(self):self.central_widget=QWidget()self.setCentralWidget(self.central_widget)self.layout=QVBoxLayout()self.image_label=QLabel(self)self.image_label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.image_label)self.load_image_button=QPushButton("加载图像",self)self.load_image_button.clicked.connect(self.load_image)self.layout.addWidget(self.load_image_button)self.start_prediction_button=QPushButton("开始预测",self)self.start_prediction_button.clicked.connect(self.start_prediction)self.layout.addWidget(self.start_prediction_button)self.stop_prediction_button=QPushButton("停止预测",self)self.stop_prediction_button.clicked.connect(self.stop_prediction)self.layout.addWidget(self.stop_prediction_button)self.central_widget.setLayout(self.layout)self.image_path=Noneself.timer=QTimer()self.timer.timeout.connect(self.update_frame)defload_image(self):options=QFileDialog.Options()file_name,_=QFileDialog.getOpenFileName(self,"选择图像文件","","Images (*.png *.jpg *.jpeg);;All Files (*)",options=options)iffile_name:self.image_path=file_name self.display_image(file_name)defdisplay_image(self,path):pixmap=QPixmap(path)scaled_pixmap=pixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)defstart_prediction(self):ifself.image_pathisnotNoneandnotself.timer.isActive():self.timer.start(30)# Update frame every 30 msdefstop_prediction(self):ifself.timer.isActive():self.timer.stop()self.image_label.clear()defupdate_frame(self):original_image=cv2.imread(self.image_path)image_rgb=cv2.cvtColor(original_image,cv2.COLOR_BGR2RGB)# Detectionresults=detection_model.predict(image_rgb,size=640,conf=0.5,iou=0.45)[0]forboxinresults.boxes.cpu().numpy():r=box.xyxy[0].astype(int)cls=int(box.cls[0])conf=box.conf[0]# Map class ID to nameclass_names=['热点','PID效应','阴影','串联故障']class_name=class_names[cls]# Draw bounding boxcv2.rectangle(image_rgb,(r[0],r[1]),(r[2],r[3]),(0,255,0),2)# Put textfont=cv2.FONT_HERSHEY_SIMPLEX cv2.putText(image_rgb,f'{class_name}({conf:.2f})',(r[0],r[1]-10),font,0.9,(0,255,0),2)h,w,ch=image_rgb.shape bytes_per_line=ch*w qt_image=QImage(image_rgb.data,w,h,bytes_per_line,QImage.Format_RGB888)pixmap=QPixmap.fromImage(qt_image)scaled_pixmap=pixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)if__name__=="__main__":app=QApplication(sys.argv)window=MainWindow()window.show()sys.exit(app.exec_())请确保将路径替换为您实际的路径。
使用说明
配置路径:
- 确保
datasets/solar_panel_inspection目录结构正确,并且包含images和labels子目录。 - 确保
runs/train/solar_panel_inspection_detection/weights/best.pt是训练好的 YOLOv8 模型权重路径。
- 确保
运行脚本:
- 在终端中运行
train_detection.py脚本来训练目标检测模型。 - 在终端中运行
evaluate_detection.py来评估目标检测模型性能。 - 在终端中运行
ui.py来启动 GUI 应用程序。
- 在终端中运行
注意事项:
- 确保所有必要的工具箱已安装,特别是
ultralytics和PyQt5。 - 根据需要调整参数,如
epochs和batch_size。
- 确保所有必要的工具箱已安装,特别是
示例
假设您的数据文件夹结构如下:
datasets/ └── solar_panel_inspection/ ├── images/ │ ├── train/ │ │ ├── image0.jpg │ │ ├── image1.jpg │ │ └── ... │ ├── val/ │ │ ├── image0.jpg │ │ ├── image1.jpg │ │ └── ... │ └── test/ │ ├── image0.jpg │ ├── image1.jpg │ └── ... └── labels/ ├── train/ │ ├── image0.txt │ ├── image1.txt │ └── ... ├── val/ │ ├── image0.txt │ ├── image1.txt │ └── ... └── test/ ├── image0.txt ├── image1.txt └── ...并且每个图像都有一个对应的TXT标签文件。运行ui.py后,您可以点击按钮来加载图像并进行光伏巡检图像缺陷检测。