效果
项目
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 < r.Length; i++)
{
if (r[i].Confidence > 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.yml label_list中为可识别的目标
mode: paddle
draw_threshold: 0.5
metric: COCO
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:
- 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