-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
281 lines (235 loc) · 12.3 KB
/
Form1.cs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using Darknet;
using System.Diagnostics;
namespace CoolooAI.YOLOv3
{
public partial class Form1 : Form
{
//global vars
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string baseFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets");
string imagePath = "";
string[,] COLORS = new string[93, 2] {
{"#DC143C", "#FFFFFF"},{"#0000FF", "#FFFFFF"},{"#8A2BE2", "#FFFFFF"},{"#7FFF00", "#1F2D3D"},{"#A52A2A", "#FFFFFF"},{"#2F4F4F", "#FFFFFF"},{"#FF1493", "#FFFFFF"},{"#D2691E", "#FFFFFF"},{"#FF7F50", "#1F2D3D"},{"#228B22", "#FFFFFF"},{"#00008B", "#FFFFFF"},{"#6495ED", "#FFFFFF"},{"#B8860B", "#FFFFFF"},{"#006400", "#FFFFFF"},{"#8B008B", "#FFFFFF"},{"#556B2F", "#FFFFFF"},{"#FF8C00", "#1F2D3D"},{"#E9967A", "#1F2D3D"},{"#483D8B", "#FFFFFF"},{"#00CED1", "#FFFFFF"},{"#9400D3", "#FFFFFF"},{"#00BFFF", "#FFFFFF"},{"#1E90FF", "#FFFFFF"},{"#B22222", "#FFFFFF"},{"#FF00FF", "#FFFFFF"},{"#FFD700", "#1F2D3D"},{"#DAA520", "#1F2D3D"},{"#808080", "#FFFFFF"},{"#008000", "#FFFFFF"},{"#ADFF2F", "#1F2D3D"},{"#FF69B4", "#1F2D3D"},{"#CD5C5C", "#FFFFFF"},{"#4B0082", "#FFFFFF"},{"#7CFC00", "#1F2D3D"},{"#ADD8E6", "#1F2D3D"},{"#F08080", "#1F2D3D"},{"#FFB6C1", "#1F2D3D"},{"#FFA07A", "#1F2D3D"},{"#20B2AA", "#FFFFFF"},{"#DEB887", "#1F2D3D"},{"#87CEFA", "#1F2D3D"},{"#778899", "#FFFFFF"},{"#778899", "#FFFFFF"},{"#32CD32", "#FFFFFF"},{"#FF00FF", "#FFFFFF"},{"#800000", "#FFFFFF"},{"#66CDAA", "#1F2D3D"},{"#0000CD", "#FFFFFF"},{"#BA55D3", "#FFFFFF"},{"#9370DB", "#FFFFFF"},{"#3CB371", "#FFFFFF"},{"#7B68EE", "#FFFFFF"},{"#00FA9A", "#1F2D3D"},{"#48D1CC", "#1F2D3D"},{"#C71585", "#FFFFFF"},{"#7FFFD4", "#1F2D3D"},{"#000080", "#FFFFFF"},{"#808000", "#FFFFFF"},{"#6B8E23", "#FFFFFF"},{"#FFA500", "#1F2D3D"},{"#FF4500", "#FFFFFF"},{"#DA70D6", "#1F2D3D"},{"#DB7093", "#FFFFFF"},{"#FFDAB9", "#1F2D3D"},{"#CD853F", "#FFFFFF"},{"#FFC0CB", "#1F2D3D"},{"#DDA0DD", "#1F2D3D"},{"#800080", "#FFFFFF"},{"#663399", "#FFFFFF"},{"#FF0000", "#FFFFFF"},{"#BC8F8F", "#1F2D3D"},{"#4169E1", "#FFFFFF"},{"#8B4513", "#FFFFFF"},{"#FA8072", "#1F2D3D"},{"#00FFFF", "#1F2D3D"},{"#F4A460", "#1F2D3D"},{"#2E8B57", "#FFFFFF"},{"#A0522D", "#FFFFFF"},{"#87CEEB", "#1F2D3D"},{"#6A5ACD", "#FFFFFF"},{"#708090", "#FFFFFF"},{"#00FF7F", "#1F2D3D"},{"#4682B4", "#FFFFFF"},{"#D2B48C", "#1F2D3D"},{"#008080", "#FFFFFF"},{"#D8BFD8", "#1F2D3D"},{"#FF6347", "#FFFFFF"},{"#40E0D0", "#1F2D3D"},{"#EE82EE", "#1F2D3D"},{"#F5DEB3", "#1F2D3D"},{"#FFFF00", "#1F2D3D"},{"#9ACD32", "#1F2D3D"},{"#000000", "#FFFFFF"}
};
List<string> Names;
bool loading = false;
YoloWrapper YOLO;
// end global vars
public Form1()
{
InitializeComponent();
}
public Task<bool> LoadYoloAsync(string modelName)
{
loading = true;
return Task.Run(() =>
{
BeginInvoke(new Action(() =>
{
button1.Text = "Loading model ...";
}));
// pre-training model
var files = Directory.GetFiles(Path.Combine(basePath, "model", modelName));
var ConfigFile = files.Where(p => p.EndsWith(".cfg")).FirstOrDefault();
var WeightsFile = files.Where(p => p.EndsWith(".weights")).FirstOrDefault();
var NamesFile = files.Where(p => p.EndsWith(".names")).FirstOrDefault();
if (string.IsNullOrEmpty(ConfigFile) || string.IsNullOrEmpty(WeightsFile) || string.IsNullOrEmpty(NamesFile))
{
BeginInvoke(new Action(() =>
{
MessageBox.Show("Missing Model files *.cfg, *.weight, *.names, all are indispensable £¡");
button1.Text = "Loading failed";
}));
return false;
}
var fileinfo = new FileInfo(WeightsFile);
label6.BeginInvoke(new Action(() =>
{
label6.Text = $"YOLOv3, RTX 2080 Ti, CUDA 10.2, {(modelName == "yolo-v3" ? "" : "Custom ")}Dataset = {fileinfo.LastWriteTime.ToString("yyyy/MM/dd")}";
}));
Names = File.ReadAllLines(NamesFile).ToList();
// use GPU 0
var GpuIndex = 0;
YOLO = new YoloWrapper(ConfigFile, WeightsFile, GpuIndex);
BeginInvoke(new Action(() =>
{
button1.Text = "Detect";
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox1.Image = null;
if (listBox1.Items.Count > 0)
{
DetectImageAsync(Path.Combine(baseFolder, modelName, listBox1.SelectedItem.ToString()));
}
}));
loading = false;
return true;
});
}
public Task<bool> DetectImageAsync(string imagePath)
{
return Task.Run(() =>
{
if (loading) return false;
if (YOLO == null) return false;
BeginInvoke(new Action(() =>
{
button1.Text = "Detecting...";
pictureBox1.ImageLocation = imagePath; // ;
using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var image = Image.FromStream(fileStream, false, false))
{
//if (image.Width > 800 || image.Height > 600)
//{
// this.Width = image.Width + 400;
// this.Height = image.Height + 400;
//}
//else if (image.Width < 800 && image.Height < 600)
//{
// this.Width = 1100;
// this.Height = 800;
//}
if (image.Width > dataGridView1.Width)
{
this.Width = image.Width + 300;
}
else
{
this.Width = dataGridView1.Width + 300;
}
dataGridView1.Top = image.Height + 40;
dataGridView1.Height = this.Height - dataGridView1.Top - 120;
//dataGridView1.BackgroundColor = Color.Black;
this.StartPosition = FormStartPosition.CenterScreen;
}
}
}));
var t1 = new Stopwatch();
t1.Start();
//var bboxes = YOLO.Detect(imagePath)?.Where(p => p.obj_id > 0 && p.prob > 0).ToList();
var bboxes = YOLO.Detect(imagePath)?.Where(p => p.w > 0 && p.h > 0 && p.prob > 0).ToList();
t1.Stop();
var obj_bboxes = bboxes?.Select((p, i) => new obj_box
{
row_id = i + 1,
obj_id = (int)p.obj_id,
obj_name = Names[(int)p.obj_id],
x = (int)p.x,
y = (int)p.y,
w = (int)p.w,
h = (int)p.h,
prob = p.prob,
track_id = (int)p.track_id,
frames_counter = (int)p.frames_counter,
x_3d = p.x_3d,
y_3d = p.y_3d,
z_3d = p.z_3d,
});
BeginInvoke(new Action(() =>
{
textBox1.Clear();
label1.Text = $"{bboxes?.Count()} object(s)";
label2.Text = t1.ElapsedMilliseconds.ToString();
label4.Text = Math.Round((decimal)(1000 / t1.ElapsedMilliseconds), 0).ToString();
dataGridView1.DataSource = obj_bboxes?.ToList();
if (!dataGridView1.Visible) dataGridView1.Visible = true;
}));
DrawDrawBoxes(pictureBox1.CreateGraphics(), obj_bboxes.ToList());
BeginInvoke(new Action(() =>
{
textBox1.AutoScrollOffset = new Point(0, 0);
button1.Text = "Done";
}));
return true;
});
}
void DrawDrawBoxes(Graphics g, List<obj_box> list)
{
if (list?.Count == 0) return;
var pen = new Pen(Color.Yellow, 2);
int i = 0, index = 0, color_index = 0;
foreach (var detect in list)
{
index = Convert.ToInt32(detect.obj_id);
//Console.WriteLine($"{(i + 1),4} {detect.obj_id,4} {Names[index],10} {detect.prob.ToString("P2"),8} {detect.track_id,4} [{detect.w,4} {detect.h,4} {detect.x,4} {detect.y,4}]");
//0, pen\brush
color_index = i % (COLORS.Length / 2);
var color_bg = COLORS[color_index, 0]; //background color
var color_fr = COLORS[color_index, 1]; //front color
var brush = new SolidBrush(ColorTranslator.FromHtml(color_bg));
//1, draw box
pen.Color = ColorTranslator.FromHtml(color_bg);
g.DrawRectangle(pen, detect.x, detect.y, detect.w, detect.h);
//2, draw background of name
var font = new Font("Tahoma", 10);
var title = $"{i + 1} {detect.obj_name}";
var bgSize = g.MeasureString(title, font);
var rect = new RectangleF(detect.x, detect.y, bgSize.Width, bgSize.Height);
g.FillRectangle(brush, rect);
//3, draw name
g.DrawString(
title,
font,
new SolidBrush(ColorTranslator.FromHtml(color_fr)),
new PointF(detect.x, detect.y));
//4, textbox1
BeginInvoke(new Action(() =>
{
textBox1.AppendText($"{i + 1,-10}{detect.obj_name,-30}{detect.prob.ToString("P2"),-10}{Environment.NewLine}");
}));
i++;
}
pen.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files (.jpg, .png)|*.jpg;*.png";
openFileDialog1.FileName = "";
dataGridView1.AutoGenerateColumns = false;
ScanModelFolderAsync();
}
public Task<bool> ScanModelFolderAsync()
{
//scan model subfolders
return Task.Run(() =>
{
var subfolders = Directory.GetDirectories(Path.Combine(basePath, "model"));
Invoke(new Action(() =>
{
comboBox1.DataSource = subfolders.Select(p => p.Substring(p.LastIndexOf(Path.DirectorySeparatorChar) + 1)).OrderByDescending(p => p).ToList();
}));
return true;
});
}
public Task<bool> ScanImageAssetsFolderAsync(string path)
{
return Task.Run(() =>
{
var files = Directory.GetFiles(path, "*.*g");
if (files.Length == 0) return false;
Invoke(new Action(() =>
{
listBox1.DataSource = files.Select(p => p.Substring(p.LastIndexOf(Path.DirectorySeparatorChar) + 1)).ToList();
}));
return true;
});
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
_ = ScanImageAssetsFolderAsync(Path.Combine(baseFolder, comboBox1.SelectedItem.ToString()));
_ = LoadYoloAsync(comboBox1.SelectedItem.ToString());
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
Task.Run(() =>
{
DetectImageAsync(openFileDialog1.FileName);
});
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var path = Path.Combine(baseFolder, comboBox1.Text, listBox1.SelectedItem.ToString());
DetectImageAsync(path);
}
}
}