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
iouNMS 阈值(推理用)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
类别mAP50mAP50-95
fire0.6170.408
smoke0.5340.337
other0.4020.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)

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


更多推荐