FrmLearning.cs
14.8 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using Acc.ImageBox;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static OnlineStore.DeviceLibrary.CountParam;
namespace OnlineStore.AutoCountClient
{
public partial class FrmLearning : Form
{
public FrmLearning()
{
InitializeComponent();
}
private SignType CurrType = SignType.AUTO;
private string CurrFullFileName = "";
private string CurrPN = "";
internal string ImagePath = ParamManager.NoConfigPath;
internal string[] ImageFiles;
private List<Button> SelBtnList = new List<Button>();
private string gouStr = "✔ ";
private void FrmLearning_Load(object sender, EventArgs e)
{
ParamManager.Init();
SelBtnList.Add(buttonAuto);
SelBtnList.Add(buttonA);
SelBtnList.Add(buttonB);
SelBtnList.Add(buttonC);
SelBtnList.Add(buttonD);
SelBtnList.Add(buttonE);
buttonAuto.Tag = 99;
buttonA.Tag = 0;
buttonB.Tag = 1;
buttonC.Tag = 2;
buttonD.Tag = 3;
buttonE.Tag = 4;
LoadImgList();
}
private List<string> ImageList = new List<string>();
private int currIndex = -1;
private void LoadImgList()
{
ParamManager.Init();
string[] fileList=new string[] { };
if (ImageFiles != null)
{
fileList = ImageFiles;
}
else if (!Directory.Exists(ImagePath))
{
MessageBox.Show("未找到文件夹:" + ImagePath);
this.Close();
}
else
{
fileList = Directory.GetFiles(ImagePath);
}
lblFolder.Text = "当前文件夹:" + ImagePath;
List<string> list = (from m in fileList orderby m ascending select m).ToList();
foreach (string f in list)
{
string houzhui = Path.GetExtension(f);
if (houzhui.Equals(".png"))
{
ImageList.Add(f);
}
}
if (ImageList.Count <= 0)
{
MessageBox.Show("未找到有效图片");
this.Close();
}
currIndex = -1;
ShowNextImg();
timer1_Tick(null, null);
}
public void ShowNextImg()
{
currIndex++;
if (currIndex >= ImageList.Count)
{
btnNext.Enabled = false;
MessageBox.Show("已经是最后一张图片");
return;
}
string fullFName = ImageList[currIndex];
string filename = Path.GetFileNameWithoutExtension(fullFName);
string[] array = filename.Split('-');
CurrPN = array[0];
groupBox4.Text = "[" + CurrPN + "] 请选择参数:";
CurrFullFileName = fullFName;
lblPnInfo.Text = fullFName;
imgCurrImg.Image = KiLighten((Bitmap)ParamManager.FormImage(fullFName), 80);
int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
imgCurrImg.Zoom = winWidth* 40 /1300;
CountParam p = ParamManager.GetOptimalParamByPN(CurrPN);
if (p != null)
{
numTh.Value = p.Threshold;
numWSize.Value = p.WindowSize;
CurrType = (p.Sign);
}
else
{
CurrType = (SignType.AUTO);
}
imgCurrImg.HorizontalScroll.Value = 173;
if (currIndex >= ImageList.Count - 1)
{
btnNext.Enabled = false;
}
CurrType = (SignType.AUTO);
btnTest_Click(buttonAuto);
if (LoadCSVLibrary.CSVBomManager.allComMap.ContainsKey(CurrPN))
{
ComponetInfo com = CSVBomManager.allComMap[CurrPN];
lblDes.Text = "描述:" + com.Describe;
lblFeng.Text = "封装:" + com.Encapsulations;
lblPN.Text = "元器件编号:" + com.PartNum;
lblProName.Text = "产品:" + com.ProductName;
}
else
{
lblPN.Text = "元器件编号:" + CurrPN;
lblDes.Text = "--";
lblFeng.Text = "--";
lblProName.Text = "--";
}
timer1.Start();
}
private void btnTest_Click(object sender)
{
if (imgCurrImg.Image == null || String.IsNullOrEmpty(CurrFullFileName))
{
MessageBox.Show("未加载图片,无法点料");
return;
}
var btnTest = (Button)sender;
foreach (Button btn in SelBtnList)
{
if (btn.Equals(btnTest))
{
btn.BackColor = Color.LightGreen;
}
else {
btn.BackColor = Color.White;
}
btnTest.Enabled = false;
}
Application.DoEvents();
LogUtil.info( Name + "用户点击 "+btnTest.Text);
try
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
int th = (int)numTh.Value;
int wsize = (int)numWSize.Value;
//Asa.API.EyemImage tpDstImg;
string resfile;
CountParam param = new CountParam("", th, wsize, CurrType);
int outCount = RobotManager.robot.XrayBean.GetCountResult(CurrFullFileName, param, out resfile);
if (System.IO.File.Exists(resfile))
{
Bitmap bmp = FileToBitmap(resfile);
imgCurrImg.Image = bmp;
}
else {
imgCurrImg.Image = KiLighten((Bitmap)ParamManager.FormImage(ImageList[currIndex]), 80);
}
label_coutresult.Text ="点料结果:"+ outCount.ToString();
LogUtil.info( "用户点击 "+ btnTest.Text + "【" + CurrFullFileName + "】 参数 " + param.ToStr() + " 结果:" + outCount);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
LogUtil.error("点料测试出错:" + ex.ToString());
}
this.Cursor = System.Windows.Forms.Cursors.Default ;
SelBtnList.ForEach((b)=> { b.Enabled = true; });
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Visible.Equals(false))
{
return;
}
lblFolder.Text = "当前文件夹:" + ImagePath;
lblCountInfo.Text = " 第" + (currIndex + 1) + "张 / 共" + ImageList.Count + "张 ";
lblImgeInfo.Text = "Zoom:"+imgCurrImg.Zoom + ";" + imgCurrImg.HorizontalScroll.Value + ";" + imgCurrImg.VerticalScroll.Value;
}
private void SaveCurrParam()
{
int th = (int)numTh.Value;
int wsize = (int)numWSize.Value;
CountParam p = new CountParam(CurrPN, th, wsize, CurrType, 0);
ParamManager.UpdateParam(p);
}
private bool IsSave = false;
private void btnSaveParam_Click(object sender, EventArgs e)
{
SaveCurrParam();
int leftC = (ImageList.Count - currIndex - 1);
if (leftC > 0)
{
DialogResult result = MessageBox.Show("当前第[" + (currIndex + 1) + "]张图片,还剩余[" + leftC + "]张图片未选择算法,是否结束学习?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result.Equals(DialogResult.Yes))
{
}
else
{
return;
}
}
ParamManager.SaveMapToFile();
MessageBox.Show("保存成功!");
ClearImage();
IsSave = true;
this.Close();
}
private void ClearImage()
{
try
{
if (!Directory.Exists(ImagePath))
{
return;
}
string targetPath = ImagePath + @"\back\";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
string[] fileList = Directory.GetFiles(ImagePath,"*.png");
List<string> list = (from m in fileList orderby m ascending select m).ToList();
foreach (string f in list)
{
string houzhui = Path.GetExtension(f);
string filename = Path.GetFileNameWithoutExtension(f);
string[] array = filename.Split('-');
string pn = array[0];
CountParam p = ParamManager.GetParamByPN(pn);
if (p != null && p.Sign >= 0)
{
string targetFile = targetPath + filename + houzhui;
File.Move(f, targetPath + filename + houzhui);
//File.Delete(f);
LogUtil.info("算法配置后清理:备份文件【" + f + "】到【" + targetFile + "】,并删除原文件");
}
}
}catch(Exception ex)
{
LogUtil.error("ClearImage 出错:" + ex.ToString());
}
}
private void btnNext_Click(object sender, EventArgs e)
{
//btnNext.Enabled = false;
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
try
{
SaveCurrParam();
ShowNextImg();
}catch(Exception ex)
{
LogUtil.error("下一张 报错:" + ex.ToString());
}
//btnNext.Enabled = true;
this.Cursor = System.Windows.Forms.Cursors.Default;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void FrmLearning_FormClosing(object sender, FormClosingEventArgs e)
{
if (!IsSave)
{
DialogResult result = MessageBox.Show("是否保存参数更改?", "确认是否保存", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result.Equals(DialogResult.Yes))
{
SaveCurrParam();
ShowNextImg();
}
else
{
LogUtil.info("关闭界面时取消了 保存参数更改");
}
}
}
public static Bitmap KiLighten(Bitmap b, int degree)
{
if (b == null)
{
return null;
}
if (degree < -255) degree = -255;
if (degree > 255) degree = 255;
try
{
int width = b.Width;
int height = b.Height;
int pix = 0;
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)data.Scan0;
int offset = data.Stride - width * 3;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// 处理指定位置像素的亮度
for (int i = 0; i < 3; i++)
{
pix = p[i] + degree;
if (degree < 0) p[i] = (byte)Math.Max(0, pix);
if (degree > 0) p[i] = (byte)Math.Min(255, pix);
} // i
p += 3;
} // x
p += offset;
} // y
}
b.UnlockBits(data);
return b;
}
catch
{
return null;
}
} // end of Lighten
private void buttonA_Click(object sender, EventArgs e)
{
CurrType = (SignType.IP_SMALL_PARTS);
btnTest_Click(sender);
}
private void buttonB_Click(object sender, EventArgs e)
{
CurrType = (SignType.IP_LARGE_PARTS);
btnTest_Click(sender);
}
private void buttonC_Click(object sender, EventArgs e)
{
CurrType = (SignType.IP_LONG_PARTS);
btnTest_Click(sender);
}
private void buttonD_Click(object sender, EventArgs e)
{
CurrType = (SignType.IP_SQUARE_PARTS);
btnTest_Click(sender);
}
private void buttonE_Click(object sender, EventArgs e)
{
CurrType = (SignType.IP_GEN_PARTS);
btnTest_Click(sender);
}
private void buttonAuto_Click(object sender, EventArgs e)
{
CurrType = (SignType.AUTO);
btnTest_Click(sender);
}
public static Bitmap FileToBitmap(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
MemoryStream mstream = null;
try
{
mstream = new MemoryStream(bytes);
return new Bitmap((Image)new Bitmap(stream));
}
catch (ArgumentNullException ex)
{
return null;
}
catch (ArgumentException ex)
{
return null;
}
finally
{
stream.Close();
}
}
}
}