效果
项目
VS2022+.net4.8+OpenCvSharp4+Sdcb.PaddleDetection
代码
using OpenCvSharp;using OpenCvSharp.Extensions;using Sdcb.PaddleDetection;using Sdcb.PaddleInference;using System;using System.Drawing;using System.Windows.Forms;using YamlDotNet;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 = 2D;int thickness = 4;LineTypes lineType = LineTypes.Link4;PaddleConfig paddleConfig;PaddleDetector d;String startupPath;float confidence = 0.75f;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;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);DetectionResult[] r = d.Run(src);for (int i = 0; i confidence){Scalar scalar = Scalar.RandomColor();Cv2.Rectangle(src, r[i].Rect, scalar, 1, LineTypes.Link8, 0);Cv2.PutText(src, r[i].LabelName, new OpenCvSharp.Point(r[i].Rect.X + r[i].Rect.Width / 2, r[i].Rect.Y + r[i].Rect.Height / 2), HersheyFonts.HersheyComplex, fontScale, scalar, thickness, lineType, false);}}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);}}}
infer_cfg.ymllabel_list中为可识别的目标
mode: paddledraw_threshold: 0.5metric: COCOuse_dynamic_shape: falsearch: YOLOmin_subgraph_size: 3Preprocess:- interp: 2keep_ratio: falsetarget_size:- 608- 608type: Resize- is_scale: truemean:- 0.485- 0.456- 0.406std:- 0.229- 0.224- 0.225type: NormalizeImage- type: Permutelabel_list:- person- bicycle- car- motorcycle- airplane- bus- train- truck- boat- traffic light- fire hydrant- stop sign- parking meter- bench- bird- cat- dog- horse- sheep- cow- elephant- bear- zebra- giraffe- backpack- umbrella- handbag- tie- suitcase- frisbee- skis- snowboard- sports ball- kite- baseball bat- baseball glove- skateboard- surfboard- tennis racket- bottle- wine glass- cup- fork- knife- spoon- bowl- banana- apple- sandwich- orange- broccoli- carrot- hot dog- pizza- donut- cake- chair- couch- potted plant- bed- dining table- toilet- tv- laptop- mouse- remote- keyboard- cell phone- microwave- oven- toaster- sink- refrigerator- book- clock- vase- scissors- teddy bear- hair drier- toothbrush
Demo下载