当前位置: 首页 > news >正文

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

基于yolov5的森林火灾识别系统,基于深度学习的森林火灾检测系统,森林火灾识别系统。森林火灾识别
以下文字及代码仅供参考。

文章目录

      • 1. 环境搭建
        • 安装依赖
      • 2. 数据准备
        • 数据集
        • 数据划分
      • 3. 模型训练
        • 配置文件
        • 训练命令
      • 4. 模型推理
        • 推理代码
      • 5. PyQt5 GUI
        • 创建GUI界面
      • 6. 性能评估

项目构建基础:
模型:YOLOV5
软件:Pycharm+Anaconda
环境:python=3.10 opencv_python PyQt5

1

预实现功能:

系统实现了对于多种火灾的识别检测功能:包括通过选择图片、视频、摄像头、文件夹图片文件进行实时识别;检测速度快、识别精度较高。
①选择图片识别森林火灾。
②选择视频识别森林火灾。
③摄像头检测识别森林火灾。
文件夹图片文件识别森林火灾

基于YOLOv5的森林火灾识别系统是一个非常实用的项目,可以实时检测和识别森林火灾。以下是构建这个系统的详细步骤,包括数据准备、环境搭建、模型训练、模型推理等。

1. 环境搭建

安装依赖

确保安装了以下依赖:

conda create-nfire_detectionpython=3.10conda activate fire_detection pipinstallopencv-python pyqt5 yolov5

2. 数据准备

数据集
  • 数据集:包含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_labels=train_test_split(images,labels,test_size=0.2,random_state=42)# 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_dir='data/images'label_dir='data/labels'output_dir='data/splits'split_data(image_dir,label_dir,output_dir)

3. 模型训练

配置文件

创建一个配置文件fire_detection.yaml

train: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_detection

4. 模型推理

推理代码

创建一个Python脚本detect_fire.py进行推理:

importtorchimportcv2fromyolov5.utils.generalimportnon_max_suppression,scale_coordsfromyolov5.utils.torch_utilsimportselect_devicedefdetect_fire(image_path,model_path='runs/train/fire_detection/weights/best.pt'):device=select_device('')model=torch.load(model_path)['model'].float().to(device).eval()image=cv2.imread(image_path)image_rgb=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)image_tensor=torch.from_numpy(image_rgb).permute(2,0,1).unsqueeze(0).float()/255.0image_tensor=image_tensor.to(device)withtorch.no_grad():pred=model(image_tensor)[0]pred=non_max_suppression(pred,0.4,0.5)for*xyxy,conf,clsinreversed(pred[0]):x1,y1,x2,y2=map(int,xyxy)cv2.rectangle(image,(x1,y1),(x2,y2),(0,255,0),2)cv2.putText(image,f'Fire:{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_path='path/to/image.jpg'detect_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')layout=QVBoxLayout()self.image_label=QLabel(self)layout.addWidget(self.image_label)self.source_combo=QComboBox(self)self.source_combo.addItem('Image')self.source_combo.addItem('Video')self.source_combo.addItem('Camera')layout.addWidget(self.source_combo)self.browse_button=QPushButton('Browse',self)self.browse_button.clicked.connect(self.browse_file)layout.addWidget(self.browse_button)self.detect_button=QPushButton('Detect',self)self.detect_button.clicked.connect(self.detect_fire)layout.addWidget(self.detect_button)self.setLayout(layout)defbrowse_file(self):file_dialog=QFileDialog()file_dialog.setNameFilter("Images (*.jpg *.jpeg *.png)")ifself.source_combo.currentText()=='Video':file_dialog.setNameFilter("Videos (*.mp4 *.avi *.mov)")iffile_dialog.exec_():self.file_path=file_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):image=cv2.imread(self.file_path)image_rgb=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)qimage=QImage(image_rgb.data,image_rgb.shape[1],image_rgb.shape[0],QImage.Format_RGB888)pixmap=QPixmap.fromImage(qimage)self.image_label.setPixmap(pixmap)defdetect_video(self):cap=cv2.VideoCapture(self.file_path)whileTrue:ret,frame=cap.read()ifnotret:breakframe_rgb=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)qimage=QImage(frame_rgb.data,frame_rgb.shape[1],frame_rgb.shape[0],QImage.Format_RGB888)pixmap=QPixmap.fromImage(qimage)self.image_label.setPixmap(pixmap)self.update()defdetect_camera(self):cap=cv2.VideoCapture(0)whileTrue:ret,frame=cap.read()ifnotret:breakframe_rgb=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)qimage=QImage(frame_rgb.data,frame_rgb.shape[1],frame_rgb.shape[0],QImage.Format_RGB888)pixmap=QPixmap.fromImage(qimage)self.image_label.setPixmap(pixmap)self.update()if__name__=='__main__':app=QApplication(sys.argv)ex=FireDetectionApp()ex.show()sys.exit(app.exec_())

6. 性能评估

使用标准的性能评估指标如准确率、召回率、F1分数等进行模型评估。

fromsklearn.metricsimportaccuracy_score,precision_score,recall_score,f1_scoredefevaluate_model(y_true,y_pred):accuracy=accuracy_score(y_true,y_pred)precision=precision_score(y_true,y_pred)recall=recall_score(y_true,y_pred)f1=f1_score(y_true,y_pred)print(f'Accuracy:{accuracy:.2f}')print(f'Precision:{precision:.2f}')print(f'Recall:{recall:.2f}')print(f'F1 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)

仅供参考,学习

http://www.gsyq.cn/news/1533590.html

相关文章:

  • 2026年佛山注册公司服务商怎么选?多维度对比本地外资企业执照注册与电商执照代办机构 - 优质品牌商家
  • 计算机毕业设计之西华花园家教管理系统
  • 中卫市黄金回收白银回收铂金回收彩金回收店铺哪家靠谱?2026实测五家诚信优选实体门店及电话地址推荐 - 盛世金银回收
  • GPT-5.5不存在?揭秘2024真实可用的大模型能力图谱
  • 昭通市黄金回收白银回收铂金回收彩金回收店铺排行榜 2026实测五家诚信优选实体门店及电话地址推荐 - 大熊猫898989
  • Win8.1笔记本详尽装机攻略:驱动注入与BIOS适配实战
  • 2026年市场洞察:浙江熏蒸托盘采购指南,5家供应商深度评测与真实案例参考 - 优质品牌商家
  • 收藏!升学季选专业不踩坑:网络安全等10类长期有前途的专业方向指南
  • MatrixVB:VB6时代的MATLAB式矩阵计算与可视化插件
  • Gemini 3.1 Pro多模态工程落地实战:ROI裁剪与Token精算
  • ROC曲线与AUC深度解析:从阈值扫描到业务决策的工程实践
  • Ubuntu下OBS Studio安装与硬件编码配置实战指南
  • 收藏!想入行金融网络安全?这个专业的培养_课程_就业全梳理
  • Visio 2019合法替代方案与专业绘图技巧全解析
  • 抖音下载神器:如何轻松批量保存你喜欢的短视频内容?
  • 3步掌握Microsoft Foundry Toolkit:在VS Code中构建AI应用的完整指南
  • 跟着 MDN 学 React 框架 Day 3:React 入门——核心概念与第一个应用
  • BERTicelli:下一代社交媒体安全防护的智能语义引擎
  • 字节面试官皱眉:“你这 Agent 跟带搜索的 ChatGPT 有啥区别?“我答:“能多轮搜,搜完接着搜啊“,他追问了一句搜索词……
  • 永城奔驰宝马奥迪保养多少钱 2026年较新行情参考 - 品牌排行榜
  • 南平市黄金回收白银回收铂金回收彩金回收店铺哪家靠谱?2026实测五家诚信优选实体门店及电话地址推荐 - 盛世金银回收
  • 2026年豪华墓碑公司哪家强?从石雕工艺到售后体系,这4家企业值得关注 - 优质品牌商家
  • EZCard卡牌批量生成器:桌游设计师的3步自动化解决方案
  • 开封市黄金回收白银回收铂金回收彩金回收店铺哪家靠谱?2026实测五家诚信优选实体门店及电话地址推荐 - 盛世金银回收
  • 柳州市黄金回收白银回收铂金回收彩金回收店铺排行榜 2026实测五家诚信优选实体门店及电话地址推荐 - 大熊猫898989
  • 温州市黄金回收白银回收铂金回收彩金回收店铺排行榜 2026实测五家诚信优选实体门店及电话地址推荐 - 大熊猫898989
  • 如何利用Tennis-Refactoring-Kata快速提升团队代码重构能力:完整实施指南
  • 如何实现微信聊天记录永久保存?WeChatMsg完整指南助你掌控个人数据
  • 轻量级安全扫描器lqsocan:从异步探测到CI/CD集成的DevSecOps实践
  • 吉安市黄金回收白银回收铂金回收彩金回收店铺排行榜 2026实测五家诚信优选实体门店及电话地址推荐 - 大熊猫898989