|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Data; |
| 6 | +using System.Drawing; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Forms; |
| 11 | +using System.Runtime.InteropServices; |
| 12 | + |
| 13 | + |
| 14 | +namespace YOLOv3_CS_Example |
| 15 | +{ |
| 16 | + public partial class Form1 : Form |
| 17 | + { |
| 18 | + [DllImport("libYOLOv3SE.dll", EntryPoint = "YoloLoad", CallingConvention = CallingConvention.Cdecl)] |
| 19 | + static extern IntPtr YoloLoad(String cfg,String weights); |
| 20 | + [DllImport("libYOLOv3SE.dll", EntryPoint = "YoloDetectFromFile", CallingConvention = CallingConvention.Cdecl)] |
| 21 | + static extern int YoloDetectFromFile(String img_path,IntPtr net,float threshold,float[] result,int result_sz); |
| 22 | + [DllImport("libYOLOv3SE.dll", EntryPoint = "YoloRelease", CallingConvention = CallingConvention.Cdecl)] |
| 23 | + static extern int YoloRelease(IntPtr net); |
| 24 | + |
| 25 | + String image_file; |
| 26 | + IntPtr net = IntPtr.Zero; |
| 27 | + public Form1() |
| 28 | + { |
| 29 | + InitializeComponent(); |
| 30 | + pictureBox1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left); |
| 31 | + btn_load.Anchor = (AnchorStyles.Bottom); |
| 32 | + btn_detect.Anchor = (AnchorStyles.Bottom); |
| 33 | + btn_openimage.Anchor = (AnchorStyles.Bottom); |
| 34 | + btn_release.Anchor = (AnchorStyles.Bottom); |
| 35 | + } |
| 36 | + |
| 37 | + private void OnClickLoad(object sender, EventArgs e) |
| 38 | + { |
| 39 | + if (net != IntPtr.Zero) |
| 40 | + { |
| 41 | + YoloRelease(net); |
| 42 | + } |
| 43 | + String weights="", cfg=""; |
| 44 | + OpenFileDialog ofd = new OpenFileDialog(); |
| 45 | + ofd.Filter = "Weights Files(*.weights)|*.weights|All files (*.*)|*.*"; |
| 46 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 47 | + { |
| 48 | + weights = ofd.FileName; |
| 49 | + ofd.Filter = "Cfg Files(*.cfg)|*.cfg|All files (*.*)|*.*"; |
| 50 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 51 | + { |
| 52 | + cfg = ofd.FileName; |
| 53 | + } |
| 54 | + } |
| 55 | + if (cfg != "" && weights != "") |
| 56 | + { |
| 57 | + this.net=YoloLoad(cfg, weights); |
| 58 | + }else |
| 59 | + { |
| 60 | + this.net = IntPtr.Zero; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void OnClickDetect(object sender, EventArgs e) |
| 65 | + { |
| 66 | + if (this.net != IntPtr.Zero) |
| 67 | + { |
| 68 | + float[] result = new float[1024]; |
| 69 | + int n=YoloDetectFromFile(image_file, net, 0.5F, result, 1024); |
| 70 | + FileStream fs = new FileStream(image_file, FileMode.Open, FileAccess.Read); |
| 71 | + Image img = System.Drawing.Image.FromStream(fs); |
| 72 | + fs.Close(); |
| 73 | + Graphics g = Graphics.FromImage(img); |
| 74 | + Pen pen = new Pen(Color.Red, 5); |
| 75 | + |
| 76 | + for (int i = 0; i < n; i++) |
| 77 | + { |
| 78 | + int c= (int)result[i * 6 + 0]; |
| 79 | + int x = (int)result[i * 6 + 2]; |
| 80 | + int y = (int)result[i * 6 + 3]; |
| 81 | + int w = (int)result[i * 6 + 4]; |
| 82 | + int h = (int)result[i * 6 + 5]; |
| 83 | + Rectangle rect = new Rectangle(x, y, w, h); |
| 84 | + g.DrawRectangle(pen, rect); |
| 85 | + } |
| 86 | + pictureBox1.Image = img; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void OnClickRelease(object sender, EventArgs e) |
| 91 | + { |
| 92 | + if (net != IntPtr.Zero) |
| 93 | + { |
| 94 | + YoloRelease(net); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void OnClickOpenImage(object sender, EventArgs e) |
| 99 | + { |
| 100 | + OpenFileDialog ofd = new OpenFileDialog(); |
| 101 | + ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"; |
| 102 | + |
| 103 | + |
| 104 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 105 | + { |
| 106 | + |
| 107 | + FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read); |
| 108 | + Image img = System.Drawing.Image.FromStream(fs); |
| 109 | + fs.Close(); |
| 110 | + |
| 111 | + pictureBox1.Image = img; |
| 112 | + pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; |
| 113 | + this.image_file = ofd.FileName; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments