C# PaddleDetection 安全帽检测

效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using Sdcb.PaddleDetection;
using Sdcb.PaddleInference;
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YamlDotNet;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace PaddleDetection目标检测__yolov3_darknet_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string img = "";

        double fontScale = 1D;
        int thickness = 2;
        LineTypes lineType = LineTypes.Link4;

        PaddleConfig paddleConfig;
        PaddleDetector d;
        String startupPath;
        float confidence = 0.75f;

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        StringBuilder sb = new StringBuilder();

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            textBox1.Text = "";

            img = ofd.FileName;
            bmp = new Bitmap(img);
            pictureBox1.Image = new Bitmap(img);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (img == "")
            {
                return;
            }

            Mat src = Cv2.ImRead(img);

            dt1 = DateTime.Now;
            DetectionResult[] r = d.Run(src);
            dt2 = DateTime.Now;

            Scalar scalar = Scalar.Red;

            for (int i = 0; i < r.Length; i++)
            {
                if (r[i].Confidence > confidence)
                {
                    
                    Cv2.Rectangle(src, r[i].Rect, scalar, 1, LineTypes.Link8, 0);
                    Cv2.PutText(src, r[i].LabelName+ " "+r[i].Confidence.ToString("0.00")
                       // , new OpenCvSharp.Point(r[i].Rect.X + r[i].Rect.Width / 2, r[i].Rect.Y + r[i].Rect.Height / 2)
                        , new OpenCvSharp.Point(r[i].Rect.TopLeft.X, r[i].Rect.TopLeft.Y)
                        , HersheyFonts.HersheyComplex
                        , fontScale
                        , scalar
                        , thickness
                        , lineType
                        , false);
                }
            }

            sb.Clear();
            sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
            sb.AppendLine("------------------------------");
            for (int i = 0; i < r.Length; i++)
            {
                sb.AppendLine(string.Format("{0}:{1},({2},{3},{4},{5})"
                    , r[i].LabelName
                    , r[i].Confidence.ToString("0.00")
                    , r[i].Rect.TopLeft.X
                    , r[i].Rect.TopLeft.Y
                    , r[i].Rect.BottomRight.X
                    , r[i].Rect.BottomRight.Y
                    ));
            }
            textBox1.Text = sb.ToString();

            pictureBox2.Image = BitmapConverter.ToBitmap(src);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;
            paddleConfig = PaddleConfig.FromModelDir(startupPath + "\\yolov3_darknet\\");
            string configYmlPath = startupPath + "\\yolov3_darknet\\infer_cfg.yml";
            d = new PaddleDetector(paddleConfig, configYmlPath);
        }
    }
}

Demo下载​​​​​​​