HDevelopCodeLearn.cs
9.3 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
using HalconDotNet;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
public class HDevelopCodeLearn
{
public static HWindow hv_ExpDefaultWinHandle;
public static HTuple hv_AcqHandle = null;
public static void HDevelopStop()
{
//MessageBox.Show("Press button to continue", "Program stop");
}
public static void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public static void RunHalcon(HWindow Window, string camerName, string codeType, string paramPath, int codeCount)
{
InitHalcon();
hv_ExpDefaultWinHandle = Window;
action(camerName, codeType, paramPath, codeCount);
}
private static bool OpenCamera(string cameraName)
{
try
{ //[1] HD USB Camera
HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
"default", -1, "false", "default", cameraName, 0, -1, out hv_AcqHandle);
//HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
// -1, "false", "default", "[1] HD USB Camera", 0, -1, out hv_AcqHandle);
return true;
}
catch (Exception ex)
{
LogUtil.error("打开摄像机失败:" + ex.ToString() + ",调用关闭摄像头");
CloseCamera(cameraName);
return false;
}
}
private static void CloseCamera(string cameraName)
{
try
{
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
hv_AcqHandle = null;
}
catch (Exception ex)
{
LogUtil.error("关闭摄像机失败:" + ex.ToString());
}
}
public static bool IsRun = false;
// Main procedure
private static bool action(string cameraName, string codeType, string paramPath, int codeCount)
{
IsRun = true;
IsLearnEnd = false;
IsTestEnd = false;
try
{
HTuple hv_code_type = codeType;
HTuple hv_model_path = paramPath;
HObject ho_Image = null, ho_SymbolXLDs = null;
HTuple hv_train_first = null, /*hv_AcqHandle = null,*/ hv_DataCodeHandle = null;
HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple();
HTuple hv_GenParamNames = new HTuple(), hv_ModelBeforeTraining = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
//Image Acquisition 04: Code generated by Image Acquisition 04
//Image Acquisition 01: Code generated by Image Acquisition 01
//hv_code_type = "Data Matrix ECC 200";
//hv_model_path = ("E:/BaiduNetdiskDownload/" + hv_code_type) + ".dcm";
hv_train_first = 1;
if (!OpenCamera(cameraName))
{
return false ;
}
//HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
HOperatorSet.CreateDataCode2dModel(hv_code_type, new HTuple(), new HTuple(),
out hv_DataCodeHandle);
//set_data_code_2d_param (DataCodeHandle, 'strict_model', 'yes')
//set_data_code_2d_param (DataCodeHandle, 'persistence', 0)
//set_data_code_2d_param (DataCodeHandle, 'polarity', 'light_on_dark')
List<string> findCode = new List<string>();
if ((int)(hv_train_first) != 0)
{
while (findCode.Count < codeCount&&IsLearnEnd.Equals(false ))
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//Image Acquisition 04: Do something
//write_image (Image, 'jpeg', 0, 'E:/BaiduNetdiskDownload/fuba2.jpg')
if (ho_Image != null)
{
ho_SymbolXLDs.Dispose();
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
"train", "all", out hv_ResultHandles, out hv_DecodedDataStrings);
if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleNotEqual(
0))) != 0)
{
HOperatorSet.QueryDataCode2dParams(hv_DataCodeHandle, "get_model_params",
out hv_GenParamNames);
HOperatorSet.GetDataCode2dParam(hv_DataCodeHandle, hv_GenParamNames, out hv_ModelBeforeTraining);
HDevelopStop();
string[] resultList = hv_DecodedDataStrings.SArr;
foreach (string str in resultList)
{
if (!findCode.Contains(str))
{
LogUtil.info("["+cameraName + "][" + codeType + "]学习【" + str+"】");
findCode.Add(str);
}
}
}
ShowImage(ho_Image,ho_SymbolXLDs);
}
Thread.Sleep(500);
}
//*参数写入文件
LogUtil.info("["+cameraName + "][" + codeType + "]保存参数到文件【" + paramPath+"】");
HOperatorSet.WriteDataCode2dModel(hv_DataCodeHandle, hv_model_path);
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
}
//Read the previously saved data code model
findCode = new List<string>();
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
while (findCode.Count < codeCount&&IsTestEnd.Equals(false ))
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//Image Acquisition 04: Do something
//write_image (Image, 'jpeg', 0, 'E:/BaiduNetdiskDownload/fuba2.jpg')
if (ho_Image != null)
{
ho_SymbolXLDs.Dispose();
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
"stop_after_result_num", 5, out hv_ResultHandles, out hv_DecodedDataStrings);
ShowImage(ho_Image,ho_SymbolXLDs);
if ((int)(new HTuple((new HTuple(hv_DecodedDataStrings.TupleLength())).TupleNotEqual(
0))) != 0)
{
string[] resultList = hv_DecodedDataStrings.SArr;
foreach (string str in resultList)
{
if (!findCode.Contains(str))
{
findCode.Add(str);
LogUtil.info("[" + cameraName + "][" + codeType + "]检测到【" + str + "】");
}
}
}
}
Thread.Sleep(500);
}
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
CloseCamera(cameraName);
ho_Image.Dispose();
ho_SymbolXLDs.Dispose();
IsRun = false;
return true;
}catch(Exception ex)
{
LogUtil.error("出错了:" + ex.ToString());
CloseCamera(cameraName);
IsRun = false;
return false;
}
}
public static bool IsTestEnd = false;
public static bool IsLearnEnd = false;
public static void StopLearn()
{
IsTestEnd = true;
IsLearnEnd = true;
}
private static int dWidth = 0;
private static int dHeight = 0;
private static void ShowImage(HObject ho_Image,HObject ho_SymbolXLDs)
{
if (dWidth <= 0)
{
HTuple width, height;
//int dWidth = 0; int dHeight = 0;
HOperatorSet.GetImageSize(ho_Image, out width, out height);
dWidth = (int)width.D;
dHeight = (int)height.D;
hv_ExpDefaultWinHandle.SetPart(0, 0, dHeight, dWidth);
}
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
HOperatorSet.DispObj(ho_SymbolXLDs, hv_ExpDefaultWinHandle);
}
}
}