首页 / 资讯中心 / 文章详情

变电站绝缘套管红外检测数据集 AI变压器的热缺陷 变电站温度过热识别 构建变电站绝缘套管红外检测系统

变电站绝缘套管红外检测数据集 AI变压器的热缺陷 变电站温度过热识别 构建变电站绝缘套管红外检测系统 ★ FEATURED ARTICLE
使用深度学习目标检测框架训练使用变电站绝缘套管红外检测数据集变电站绝缘套管红外检测数据集 1分类包含变压器热缺陷。共计图片10000张。YOLO的txt格式。附数据增强代码数据集介绍变压器是变电站的心脏中枢在运行过程中要求工作必须可靠一旦出现故障轻则造成设备损坏重则引发火情危及正常的站内安全因此必须及时且有效地对变压器进行热缺陷检测以防止安全事故发生。 在变压器出现故障的前夕都伴随着自身温度的升高而红外热成像是发现变压器热缺陷的检测技术为变压器的热缺陷、设备状态、安全运行监测提供红外安全检测。使用深度学习目标检测框架训练使用变电站绝缘套管红外检测数据集 识别变压器的热缺陷 温度过热变电站绝缘套管红外检测数据集并且标注文件已经转换为YOLO适用的TXT格式我们可以直接进行模型训练、评估以及可视化。以下是详细的步骤以下仅供参考环境准备安装必要的库。数据集组织确认数据集的结构和路径。数据增强如果需要。模型定义与训练使用YOLOv8进行目标检测。评估与可视化评估模型性能。可视化结果。环境准备首先我们需要安装必要的库。您可以使用以下命令来设置环境pipinstallultralytics opencv-python-headless pandas scikit-learn albumentations数据集组织假设您的数据集已经按照YOLO格式组织好并且包含以下文件夹结构datasets/substation_insulators/ ├── 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配置文件substation_insulators.yaml来描述数据集。以下是示例配置文件[titleYOLOv8 Configuration File for Substation Insulator Defects Detection]train:../datasets/substation_insulators/images/trainval:../datasets/substation_insulators/images/valtest:../datasets/substation_insulators/images/testnc:1names:[变压器热缺陷]将上述内容保存为datasets/substation_insulators/substation_insulators.yaml。分割数据集为训练集、验证集和测试集我们将使用Python脚本来按8:1:1的比例分割数据集。以下是分割数据集的脚本split_dataset.py:[titleSplit Dataset into Train, Validation, and Test Sets]importosimportrandomfromsklearn.model_selectionimporttrain_test_splitfrompathlibimportPath# Define pathsbase_pathPath(datasets/substation_insulators)images_dirbase_path/JPEGImagesannotations_dirbase_path/labelstrain_images_dirbase_path/images/traintrain_labels_dirbase_path/labels/trainval_images_dirbase_path/images/valval_labels_dirbase_path/labels/valtest_images_dirbase_path/images/testtest_labels_dirbase_path/labels/test# Create directories if they dont existos.makedirs(train_images_dir,exist_okTrue)os.makedirs(train_labels_dir,exist_okTrue)os.makedirs(val_images_dir,exist_okTrue)os.makedirs(val_labels_dir,exist_okTrue)os.makedirs(test_images_dir,exist_okTrue)os.makedirs(test_labels_dir,exist_okTrue)# List all image filesimage_fileslist(images_dir.glob(*.jpg))# Adjust extension if necessary# Shuffle the image filesrandom.shuffle(image_files)# Split ratiostrain_ratio0.8val_ratio0.1test_ratio0.1# Calculate split indicesnum_imageslen(image_files)train_splitint(num_images*train_ratio)val_splitint(num_images*(train_ratioval_ratio))# Split images and labelstrain_imagesimage_files[:train_split]val_imagesimage_files[train_split:val_split]test_imagesimage_files[val_split:]defcopy_files(source_images,dest_images_dir,dest_labels_dir):forimg_fileinsource_images:label_fileannotations_dir/(img_file.stem.txt)iflabel_file.exists():os.symlink(img_file,dest_images_dir/img_file.name)os.symlink(label_file,dest_labels_dir/label_file.name)copy_files(train_images,train_images_dir,train_labels_dir)copy_files(val_images,val_images_dir,val_labels_dir)copy_files(test_images,test_images_dir,test_labels_dir)print(Dataset splitting completed.)数据增强为了提高模型的泛化能力我们可以使用数据增强技术。以下是数据增强的脚本data_augmentation.py:[titleData Augmentation Script using Albumentations]importcv2importnumpyasnpimportosfrompathlibimportPathfromalbumentationsimport(HorizontalFlip,VerticalFlip,RandomBrightnessContrast,ShiftScaleRotate,CLAHE,RandomGamma,HueSaturationValue,Compose,BboxParams)# Define pathsbase_pathPath(datasets/substation_insulators)images_dirbase_path/JPEGImagesannotations_dirbase_path/labelsaugmented_images_dirbase_path/augmented_imagesaugmented_labels_dirbase_path/augmented_labels# Create directories if they dont existos.makedirs(augmented_images_dir,exist_okTrue)os.makedirs(augmented_labels_dir,exist_okTrue)# Define augmentation transformationstransformCompose([HorizontalFlip(p0.5),VerticalFlip(p0.5),ShiftScaleRotate(shift_limit0.0625,scale_limit0.2,rotate_limit45,p0.5),RandomBrightnessContrast(brightness_limit0.2,contrast_limit0.2,p0.5),RandomGamma(gamma_limit(80,120),p0.5),HueSaturationValue(hue_shift_limit20,sat_shift_limit30,val_shift_limit20,p0.5),CLAHE(clip_limit4.0,tile_grid_size(8,8),p0.5)],bbox_paramsBboxParams(formatyolo,min_visibility0.3,label_fields[class_labels]))defread_label(file_path):withopen(file_path,r)asf:linesf.readlines()bboxes[]class_labels[]forlineinlines:partsline.strip().split()class_labels.append(int(parts[0]))bboxes.append([float(x)forxinparts[1:]])returnbboxes,class_labelsdefwrite_label(file_path,bboxes,class_labels):withopen(file_path,w)asf:forcls,bboxinzip(class_labels,bboxes):f.write(f{cls}{ .join(map(str,bbox))}\n)defaugment_image(image,bboxes,class_labels):transformedtransform(imageimage,bboxesbboxes,class_labelsclass_labels)augmented_imagetransformed[image]augmented_bboxestransformed[bboxes]augmented_class_labelstransformed[class_labels]returnaugmented_image,augmented_bboxes,augmented_class_labels# Process each image and its corresponding labelforimage_fileinimages_dir.glob(*.jpg):label_fileannotations_dir/(image_file.stem.txt)ifnotlabel_file.exists():continueimagecv2.imread(str(image_file))bboxes,class_labelsread_label(label_file)augmented_image,augmented_bboxes,augmented_class_labelsaugment_image(image,bboxes,class_labels)new_image_pathaugmented_images_dir/(image_file.stem_aug.jpg)new_label_pathaugmented_labels_dir/(label_file.stem_aug.txt)cv2.imwrite(str(new_image_path),augmented_image)write_label(new_label_path,augmented_bboxes,augmented_class_labels)print(Data augmentation completed.)模型定义与训练我们将使用YOLOv8进行目标检测。以下是训练脚本train_detection.py:[titleTraining Script for Substation Insulator Defects Detection using YOLOv8]fromultralyticsimportYOLO# Load a modelmodelYOLO(yolov8n.pt)# load a pretrained model (recommended for training)# Train the modelresultsmodel.train(data../datasets/substation_insulators/substation_insulators.yaml,epochs50,imgsz640,batch16,project../runs/train,namesubstation_insulator_defects_detection)# Evaluate the modelmetricsmodel.val()resultsmodel.export(formatonnx)# export the trained model to ONNX format评估与可视化使用YOLOv8自带的评估脚本来评估目标检测模型。[titleEvaluation Script for Substation Insulator Defects Detection using YOLOv8]fromultralyticsimportYOLO# Load the best modelbest_modelYOLO(../runs/train/substation_insulator_defects_detection/weights/best.pt)# Evaluate the model on the validation datasetmetricsbest_model.val(data../datasets/substation_insulators/substation_insulators.yaml,conf0.5,iou0.45)print(metrics)用户界面我们将使用 PyQt5 创建一个简单的 GUI 来加载和运行模型进行实时预测。以下是用户界面脚本ui.py:[titlePyQt5 Main Window for Substation Insulator Defects Detection]importsysimportcv2importnumpyasnpfromPyQt5.QtWidgetsimportQApplication,QMainWindow,QLabel,QPushButton,QVBoxLayout,QWidget,QFileDialogfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQt,QTimerfromultralyticsimportYOLO# Load modeldetection_modelYOLO(../runs/train/substation_insulator_defects_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_widgetQWidget()self.setCentralWidget(self.central_widget)self.layoutQVBoxLayout()self.image_labelQLabel(self)self.image_label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.image_label)self.load_image_buttonQPushButton(加载图像,self)self.load_image_button.clicked.connect(self.load_image)self.layout.addWidget(self.load_image_button)self.start_prediction_buttonQPushButton(开始预测,self)self.start_prediction_button.clicked.connect(self.start_prediction)self.layout.addWidget(self.start_prediction_button)self.stop_prediction_buttonQPushButton(停止预测,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_pathNoneself.timerQTimer()self.timer.timeout.connect(self.update_frame)defload_image(self):optionsQFileDialog.Options()file_name,_QFileDialog.getOpenFileName(self,选择图像文件,,Images (*.png *.jpg *.jpeg);;All Files (*),optionsoptions)iffile_name:self.image_pathfile_name self.display_image(file_name)defdisplay_image(self,path):pixmapQPixmap(path)scaled_pixmappixmap.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_imagecv2.imread(self.image_path)image_rgbcv2.cvtColor(original_image,cv2.COLOR_BGR2RGB)# Detectionresultsdetection_model.predict(image_rgb,size640,conf0.5,iou0.45)[0]forboxinresults.boxes.cpu().numpy():rbox.xyxy[0].astype(int)clsint(box.cls[0])confbox.conf[0]# Map class ID to nameclass_names[变压器热缺陷]class_nameclass_names[cls]# Draw bounding boxcv2.rectangle(image_rgb,(r[0],r[1]),(r[2],r[3]),(0,255,0),2)# Put textfontcv2.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,chimage_rgb.shape bytes_per_linech*w qt_imageQImage(image_rgb.data,w,h,bytes_per_line,QImage.Format_RGB888)pixmapQPixmap.fromImage(qt_image)scaled_pixmappixmap.scaled(self.image_label.width(),self.image_label.height(),Qt.KeepAspectRatio)self.image_label.setPixmap(scaled_pixmap)if__name____main__:appQApplication(sys.argv)windowMainWindow()window.show()sys.exit(app.exec_())请确保将路径替换为您实际的路径。使用说明配置路径确保datasets/substation_insulators目录结构正确并且包含images和labels子目录。确保runs/train/substation_insulator_defects_detection/weights/best.pt是训练好的 YOLOv8 模型权重路径。运行脚本在终端中运行split_dataset.py脚本来分割数据集。在终端中运行data_augmentation.py脚本来进行数据增强。在终端中运行train_detection.py脚本来训练目标检测模型。在终端中运行evaluate_detection.py来评估目标检测模型性能。在终端中运行ui.py来启动 GUI 应用程序。注意事项确保所有必要的工具箱已安装特别是ultralytics和PyQt5。根据需要调整参数如epochs和batch_size。示例假设您的数据文件夹结构如下datasets/ └── substation_insulators/ ├── 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后您可以点击按钮来加载图像并进行变电站绝缘套管红外检测。总结通过上述步骤我们可以构建一个全面的变电站绝缘套管红外检测系统包括数据集准备、环境部署、模型定义、训练、评估、结果分析、可视化以及用户界面开发。以下是所有相关的代码文件分割数据集脚本(split_dataset.py)数据增强脚本(data_augmentation.py)目标检测训练脚本(train_detection.py)YOLOv8 配置文件(substation_insulators.yaml)目标检测评估脚本(evaluate_detection.py)用户界面(ui.py)
阅读完成 · 觉得有帮助?
咨询建站