yolo目标检测技术之yolov11项目实战(三)


一、 基于 YOLO11 的火焰与烟雾检测系统(实战+代码)

使用 YOLO11 实现实时火焰与烟雾检测,支持图像/视频/摄像头/RTSP 流输入,适合部署与演示。


项目目标

功能 支持内容
模型 YOLO11(支持检测/分割)
输入 图像、视频、摄像头、RTSP
输出 火焰/烟雾检测框、置信度
部署方式 Python + PyTorch + OpenCV
可视化界面 可扩展(如 Streamlit/Gradio)

环境搭建

创建虚拟环境

conda create -n yolov11_env python=3.12
conda activate yolov11_env

安装依赖

# CUDA 12.1(根据你显卡驱动选择)
pip install torch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cu121

# 安装 ultralytics(YOLO11)
pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple

1.1 数据集准备

1. 下载地址

2. 数据格式转换(XML → TXT)

YOLO 格式要求:class_id x_center y_center width height(归一化)

import os
import xml.etree.ElementTree as ET

classes = ['_background_', 'fire', 'other', 'smoke']

def parse_xml(xml_path, txt_path):
    for xml_file in os.listdir(xml_path):
        file_name = xml_file.split('.')[0]
        tree = ET.parse(os.path.join(xml_path, xml_file))
        root = tree.getroot()

        size = root.find('size')
        w = int(size.find('width').text)
        h = int(size.find('height').text)

        with open(os.path.join(txt_path, file_name + '.txt'), 'w') as f:
            for obj in root.findall('object'):
                cls = obj.find('name').text
                cls_id = classes.index(cls)
                bndbox = obj.find('bndbox')
                xmin = float(bndbox.find('xmin').text)
                ymin = float(bndbox.find('ymin').text)
                xmax = float(bndbox.find('xmax').text)
                ymax = float(bndbox.find('ymax').text)

                x_center = ((xmin + xmax) / 2) / w
                y_center = ((ymin + ymax) / 2) / h
                width = (xmax - xmin) / w
                height = (ymax - ymin) / h

                f.write(f"{cls_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n")

# 使用示例
if __name__ == '__main__':
    parse_xml('datasets/fire/Annotations', 'datasets/fire/labels')

1.2 模型训练

方法1:命令行训练

yolo detect train data=ultralytics/cfg/datasets/fire.yaml model=yolo11s.pt epochs=10 imgsz=640 batch=8 device=0 project=runs

方法2:Python 脚本训练(推荐)

from ultralytics import YOLO

if __name__ == "__main__":
    model = YOLO("yolo11s.pt")  # 加载预训练模型
    model.train(
        data="ultralytics/cfg/datasets/fire.yaml",
        epochs=10,
        batch=8,
        imgsz=640,
        device='0',
        project='runs'
    )

1.3 模型推理

方法1:命令行推理

yolo predict model=runs/train/weights/best.pt source=datasets/fire/test.jpg save=True

方法2:Python 脚本推理(推荐)

from ultralytics import YOLO

model = YOLO("runs/train/weights/best.pt")
results = model.predict(
    source="datasets/fire/test.jpg",
    save=True,
    conf=0.5,
    iou=0.6
)

1.4 常用参数对照表

参数名 含义说明 示例值
data 数据集配置文件路径 fire.yaml
model 模型权重路径或结构路径 yolo11s.pt
epochs 训练轮数 10
batch 每批次图像数量 8
imgsz 输入图像尺寸 640 / 1280
conf 置信度阈值(推理用) 0.5
iou NMS 阈值(推理用) 0.6
device 训练/推理设备 0 / cpu
save 是否保存推理结果 True

1.5 性能验证

yolo val model=runs/train/weights/best.pt data=ultralytics/cfg/datasets/fire.yaml conf=0.5 iou=0.6
类别 mAP50 mAP50-95
fire 0.617 0.408
smoke 0.534 0.337
other 0.402 0.257

1.6 模型优化建议

优化方式 操作建议
提高输入分辨率 imgsz=1280
数据增强 mixup=0.36, copy_paste=0.33
使用更大模型 yolo11m.pt / yolo11l.pt
调整阈值 conf=0.3~0.6, iou=0.5~0.7
加入注意力机制 可尝试 C2PSA 模块

1.7 项目结构建议

ultralytics/
├── datasets/
│   └── fire/
│       ├── images/train
│       ├── images/val
│       ├── labels/train
│       └── labels/val
├── runs/
│   └── train/
│       └── weights/
│           ├── best.pt
│           └── last.pt
└── my_train.py / my_detect.py

结语

本项目适合作为 YOLO11 入门实战项目,覆盖从数据准备、训练、推理到部署的完整流程。后续可扩展为:

  • Web 检测界面(Gradio/Streamlit)
  • RTSP 实时流检测
  • 边缘设备部署(Jetson Nano)

如需完整源码或部署脚本,欢迎留言或私信交流!


Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐