/// <summary> /// use c# to do preprocess, the result will a little different from others /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void classifyPreBtn_Click(object sender, EventArgs e) { if (!netInited) { MessageBox.Show("Network is not inited"); return; } if (pictureBox1.Image == null) { MessageBox.Show("there is no pic in picBox!"); return; } MsnhnetDef.Dim dim = net.GetInputDim(); Bitmap bitmap = (Bitmap)pictureBox1.Image;//image转bitmap Bitmap outBitmap; ImgPro.ReSize(bitmap, dim.width, dim.height, out outBitmap, true);// a little different from opencv bitmap = ImgPro.ConvertTo24bpp(outBitmap); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat); float[] inputData = new float[3 * bmpData.Width * bmpData.Height]; unsafe { byte *ptr = (byte *)(bmpData.Scan0); for (int i = 0; i < bmpData.Height; i++) { for (int k = 0; k < 3; ++k) { for (int j = 0; j < bmpData.Width; j++) { inputData[(k * bmpData.Width * bmpData.Height + i * bmpData.Width + j)] = ptr[i * 3 * bmpData.Width + j * 3 + k] / 255.0f; } } } } bitmap.UnlockBits(bmpData); int best = net.RunClassifyNoPred(inputData, false); string[] labelList = labels.Split('\n'); // actually time need to add preprocess time , you can do it by your self float time = net.GetCpuForwardTime(); richTextBox1.AppendText("CPU inference time:" + time.ToString() + " ms\n"); richTextBox1.AppendText("CPU inferece result: " + labelList[best] + "\n"); }
private void resetImgBtn_Click(object sender, EventArgs e) { try { pictureBox1.Image = Image.FromFile(savedImagePath); pictureBox1.Image = ImgPro.ConvertTo24bpp(pictureBox1.Image); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void yoloGPUBtn_Click(object sender, EventArgs e) { if (!netInited) { MessageBox.Show("Network is not inited"); return; } if (!Msnhnet.WithGPU()) { MessageBox.Show("Msnhnet not build with GPU"); return; } try { Rectangle rect = new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height); // image的长宽,非pictureBox的长宽 Bitmap bitmap = (Bitmap)pictureBox1.Image; //image转bitmap BitmapData bmpdata = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat); List <Msnhnet.BBox> bboxes = net.RunYoloList(bmpdata, false, true); bitmap.UnlockBits(bmpdata); for (int i = 0; i < bboxes.Count; i++) { bboxes[i] = ImgPro.bboxResize2Org(bboxes[i], net.GetInputDim().width, net.GetInputDim().height, bitmap.Width, bitmap.Height); } pictureBox1.Image = drawYolo(bitmap, bboxes, labels); float time = net.GetGpuForwardTime(); richTextBox1.AppendText("GPU inference time:" + time.ToString() + " ms\n"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void readImgBtn_Click(object sender, EventArgs e) { OpenFileDialog of = new OpenFileDialog(); of.Filter = picOpenFilter; if (of.ShowDialog() == DialogResult.OK) { string temp = of.FileName; savedImagePath = temp; resetImgBtn.Enabled = true; try { pictureBox1.Image = Image.FromFile(temp); pictureBox1.Image = ImgPro.ConvertTo24bpp(pictureBox1.Image); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }