99网
您的当前位置:首页[C#][winform]基于yolov8的DMS驾驶员抽烟打电话喝水吃东西检测系统C#源码+onnx模型+评估指标曲线+精美GUI界面

[C#][winform]基于yolov8的DMS驾驶员抽烟打电话喝水吃东西检测系统C#源码+onnx模型+评估指标曲线+精美GUI界面

来源:99网

【重要说明】

该系统以opencvsharp作图像处理,onnxruntime做推理引擎,使用CPU进行推理,适合有显卡或者没有显卡windows x系统均可,不支持macOS和Linux系统,不支持x86的windows操作系统。由于采用CPU推理,要比GPU慢。为了适合大部分操作系统我们暂时只写了CPU推理源码,GPU推理源码后期根据需要可能会调整,目前只考虑CPU推理,主要是为了照顾现在大部分使用该源码是学生,很多人并没有显卡的电脑情况。

【算法介绍】

基于YOLOv8的DMS(驾驶员监控系统)驾驶员抽烟、打电话、喝水、吃东西检测系统是一种利用先进计算机视觉技术的实时监测系统。该系统通过YOLOv8算法,一种在速度和准确性上均表现优异的实时目标检测算法,实现对驾驶员行为的实时监测。

该系统能够自动接收视频或图像输入,通过单次神经网络前向传播,即可快速准确地检测并识别出驾驶员是否在抽烟、打电话、喝水或吃东西等分心行为。这些检测不仅基于图像中的物体识别,还结合了人脸和手部的位置关系、嘴部区域特征等度信息,以提高检测的准确性。

为了训练这一系统,研究人员构建了包含大量标注图像的数据集,这些图像覆盖了各种驾驶环境下的分心行为实例。通过深度学习和优化,系统能够在复杂环境中稳定工作,为驾驶安全提供有力保障。

此外,该系统还具备实时预警功能,当检测到分心行为时,会立即通过声音、震动或视觉警告提醒驾驶员,有效降低因分心驾驶导致的事故风险。该系统在交通安全监控领域具有广泛应用前景,有望成为未来智能车辆不可或缺的一部分。

【效果展示】

【测试环境】

windows10 x系统
VS2019
netframework4.7.2
opencvsharp4.8.0
onnxruntime1.16.3

【模型可以检测出类别】

Sleepy
Cigarette
Drinking
Phone
microsleep
HandsOnWheel
Eating
HandsNotOnWheel
Seatbelt

【训练数据集】

【训练信息】

参数
训练集图片数5168
验证集图片数575
训练map91.3%
训练精度(Precision)90.8%
训练召回率(Recall)84.8%

验证集map信息

Class

Images

Instances

P

R

mAP50

mAP50-95

all

575

6

0.901

0.848

0.913

0.665

Sleepy

133

133

0.938

0.97

0.985

0.729

Cigarette

77

77

0.914

0.714

0.846

0.523

Drinking

1

1

0.809

0.799

0.818

0.519

Phone

10

10

0.75

0.5

0.75

0.513

microsleep

45

45

0.

0.903

0.959

0.745

HandsOnWheel

24

24

1

0.951

0.979

0.74

Eating

54

54

0.98

0.963

0.993

0.776

HandsNotOnWheel

38

38

0.926

0.99

0.987

0.792

Seatbelt

100

101

0.9

0.842

0.8

0.9

【部分实现源码】

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace FIRC
{
    public partial class Form1 : Form
    {
 
        public bool videoStart = false;//视频停止标志
        string weightsPath = Application.StartupPath + "\\weights";//模型目录
        string labelTxt= Application.StartupPath + "\\weights\\class_names.txt";//类别文件
        Yolov8Manager detetor = new Yolov8Manager();//推理引擎
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;//线程更新控件不报错
        }
        private void LoadWeightsFromDir()
        {
            var di = new DirectoryInfo(weightsPath);
            foreach(var fi in di.GetFiles("*.onnx"))
            {
                comboBox1.Items.Add(fi.Name);
            }
            if(comboBox1.Items.Count>0)
            {
                comboBox1.SelectedIndex = 0;
            }
            else
            {
                tssl_show.Text = "未找到模型,请关闭程序,放入模型到weights文件夹!";
                tsb_pic.Enabled = false;
                tsb_video.Enabled = false;
                tsb_camera.Enabled = false;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadWeightsFromDir();//从目录加载模型
        }
        public string GetResultString(Result result)
        {
            Dictionary<string, int> resultDict = new Dictionary<string, int>();
            for (int i = 0; i < result.length; i++)
            {
                if(resultDict.ContainsKey( result.classes[i]) )
                {
                    resultDict[result.classes[i]]++;
                }
                else
                {
                    resultDict[result.classes[i]]=1;
                }
            }
 
            var resultStr = "";
            foreach(var item in resultDict)
            {
                resultStr += string.Format("{0}:{1}\n",item.Key,item.Value);
            }
            return resultStr;
        }
        private void tsb_pic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
            if (ofd.ShowDialog() != DialogResult.OK) return;
            tssl_show.Text = "正在检测中...";
            Task.Run(() => {
                var sw = new Stopwatch();
                sw.Start();
                Mat image = Cv2.ImRead(ofd.FileName);
                detetor.Confidence =Convert.ToSingle(numericUpDown1.Value);
                detetor.IOU = Convert.ToSingle(numericUpDown2.Value);
                var results=detetor.Inference(image);
                
                var resultImage = detetor.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image), results);
    
                sw.Stop();
                pb_show.Image = resultImage;
                tb_res.Text = GetResultString(results);
                tssl_show.Text = "检测已完成!总计耗时"+sw.Elapsed.TotalSeconds+"秒";
            });
           
 
 
        }
 
        public void VideoProcess(string videoPath)
        {
            Task.Run(() => {
 
                detetor.Confidence = Convert.ToSingle(numericUpDown1.Value);
                detetor.IOU = Convert.ToSingle(numericUpDown2.Value);
                VideoCapture capture = new VideoCapture(videoPath);
                if (!capture.IsOpened())
                {
                    tssl_show.Text="视频打开失败!";
                    return;
                }
                Mat frame = new Mat();
                var sw = new Stopwatch();
                int fps = 0;
                while (videoStart)
                {
 
                    capture.Read(frame);
                    if (frame.Empty())
                    {
                        Console.WriteLine("data is empty!");
                        break;
                    }
                    sw.Start();
                    var results = detetor.Inference(frame);
                    var resultImg = detetor.DrawImage(frame,results);
                    sw.Stop();
                    fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
                    sw.Reset();
                    Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
                    //显示结果
                    pb_show.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultImg);
                    tb_res.Text = GetResultString(results);
                    Thread.Sleep(5);
 
 
                }
 
                capture.Release();
 
                pb_show.Image = null;
                tssl_show.Text = "视频已停止!";
                tsb_video.Text = "选择视频";
 
            });
        }
        public void CameraProcess(int cameraIndex=0)
        {
            Task.Run(() => {
 
                detetor.Confidence = Convert.ToSingle(numericUpDown1.Value);
                detetor.IOU = Convert.ToSingle(numericUpDown2.Value);
                VideoCapture capture = new VideoCapture(cameraIndex);
                if (!capture.IsOpened())
                {
                    tssl_show.Text = "摄像头打开失败!";
                    return;
                }
                Mat frame = new Mat();
                var sw = new Stopwatch();
                int fps = 0;
                while (videoStart)
                {
 
                    capture.Read(frame);
                    if (frame.Empty())
                    {
                        Console.WriteLine("data is empty!");
                        break;
                    }
                    sw.Start();
                    var results = detetor.Inference(frame);
                    var resultImg = detetor.DrawImage(frame, results);
                    sw.Stop();
                    fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
                    sw.Reset();
                    Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
                    //显示结果
                    pb_show.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultImg);
                    tb_res.Text = GetResultString(results);
                    Thread.Sleep(5);
 
 
                }
 
                capture.Release();
 
                pb_show.Image = null;
                tssl_show.Text = "摄像头已停止!";
                tsb_camera.Text = "打开摄像头";
 
            });
        }
        private void tsb_video_Click(object sender, EventArgs e)
        {
            if(tsb_video.Text=="选择视频")
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "视频文件(*.*)|*.mp4;*.avi";
                if (ofd.ShowDialog() != DialogResult.OK) return;
                videoStart = true;
                VideoProcess(ofd.FileName);
                tsb_video.Text = "停止";
                tssl_show.Text = "视频正在检测中...";
 
            }
            else
            {
                videoStart = false;
               
            }
        }
 
        private void tsb_camera_Click(object sender, EventArgs e)
        {
            if (tsb_camera.Text == "打开摄像头")
            {
                videoStart = true;
                CameraProcess(0);
                tsb_camera.Text = "停止";
                tssl_show.Text = "摄像头正在检测中...";
 
            }
            else
            {
                videoStart = false;
 
            }
        }
 
        private void tsb_exit_Click(object sender, EventArgs e)
        {
            videoStart = false;
            this.Close();
        }
 
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            numericUpDown1.Value = Convert.ToDecimal(trackBar1.Value / 100.0f);
        }
 
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            numericUpDown2.Value = Convert.ToDecimal(trackBar2.Value / 100.0f);
        }
 
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            trackBar1.Value = (int)(Convert.ToSingle(numericUpDown1.Value) * 100);
        }
 
        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            trackBar2.Value = (int)(Convert.ToSingle(numericUpDown2.Value) * 100);
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            tssl_show.Text="加载模型:"+comboBox1.Text;
            detetor.LoadWeights(weightsPath+"\\"+comboBox1.Text,labelTxt);
            tssl_show.Text = "模型加载已完成!";
        }
    }
}

【使用步骤】

使用步骤:
(1)首先根据官方框架ultralytics安装教程安装好yolov8环境,并根据官方export命令将自己pt模型转成onnx模型
(2)使用vs2019打开sln项目,选择x release并且修改一些必要的参数,比如输入shape等,点击运行即可查看最后效果

特别注意如果运行报错了,请参考我的博文进行重新引用我源码的DLL:

【提供文件】

C#源码
yolov8s.onnx模型(不提供pytorch模型)
训练的map,P,R曲线图(在weights\results.png)
测试图片(在test_img文件夹下面)

因篇幅问题不能全部显示,请点此查看更多更全内容