基于yolov5的森林火灾识别系统,基于深度学习的森林火灾检测系统,森林火灾识别系统。

发布时间:2026/6/16 6:45:51

基于yolov5的森林火灾识别系统,基于深度学习的森林火灾检测系统,森林火灾识别系统。 基于yolov5的森林火灾识别系统基于深度学习的森林火灾检测系统森林火灾识别系统。森林火灾识别以下文字及代码仅供参考。文章目录1. 环境搭建安装依赖2. 数据准备数据集数据划分3. 模型训练配置文件训练命令4. 模型推理推理代码5. PyQt5 GUI创建GUI界面6. 性能评估项目构建基础模型YOLOV5软件PycharmAnaconda环境python3.10 opencv_python PyQt51预实现功能系统实现了对于多种火灾的识别检测功能包括通过选择图片、视频、摄像头、文件夹图片文件进行实时识别检测速度快、识别精度较高。①选择图片识别森林火灾。②选择视频识别森林火灾。③摄像头检测识别森林火灾。文件夹图片文件识别森林火灾基于YOLOv5的森林火灾识别系统是一个非常实用的项目可以实时检测和识别森林火灾。以下是构建这个系统的详细步骤包括数据准备、环境搭建、模型训练、模型推理等。1. 环境搭建安装依赖确保安装了以下依赖conda create-nfire_detectionpython3.10conda activate fire_detection pipinstallopencv-python pyqt5 yolov52. 数据准备数据集数据集包含2000张图片的数据集每张图片都有标注。格式转换将标注文件转换为YOLOv5所需的格式.txt文件。数据划分使用train_test_split将数据集划分为训练集和验证集。importosimportrandomfromsklearn.model_selectionimporttrain_test_splitdefsplit_data(image_dir,label_dir,output_dir):images[fforfinos.listdir(image_dir)iff.endswith(.jpg)]labels[fforfinos.listdir(label_dir)iff.endswith(.txt)]# Ensure the same order for images and labelsimages.sort()labels.sort()# Split into train and test setstrain_images,val_images,train_labels,val_labelstrain_test_split(images,labels,test_size0.2,random_state42)# Save the splitswithopen(os.path.join(output_dir,train.txt),w)asf:forimg,lblinzip(train_images,train_labels):f.write(f{img}{lbl}\n)withopen(os.path.join(output_dir,val.txt),w)asf:forimg,lblinzip(val_images,val_labels):f.write(f{img}{lbl}\n)# Example usageimage_dirdata/imageslabel_dirdata/labelsoutput_dirdata/splitssplit_data(image_dir,label_dir,output_dir)3. 模型训练配置文件创建一个配置文件fire_detection.yamltrain:data/train/images/val:data/val/images/test:data/test/images/nc:1# number of classesnames:[fire]训练命令使用以下命令进行训练python yolov5/train.py--img640--batch16--epochs100--datafire_detection.yaml--weightsyolov5s.pt--namefire_detection4. 模型推理推理代码创建一个Python脚本detect_fire.py进行推理importtorchimportcv2fromyolov5.utils.generalimportnon_max_suppression,scale_coordsfromyolov5.utils.torch_utilsimportselect_devicedefdetect_fire(image_path,model_pathruns/train/fire_detection/weights/best.pt):deviceselect_device()modeltorch.load(model_path)[model].float().to(device).eval()imagecv2.imread(image_path)image_rgbcv2.cvtColor(image,cv2.COLOR_BGR2RGB)image_tensortorch.from_numpy(image_rgb).permute(2,0,1).unsqueeze(0).float()/255.0image_tensorimage_tensor.to(device)withtorch.no_grad():predmodel(image_tensor)[0]prednon_max_suppression(pred,0.4,0.5)for*xyxy,conf,clsinreversed(pred[0]):x1,y1,x2,y2map(int,xyxy)cv2.rectangle(image,(x1,y1),(x2,y2),(0,255,0),2)cv2.putText(image,fFire:{conf:.2f},(x1,y1-10),cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)cv2.imshow(Fire Detection,image)cv2.waitKey(0)cv2.destroyAllWindows()if__name____main__:image_pathpath/to/image.jpgdetect_fire(image_path)5. PyQt5 GUI创建GUI界面创建一个PyQt5界面用于选择图片、视频或摄像头进行实时检测importsysfromPyQt5.QtWidgetsimportQApplication,QWidget,QVBoxLayout,QPushButton,QLabel,QFileDialog,QComboBox,QLineEditfromPyQt5.QtGuiimportQImage,QPixmapfromPyQt5.QtCoreimportQTimerimportcv2importnumpyasnpclassFireDetectionApp(QWidget):def__init__(self):super().__init__()self.initUI()definitUI(self):self.setWindowTitle(Forest Fire Detection)layoutQVBoxLayout()self.image_labelQLabel(self)layout.addWidget(self.image_label)self.source_comboQComboBox(self)self.source_combo.addItem(Image)self.source_combo.addItem(Video)self.source_combo.addItem(Camera)layout.addWidget(self.source_combo)self.browse_buttonQPushButton(Browse,self)self.browse_button.clicked.connect(self.browse_file)layout.addWidget(self.browse_button)self.detect_buttonQPushButton(Detect,self)self.detect_button.clicked.connect(self.detect_fire)layout.addWidget(self.detect_button)self.setLayout(layout)defbrowse_file(self):file_dialogQFileDialog()file_dialog.setNameFilter(Images (*.jpg *.jpeg *.png))ifself.source_combo.currentText()Video:file_dialog.setNameFilter(Videos (*.mp4 *.avi *.mov))iffile_dialog.exec_():self.file_pathfile_dialog.selectedFiles()[0]defdetect_fire(self):ifself.source_combo.currentText()Image:self.detect_image()elifself.source_combo.currentText()Video:self.detect_video()elifself.source_combo.currentText()Camera:self.detect_camera()defdetect_image(self):imagecv2.imread(self.file_path)image_rgbcv2.cvtColor(image,cv2.COLOR_BGR2RGB)qimageQImage(image_rgb.data,image_rgb.shape[1],image_rgb.shape[0],QImage.Format_RGB888)pixmapQPixmap.fromImage(qimage)self.image_label.setPixmap(pixmap)defdetect_video(self):capcv2.VideoCapture(self.file_path)whileTrue:ret,framecap.read()ifnotret:breakframe_rgbcv2.cvtColor(frame,cv2.COLOR_BGR2RGB)qimageQImage(frame_rgb.data,frame_rgb.shape[1],frame_rgb.shape[0],QImage.Format_RGB888)pixmapQPixmap.fromImage(qimage)self.image_label.setPixmap(pixmap)self.update()defdetect_camera(self):capcv2.VideoCapture(0)whileTrue:ret,framecap.read()ifnotret:breakframe_rgbcv2.cvtColor(frame,cv2.COLOR_BGR2RGB)qimageQImage(frame_rgb.data,frame_rgb.shape[1],frame_rgb.shape[0],QImage.Format_RGB888)pixmapQPixmap.fromImage(qimage)self.image_label.setPixmap(pixmap)self.update()if__name____main__:appQApplication(sys.argv)exFireDetectionApp()ex.show()sys.exit(app.exec_())6. 性能评估使用标准的性能评估指标如准确率、召回率、F1分数等进行模型评估。fromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_scoredefevaluate_model(y_true,y_pred):accuracyaccuracy_score(y_true,y_pred)precisionprecision_score(y_true,y_pred)recallrecall_score(y_true,y_pred)f1f1_score(y_true,y_pred)print(fAccuracy:{accuracy:.2f})print(fPrecision:{precision:.2f})print(fRecall:{recall:.2f})print(fF1 Score:{f1:.2f})# Example usagey_true[0,1,0,1,1,0,1,0,1,1]y_pred[0,1,0,1,1,0,1,0,1,1]evaluate_model(y_true,y_pred)仅供参考学习

相关新闻