• C# PaddleDetection 安全帽检测


    目录

    效果

    项目 

    infer_cfg.yml

    代码

    下载 


    效果

    项目 

    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

    代码

    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.TopLeft.X, r[i].Rect.TopLeft.Y)
                , HersheyFonts.HersheyComplex
                , fontScale
                , scalar
                , thickness
                , lineType
                , false);
        }
    }

    sb.Clear();
    sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
    sb.AppendLine("------------------------------");

    1. using OpenCvSharp;
    2. using OpenCvSharp.Extensions;
    3. using Sdcb.PaddleDetection;
    4. using Sdcb.PaddleInference;
    5. using System;
    6. using System.Drawing;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using YamlDotNet;
    10. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
    11. namespace PaddleDetection目标检测__yolov3_darknet_
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. Bitmap bmp;
    20. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    21. string img = "";
    22. double fontScale = 1D;
    23. int thickness = 2;
    24. LineTypes lineType = LineTypes.Link4;
    25. PaddleConfig paddleConfig;
    26. PaddleDetector d;
    27. String startupPath;
    28. float confidence = 0.75f;
    29. DateTime dt1 = DateTime.Now;
    30. DateTime dt2 = DateTime.Now;
    31. StringBuilder sb = new StringBuilder();
    32. private void button1_Click(object sender, EventArgs e)
    33. {
    34. OpenFileDialog ofd = new OpenFileDialog();
    35. ofd.Filter = fileFilter;
    36. if (ofd.ShowDialog() != DialogResult.OK) return;
    37. pictureBox1.Image = null;
    38. textBox1.Text = "";
    39. img = ofd.FileName;
    40. bmp = new Bitmap(img);
    41. pictureBox1.Image = new Bitmap(img);
    42. }
    43. private void button2_Click(object sender, EventArgs e)
    44. {
    45. if (img == "")
    46. {
    47. return;
    48. }
    49. Mat src = Cv2.ImRead(img);
    50. dt1 = DateTime.Now;
    51. DetectionResult[] r = d.Run(src);
    52. dt2 = DateTime.Now;
    53. Scalar scalar = Scalar.Red;
    54. for (int i = 0; i < r.Length; i++)
    55. {
    56. if (r[i].Confidence > confidence)
    57. {
    58. Cv2.Rectangle(src, r[i].Rect, scalar, 1, LineTypes.Link8, 0);
    59. Cv2.PutText(src, r[i].LabelName+ " "+r[i].Confidence.ToString("0.00")
    60. // , new OpenCvSharp.Point(r[i].Rect.X + r[i].Rect.Width / 2, r[i].Rect.Y + r[i].Rect.Height / 2)
    61. , new OpenCvSharp.Point(r[i].Rect.TopLeft.X, r[i].Rect.TopLeft.Y)
    62. , HersheyFonts.HersheyComplex
    63. , fontScale
    64. , scalar
    65. , thickness
    66. , lineType
    67. , false);
    68. }
    69. }
    70. sb.Clear();
    71. sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
    72. sb.AppendLine("------------------------------");
    73. for (int i = 0; i < r.Length; i++)
    74. {
    75. sb.AppendLine(string.Format("{0}:{1},({2},{3},{4},{5})"
    76. , r[i].LabelName
    77. , r[i].Confidence.ToString("0.00")
    78. , r[i].Rect.TopLeft.X
    79. , r[i].Rect.TopLeft.Y
    80. , r[i].Rect.BottomRight.X
    81. , r[i].Rect.BottomRight.Y
    82. ));
    83. }
    84. textBox1.Text = sb.ToString();
    85. pictureBox2.Image = BitmapConverter.ToBitmap(src);
    86. }
    87. private void Form1_Load(object sender, EventArgs e)
    88. {
    89. startupPath = Application.StartupPath;
    90. paddleConfig = PaddleConfig.FromModelDir(startupPath + "\\yolov3_darknet\\");
    91. string configYmlPath = startupPath + "\\yolov3_darknet\\infer_cfg.yml";
    92. d = new PaddleDetector(paddleConfig, configYmlPath);
    93. }
    94. }
    95. }

    下载 

    Demo下载

  • 相关阅读:
    cortex-A7核 中断实验(按键中断实验)
    数据结构基础10:三路划分(解决快速排序的问题)
    tomcat-8.5.78连接oracle的步骤方式
    fwknop服务端代码理解
    QT基础教程(QDebug和QString)
    问题杂谈(三十八)处理Cesium双击定位后无法平移视角,只能旋转的问题
    2023高教社杯 国赛数学建模E题思路 - 黄河水沙监测数据分析
    微信小程序-1
    CEF 实现放大缩小视图功能
    【论文极速读】EMT——评估多模态LLM中的灾难性遗忘问题
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133083937