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.Xml;
12
13 namespace Image_mark
14 {
15
16 public partial class Form1 : Form
17 {
18 // 我设置的全局变量
19 private List<string> all_picture_path;
20 private List<string> all_picture_name;
21 private string current_picture_path;
22 private int current_picture_index; // 从1开始
23
24 private int function_select;
25 private const int BROW = 1;
26 private const int EYE = 2;
27 private const int NOSE = 3;
28 private const int MOUTH = 4;
29
30 private string xml_path;
31 private int xml_node_num;
32
33 private string output_path;
34
35 private string brow_tpye_path;
36 private string eye_tpye_path;
37 private string nose_tpye_path;
38 private string mouth_tpye_path;
39 private ImageList marker_type_list;
40
41 private Button[] type_button = new Button[20];
42 private const int type_button_WIDTH = 100;
43 private const int type_button_HEIGHT = 50;
44 private const int type_button_1_location_x = 450;
45 private const int type_button_1_location_y = 70;
46
47 //private Panel button_panel;
48
49
50 public Form1()
51 {// 构造函数,初始化
52 InitializeComponent();
53
54 Init();
55
56 }
57
58 private void Init()
59 {// 一些初始化
60
61 all_picture_path = new List<string>();
62 all_picture_name = new List<string>();
63 current_picture_index = 0;
64
65 buttonLeft.Enabled = false;
66 buttonRight.Enabled = false;
67
68 function_select = 0;
69
70 xml_path = "E:\\";
71 xml_node_num = 0;
72 output_path = "E:\\";
73
74 // 需要修改 标记类型模板 路径
75 brow_tpye_path = "E:\\huangtao7\\C#\\project\\Image_mark\\brow\\";
76 eye_tpye_path = "E:\\huangtao7\\C#\\project\\Image_mark\\eye\\";
77 nose_tpye_path = "E:\\huangtao7\\C#\\project\\Image_mark\\nose\\";
78 mouth_tpye_path = "E:\\huangtao7\\C#\\project\\Image_mark\\mouth\\";
79
80 marker_type_list = new ImageList();
81 marker_type_list.ImageSize = new Size(100, 60);
82
83 //button_panel = new Panel();
84 //button_panel.Controls.Add(buttonMarkerType1);
85 //button_panel.Controls.Add(buttonMarkerType2);
86 //button_panel.Controls.Add(buttonMarkerType3);
87 //button_panel.Controls.Add(buttonMarkerType4);
88 //button_panel.Controls.Add(buttonMarkerType5);
89 //button_panel.Controls.Add(buttonMarkerType6);
90
91 // 显示样例 ListView
92 //listView1.Bounds = new Rectangle(new Point(10, 10), new Size(300, 200));
93 // 显示方式
94 listView1.View = View.LargeIcon;
95 listView1.LabelEdit = false;
96 listView1.AllowColumnReorder = false;
97 listView1.CheckBoxes = false;
98 listView1.FullRowSelect = false;
99 listView1.GridLines = false;
100 listView1.BorderStyle = BorderStyle.None;
101 listView1.Sorting = SortOrder.Ascending;
102
103
104
105 }
106
107 #region 根据标记功能设置 ListView 显示样例
108 void Set_example_picture()
109 {
110 marker_type_list.Images.Clear();
111 listView1.Items.Clear();
112
113 switch(function_select)
114 {
115 case BROW:
116 {
117 #region 显示眉毛的样例
118 DirectoryInfo TheFolder = new DirectoryInfo(brow_tpye_path);
119 List<string> brow_type_name = new List<string>();
120
121 foreach (FileInfo NextFile in TheFolder.GetFiles("*.jpg"))
122 {
123 marker_type_list.Images.Add(Image.FromFile(brow_tpye_path + @"\" + NextFile.Name));
124 brow_type_name.Add(NextFile.Name);
125
126 }
127 //foreach (FileInfo NextFile in TheFolder.GetFiles("*.png"))
128 //{
129 // marker_type_list.Images.Add(Image.FromFile(brow_tpye_path + @"\" + NextFile.Name));
130 // brow_type_name.Add(NextFile.Name);
131
132 //}
133
134 listView1.LargeImageList = marker_type_list;
135 for (int i = 0; i < marker_type_list.Images.Count; i++)
136 {
137 listView1.Items.Add(brow_type_name[i]);
138 listView1.Items[i].ImageIndex = i;
139 }
140 #endregion
141
142
143
144 }
145 break;
146 case EYE:
147 {
148
149 }
150 break;
151 case NOSE:
152 {
153
154 }
155 break;
156 case MOUTH:
157 {
158
159 }
160 break;
161 default: break;
162 }
163
164
165 }
166 #endregion
167
168 #region 根据标记功能加载 XML 文件,设置按钮
169 void Load_xml_set_button()
170 {
171 XmlDocument xmlDoc = new XmlDocument();
172 XmlReaderSettings settings = new XmlReaderSettings();
173 settings.IgnoreComments = true;
174 XmlReader reader = XmlReader.Create("E:\\brow.xml", settings);
175 xmlDoc.Load(reader);
176
177 if(xmlDoc.DocumentElement.ChildNodes.Count <=0)
178 {
179 MessageBox.Show("该 xml 无配置信息");
180 return;
181 }
182
183 // 标记类型样本数量
184 xml_node_num = xmlDoc.DocumentElement.ChildNodes.Count;
185
186 // 是眉毛 xml
187 if(xmlDoc.DocumentElement.ChildNodes[0].Name == "brow")
188 {
189 //int x = type_button_1_location_x, y = type_button_1_location_y;
190 int x = 10, y = 20;
191 int x_button_num = 0;
192 for (int i = 0; i < xml_node_num; i++)
193 {
194 type_button[i] = new Button();
195 type_button[i].Location = new Point(x, y);
196 type_button[i].Size = new Size(type_button_WIDTH, type_button_HEIGHT);
197 type_button[i].Name = ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("ID").ToString();
198 type_button[i].Text = ((XmlElement)xmlDoc.DocumentElement.ChildNodes[i]).GetAttribute("type").ToString();
199 type_button[i].Click += new System.EventHandler(this.type_button_Click);
200
201 this.groupBox1.Controls.Add(type_button[i]);
202
203 x += 140;
204 ++x_button_num;
205 if (x_button_num == 4)
206 {
207 x = type_button_1_location_x;
208 x_button_num = 0;
209 y += 60;
210 }
211
212 }
213
214
215
216 }
217
218 switch (function_select)
219 {
220 case BROW:
221 {
222
223 }
224 break;
225 case EYE:
226 {
227
228 }
229 break;
230 case NOSE:
231 {
232
233 }
234 break;
235 case MOUTH:
236 {
237
238 }
239 break;
240 default: break;
241 }
242
243 }
244 #endregion
245
246 private void type_button_Click(object sender, EventArgs e)
247 {
248 //MessageBox.Show(((Button)sender).Text + " was clicked !");
249
250 if(!File.Exists(@"output_brow.xml"))
251 {
252 XmlTextWriter output_xml = new XmlTextWriter("output_brow.xml", Encoding.UTF8);
253 output_xml.Formatting = Formatting.Indented;
254 output_xml.WriteStartDocument(false);
255
256 output_xml.WriteStartElement("图片标记信息");
257
258 output_xml.WriteComment("这是使用软件标记后输出的图片信息");
259
260 output_xml.WriteEndElement();
261
262 output_xml.Flush();
263 output_xml.Close();
264 }
265
266 XmlDocument xmldoc = new XmlDocument();
267 xmldoc.Load("output_brow.xml");
268 XmlNode root = xmldoc.SelectSingleNode("图片标记信息");
269
270 //创建一个<Node>节点
271 XmlElement xe1 = xmldoc.CreateElement(all_picture_name[current_picture_index - 1]);
272 //设置该节点 type 属性
273 xe1.SetAttribute("type", ((Button)sender).Text);
274
275 //添加到<图片标记信息>节点中
276 root.AppendChild(xe1);
277
278 xmldoc.Save("output_brow.xml");
279
280
281 }
282
283 #region 设置图片路径和输出文件路径
284 private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
285 {
286 current_picture_path = "";
287 all_picture_path.Clear();
288 all_picture_name.Clear();
289
290 FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
291 FolderBrowserDialog1.Description = "请选择图片路径:";
292
293 if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
294 {
295 current_picture_path = FolderBrowserDialog1.SelectedPath;
296
297 DirectoryInfo TheFolder = new DirectoryInfo(current_picture_path);
298 List<string> all = new List<string>();
299
300 foreach (FileInfo NextFile in TheFolder.GetFiles("*.jpg"))
301 {
302 all.Add(NextFile.Name);
303
304 }
305 foreach (FileInfo NextFile in TheFolder.GetFiles("*.bmp"))
306 {
307 all.Add(NextFile.Name);
308
309 }
310 foreach (FileInfo NextFile in TheFolder.GetFiles("*.png"))
311 {
312 all.Add(NextFile.Name);
313
314 }
315
316 if(all.Count > 0)
317 {
318 for (int i = 0; i < all.Count; i++)
319 {
320 all_picture_path.Add(current_picture_path + @"\" + all[i]);
321 all_picture_name.Add(all[i]);
322 }
323
324 current_picture_index = 1;
325
326 buttonLeft.Enabled = true;
327 buttonRight.Enabled = true;
328
329 // 显示图片
330 pictureBox1.Load(all_picture_path[current_picture_index - 1]);
331 }
332 else
333 {
334 MessageBox.Show("此路径下无图片,重新设置");
335 }
336
337 }
338 }
339
340 private void 输出路径ToolStripMenuItem_Click(object sender, EventArgs e)
341 {
342 FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
343 FolderBrowserDialog1.Description = "请设置输出文件路径:";
344
345 if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
346 {
347 output_path = FolderBrowserDialog1.SelectedPath;
348 label1.Text = output_path;
349 }
350 }
351
352 #endregion
353
354
355 #region 上一张或下一张图片
356 private void buttonLeft_Click(object sender, EventArgs e)
357 {
358 if (current_picture_index > 1)
359 {
360 --current_picture_index;
361 }
362 else
363 current_picture_index = all_picture_path.Count;
364
365 pictureBox1.Load(all_picture_path[current_picture_index - 1]);
366
367 switch (function_select)
368 {
369 case BROW:
370 {
371 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片";
372 }
373 break;
374 case EYE:
375 {
376 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片";
377 }
378 break;
379 case NOSE:
380 {
381 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片";
382 }
383 break;
384 case MOUTH:
385 {
386 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片";
387 }
388 break;
389 default: break;
390 }
391 }
392
393 private void buttonRight_Click(object sender, EventArgs e)
394 {
395 if (current_picture_index < all_picture_path.Count )
396 {
397 ++current_picture_index;
398 }
399 else
400 current_picture_index = 1;
401
402 pictureBox1.Load(all_picture_path[current_picture_index - 1]);
403
404 switch(function_select)
405 {
406 case BROW:
407 {
408 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片";
409 }
410 break;
411 case EYE:
412 {
413 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片";
414 }
415 break;
416 case NOSE:
417 {
418 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片";
419 }
420 break;
421 case MOUTH:
422 {
423 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片";
424 }
425 break;
426 default: break;
427 }
428
429
430 }
431 #endregion
432
433 #region 标记功能选择
434 private void 标记眉毛ToolStripMenuItem_Click(object sender, EventArgs e)
435 {
436 function_select = BROW;
437
438 if (all_picture_path.Count != 0)
439 {
440 label1.Text = "正在 标记眉毛...第 " + current_picture_index.ToString() + " 张图片";
441
442 }
443 else
444 {
445 MessageBox.Show("先设置图片路径");
446 }
447
448 Set_example_picture();
449 Load_xml_set_button();
450
451 }
452
453 private void 标记眼睛ToolStripMenuItem_Click(object sender, EventArgs e)
454 {
455
456 function_select = EYE;
457
458 if(all_picture_path.Count != 0)
459 {
460 label1.Text = "正在 标记眼睛...第 " + current_picture_index.ToString() + " 张图片";
461
462 }
463 else
464 {
465 MessageBox.Show("先设置图片路径");
466 }
467 }
468
469 private void 标记鼻子ToolStripMenuItem_Click(object sender, EventArgs e)
470 {
471 function_select = NOSE;
472
473 if (all_picture_path.Count != 0)
474 {
475 label1.Text = "正在 标记鼻子...第 " + current_picture_index.ToString() + " 张图片";
476
477 }
478 else
479 {
480 MessageBox.Show("先设置图片路径");
481 }
482 }
483
484 private void 标记嘴巴ToolStripMenuItem_Click(object sender, EventArgs e)
485 {
486 function_select = MOUTH;
487
488 if (all_picture_path.Count != 0)
489 {
490 label1.Text = "正在 标记嘴巴...第 " + current_picture_index.ToString() + " 张图片";
491
492 }
493 else
494 {
495 MessageBox.Show("先设置图片路径");
496 }
497 }
498 #endregion
499
500
501 }
502 }