// 窗口关闭 private void SingleCamera_FormClosing(object sender, FormClosingEventArgs e) { BnClose_Click(sender, e); this.Cursor = Cursors.WaitCursor; // wait ~ 1.5 seconds for (int i = 0; i < 3; i++) { System.Threading.Thread.Sleep(500); } this.Cursor = Cursors.Default; PaddleX.FreeModelMemory(paddlex.model); //释放model资源 System.Environment.Exit(0); }
// 加载模型 private void BnLoadModel_Click(object sender, EventArgs e) { BnStopDetection_Click(sender, e); //Paths = System.IO.Directory.GetFiles(@"C:\Users\Cleme\source\repos\WindowsFormsApp3\bin\Debug\netcoreapp3.1\datasets\vegetables_cls\all"); Paths = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"..\..\..\vegetables_cls"); CommonOpenFileDialog fileDialog = new CommonOpenFileDialog(); fileDialog.IsFolderPicker = true; this.bnLoadModel.Enabled = false; if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok) { if (paddlex.modelPath == "") { paddlex.modelPath = fileDialog.FileName; } //MessageBox.Show("已选择模型路径:" + modelPath, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information); paddlex.model = PaddleX.CreatePaddlexModel(ref paddlex.modelType, paddlex.modelPath, paddlex.useGPU, paddlex.useTrt, paddlex.useMkl, paddlex.mklThreadNum, paddlex.gpuID, paddlex.key, paddlex.useIrOptim); this.bnLoadModel.Enabled = true; //Func<bool> handle = () => //{ // this.Cursor = Cursors.WaitCursor; // paddlex.model = PaddleX.CreatePaddlexModel(ref paddlex.modelType, paddlex.modelPath, paddlex.useGPU, paddlex.useTrt, paddlex.useMkl, paddlex.mklThreadNum, paddlex.gpuID, paddlex.key, paddlex.useIrOptim); // return true; //}; //IAsyncResult result = handle.BeginInvoke(null, null); //Thread loading = new Thread(() => //{ // while (!result.AsyncWaitHandle.WaitOne(500)) // { // //do something else ... // } // if (handle.EndInvoke(result)) // { // this.Cursor = Cursors.Default; // this.bnLoadModel.Enabled = true; // } //}); //loading.IsBackground = true; //loading.Start(); //GC.KeepAlive(modelType); switch (paddlex.modelType) { case 0: tbModeltype.Text = "0:图像分类"; break; case 1: tbModeltype.Text = "1:目标检测"; break; case 2: tbModeltype.Text = "2:语义分割"; break; } if (paddlex.useGPU) { tbModeltype.Text += " GPU"; } else { tbModeltype.Text += " CPU"; } bnStartDetection.Enabled = true; //bnStopDetection.Enabled = true; bnThreshold.Enabled = true; } else { this.bnLoadModel.Enabled = true; } }
private Bitmap Inference(ref Bitmap bmp) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Bitmap bmpNew = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), bmp.PixelFormat); //Bitmap resultShow; Mat img = BitmapConverter.ToMat(bmpNew); Graphics g = Graphics.FromImage(bmpNew); SolidBrush brush = new SolidBrush(Color.White); int channel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8; byte[] source = GetbyteData(bmp); //int stride; //byte[] source = GetBGRValues(bmp, out stride); if (paddlex.modelType == 0) { bool res = PaddleX.PaddlexClsPredict(paddlex.model, source, bmp.Height, bmp.Width, channel, out int categoryID, out float score); if (res && score > 0.9) { Scalar color = new Scalar(0, 0, 255); string text = paddlex.category[categoryID] + ": " + score.ToString("f2"); //OpenCvSharp.Size labelSize = Cv2.GetTextSize(text, HersheyFonts.HersheySimplex, 1, 1, out int baseline); //Cv2.Rectangle(img, new OpenCvSharp.Point(0, 0), new OpenCvSharp.Point(100,20), color, -1, LineTypes.AntiAlias); //Cv2.PutText(img, text, new OpenCvSharp.Point(5, 10), HersheyFonts.HersheySimplex, 0.5, Scalar.White); //resultShow = new Bitmap(img.Cols, img.Rows, (int)img.Step(), PixelFormat.Format24bppRgb, img.Data); //g = Graphics.FromImage(resultShow); Rectangle rect = new Rectangle(5, 5, 110, 12); g.FillRectangle(Brushes.Red, rect); Rectangle innerBounds = new Rectangle(rect.Left, rect.Top, rect.Width - 1, rect.Height - 1); g.DrawRectangle(Pens.Red, innerBounds); g.DrawString(text, this.Font, brush, new PointF(5, 5)); } } else if (paddlex.modelType == 1) { int max_box = 10; bool res = false; float[] result = new float[max_box * 6 + 1]; res = PaddleX.PaddlexDetPredict(paddlex.model, source, bmp.Height, bmp.Width, channel, max_box, result, paddlex.visualize); if (res) { Scalar color = new Scalar(255, 0, 0); for (int i = 0; i < result[0]; i++) { if (result[6 * i + 2] < 0.9) { continue; } Rect rect = new Rect((int)result[6 * i + 3], (int)result[6 * i + 4], (int)result[6 * i + 5], (int)result[6 * i + 6]); Cv2.Rectangle(img, rect, color, 1, LineTypes.AntiAlias); string text = paddlex.category[(int)result[6 * i + 1]] + ":" + result[6 * i + 2].ToString("f2"); Cv2.PutText(img, text, new OpenCvSharp.Point((int)result[6 * i + 3], (int)result[6 * i + 4] - 5), HersheyFonts.HersheySimplex, 0.3, Scalar.Red); } } else { LogHelper.WriteLog("产品ID检测失败!"); } } stopwatch.Stop(); DateTime now = DateTime.Now; brush.Color = Color.Red; g.DrawString(now.ToString("yyyy-MM-dd HH:mm:ss dddd"), this.Font, brush, new PointF(bmpNew.Width - 120, bmpNew.Height - 15)); g.DrawString(string.Format("ID:{0},耗时:{1}/ms", ++n, stopwatch.ElapsedMilliseconds), this.Font, brush, new PointF(5, bmpNew.Height - 15)); brush.Dispose(); g.Dispose(); System.GC.Collect(); return(bmpNew); }