• C#中如何使用PaddleDetection实现图片安全帽检测功能?
  • 发布于 1个月前
  • 53 热度
    0 评论
  • 双人剧
  • 0 粉丝 30 篇博客
  •   
效果

项目

infer_cfg.yml
mode: fluid
draw_threshold: 0.5
metric: VOC
use_dynamic_shape: false
arch: YOLO
min_subgraph_size: 3
Preprocess:
- interp: 2
  keep_ratio: false
  target_size:
  - 608
  - 608
  type: Resize
- is_scale: true
  mean:
  - 0.485
  - 0.456
  - 0.406
  std:
  - 0.229
  - 0.224
  - 0.225
  type: NormalizeImage
- type: Permute
label_list:
- head
- helmet
- person
代码
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);
            // 堆代码 duidaima.com
            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);
        }
    }
}

用户评论