1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| using OpenCvSharp; using OpenCvSharp.Dnn; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms;
namespace OpenCvSharp_DNN_Demo { public partial class frmMain : Form { public frmMain() { InitializeComponent(); }
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; string image_path = "";
DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now;
float confThreshold; float nmsThreshold;
int num_stride = 3; float[] strides = new float[3] { 8.0f, 16.0f, 32.0f };
string modelpath;
int inpHeight; int inpWidth;
List<string> class_names; int num_class;
Net opencv_net; Mat BN_image;
Mat image; Mat result_image;
private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null; pictureBox2.Image = null; textBox1.Text = "";
image_path = ofd.FileName; pictureBox1.Image = new Bitmap(image_path); image = new Mat(image_path); }
private void Form1_Load(object sender, EventArgs e) { confThreshold = 0.8f; nmsThreshold = 0.5f;
modelpath = "model/yolo_free_huge_widerface_192x320.onnx";
inpHeight = 192; inpWidth = 320;
opencv_net = CvDnn.ReadNetFromOnnx(modelpath);
class_names = new List<string>(); class_names.Add("face"); num_class = 1;
image_path = "test_img/1.jpg"; pictureBox1.Image = new Bitmap(image_path);
}
private unsafe void button2_Click(object sender, EventArgs e) { if (image_path == "") { return; } textBox1.Text = "检测中,请稍等……"; pictureBox2.Image = null; Application.DoEvents();
image = new Mat(image_path);
float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols); int neww = (int)(image.Cols * ratio); int newh = (int)(image.Rows * ratio);
Mat dstimg = new Mat(); Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));
Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);
BN_image = CvDnn.BlobFromImage(dstimg);
opencv_net.SetInput(BN_image);
Mat[] outs = new Mat[1] { new Mat() }; string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();
dt1 = DateTime.Now;
opencv_net.Forward(outs, outBlobNames);
dt2 = DateTime.Now;
int num_proposal = outs[0].Size(1); int nout = outs[0].Size(2);
float* pdata = (float*)outs[0].Data;
List<float> confidences = new List<float>(); List<Rect> boxes = new List<Rect>(); List<int> classIds = new List<int>();
for (int n = 0; n < num_stride; n++) { int num_grid_x = (int)Math.Ceiling(inpWidth / strides[n]); int num_grid_y = (int)Math.Ceiling(inpHeight / strides[n]);
for (int i = 0; i < num_grid_y; i++) { for (int j = 0; j < num_grid_x; j++) { float box_score = pdata[4]; int max_ind = 0; float max_class_socre = 0; for (int k = 0; k < num_class; k++) { if (pdata[k + 5] > max_class_socre) { max_class_socre = pdata[k + 5]; max_ind = k; } } max_class_socre = max_class_socre * box_score; max_class_socre = (float)Math.Sqrt(max_class_socre);
if (max_class_socre > confThreshold) { float cx = (0.5f + j + pdata[0]) * strides[n]; float cy = (0.5f + i + pdata[1]) * strides[n]; float w = (float)(Math.Exp(pdata[2]) * strides[n]); float h = (float)(Math.Exp(pdata[3]) * strides[n]);
float xmin = (float)((cx - 0.5 * w) / ratio); float ymin = (float)((cy - 0.5 * h) / ratio); float xmax = (float)((cx + 0.5 * w) / ratio); float ymax = (float)((cy + 0.5 * h) / ratio);
int left = (int)((cx - 0.5 * w) / ratio); int top = (int)((cy - 0.5 * h) / ratio); int width = (int)(w / ratio); int height = (int)(h / ratio);
confidences.Add(max_class_socre); boxes.Add(new Rect(left, top, width, height)); classIds.Add(max_ind); } pdata += nout; } }
}
int[] indices; CvDnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, out indices);
result_image = image.Clone();
for (int ii = 0; ii < indices.Length; ++ii) { int idx = indices[ii]; Rect box = boxes[idx]; Cv2.Rectangle(result_image, new OpenCvSharp.Point(box.X, box.Y), new OpenCvSharp.Point(box.X + box.Width, box.Y + box.Height), new Scalar(0, 0, 255), 2); string label = class_names[classIds[idx]] + ":" + confidences[idx].ToString("0.00"); Cv2.PutText(result_image, label, new OpenCvSharp.Point(box.X, box.Y - 5), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2); }
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream()); textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
}
private void pictureBox2_DoubleClick(object sender, EventArgs e) { Common.ShowNormalImg(pictureBox2.Image); }
private void pictureBox1_DoubleClick(object sender, EventArgs e) { Common.ShowNormalImg(pictureBox1.Image); } } }
|