口罩检测和识别2:基于深度学习YOLO26神经网络实现口罩检测和识别(含训练代码和数据集)

基于深度学习YOLO26神经网络实现口罩检测和识别,其能识别检测出3种口罩检测:names: 'R_mask', 'W_mask', 'N_mask'

具体图片见如下:

第一步:YOLO26介绍

YOLO26采用了端到端无NMS推理,直接生成预测结果,无需非极大值抑制(NMS)后处理。这种设计减少了延迟,简化了集成,并提高了部署效率。此外,YOLO26移除了分布焦点损失(DFL),从而增强了硬件兼容性,特别是在边缘设备上的表现。

模型还引入了ProgLoss 和小目标感知标签分配(STAL) ,显著提升了小目标检测的精度。这对于物联网、机器人技术和航空影像等应用至关重要。同时,YOLO26采用了全新的MuSGD优化器,结合了SGD和Muon优化技术,提供更稳定的训练和更快的收敛速度。

第二步:YOLO26网络结构

​

第三步:代码展示

python 复制代码
# Ultralytics YOLO 🚀, AGPL-3.0 license

from pathlib import Path

from ultralytics.engine.model import Model
from ultralytics.models import yolo
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, OBBModel, PoseModel, SegmentationModel, WorldModel
from ultralytics.utils import ROOT, yaml_load


class YOLO(Model):
    """YOLO (You Only Look Once) object detection model."""

    def __init__(self, model="yolo11n.pt", task=None, verbose=False):
        """Initialize YOLO model, switching to YOLOWorld if model filename contains '-world'."""
        path = Path(model)
        if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}:  # if YOLOWorld PyTorch model
            new_instance = YOLOWorld(path, verbose=verbose)
            self.__class__ = type(new_instance)
            self.__dict__ = new_instance.__dict__
        else:
            # Continue with default YOLO initialization
            super().__init__(model=model, task=task, verbose=verbose)

    @property
    def task_map(self):
        """Map head to model, trainer, validator, and predictor classes."""
        return {
            "classify": {
                "model": ClassificationModel,
                "trainer": yolo.classify.ClassificationTrainer,
                "validator": yolo.classify.ClassificationValidator,
                "predictor": yolo.classify.ClassificationPredictor,
            },
            "detect": {
                "model": DetectionModel,
                "trainer": yolo.detect.DetectionTrainer,
                "validator": yolo.detect.DetectionValidator,
                "predictor": yolo.detect.DetectionPredictor,
            },
            "segment": {
                "model": SegmentationModel,
                "trainer": yolo.segment.SegmentationTrainer,
                "validator": yolo.segment.SegmentationValidator,
                "predictor": yolo.segment.SegmentationPredictor,
            },
            "pose": {
                "model": PoseModel,
                "trainer": yolo.pose.PoseTrainer,
                "validator": yolo.pose.PoseValidator,
                "predictor": yolo.pose.PosePredictor,
            },
            "obb": {
                "model": OBBModel,
                "trainer": yolo.obb.OBBTrainer,
                "validator": yolo.obb.OBBValidator,
                "predictor": yolo.obb.OBBPredictor,
            },
        }


class YOLOWorld(Model):
    """YOLO-World object detection model."""

    def __init__(self, model="yolov8s-world.pt", verbose=False) -> None:
        """
        Initialize YOLOv8-World model with a pre-trained model file.

        Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default
        COCO class names.

        Args:
            model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
            verbose (bool): If True, prints additional information during initialization.
        """
        super().__init__(model=model, task="detect", verbose=verbose)

        # Assign default COCO class names when there are no custom names
        if not hasattr(self.model, "names"):
            self.model.names = yaml_load(ROOT / "cfg/datasets/coco8.yaml").get("names")

    @property
    def task_map(self):
        """Map head to model, validator, and predictor classes."""
        return {
            "detect": {
                "model": WorldModel,
                "validator": yolo.detect.DetectionValidator,
                "predictor": yolo.detect.DetectionPredictor,
                "trainer": yolo.world.WorldTrainer,
            }
        }

    def set_classes(self, classes):
        """
        Set classes.

        Args:
            classes (List(str)): A list of categories i.e. ["person"].
        """
        self.model.set_classes(classes)
        # Remove background if it's given
        background = " "
        if background in classes:
            classes.remove(background)
        self.model.names = classes

        # Reset method class names
        # self.predictor = None  # reset predictor otherwise old names remain
        if self.predictor:
            self.predictor.model.names = classes

第四步:统计训练过程的一些指标,相关指标都有

​

第五步:运行预测代码

python 复制代码
#coding:utf-8
from ultralytics import YOLO
import cv2

# 所需加载的模型目录
path = 'models/best.pt'
# 需要检测的图片地址
img_path = "TestFiles/000353.jpg"

# 加载预训练模型
# conf	0.25	object confidence threshold for detection
# iou	0.7	intersection over union (IoU) threshold for NMS
model = YOLO(path, task='detect')
results = model.predict(img_path, iou=0.5)


# 检测图片
res = results[0].plot()
cv2.imshow("YOLO26 Detection", res)
cv2.waitKey(0)

​

第六步:整个工程的内容

包含数据集、训练代码和预测代码

​

项目完整文件下载请见演示与介绍视频的简介处给出:➷➷➷

https://www.bilibili.com/video/BV1QXMu6TEM5/

​

相关推荐
骑着蜗牛撵大象32716 分钟前
AgentScope 搭大脑、Spring AI 当手脚:企业智能体推理与执行分层落地
java·人工智能·spring·智能体·工具调用·agentscope·模型编排
会议咨询34 分钟前
2027年人工智能、航天与信号处理国际会议(AIASP 2027)
人工智能·信号处理·航空航天
老唐77737 分钟前
具身智能视觉感知详解:从图像识别到机器人环境理解,核心技术、算法架构与应用实践
人工智能·深度学习·算法·机器人·大模型·具身智能
天远Date Lab40 分钟前
零信任架构实战:基于天远运营商近3个月欠费次数构建自动化通信履约评估网关
运维·人工智能·架构·自动化
ESDWAN1 小时前
AI Agent兴起:从智能问答到企业工作流自动化
运维·人工智能·自动化
美团技术团队1 小时前
从代码生成到可信交付:美团 AIDE 数据研发 Agent 实践
人工智能
EatFan1 小时前
2026 后端 AI 集成选型拆解:Spring AI 2.0、MCP 协议与 Agent 内嵌三条路径的决策要点
java·人工智能·spring
一切皆是因缘际会1 小时前
学习 曲线
大数据·人工智能
慎思知行1 小时前
AI 外呼系统的工程落地要点:线路、意图识别与并发计费模型
人工智能·外呼系统·ai外呼·企业服务·ai获客·极光拓客
凡达Ai派1 小时前
Dify + n8n 联动实战:周报自动发到飞书,不用每天手动推
人工智能·飞书·课程设计·凡达ai·凡达ai画布