BoxEqiup_Tongs.cs
9.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public partial class BoxEquip
{
/// <summary>
/// 图像信息
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct EyemImage
{
/// <summary>
/// 地址
/// </summary>
public IntPtr ucpImage;
/// <summary>
/// 图像内存 x 方向大小
/// </summary>
public int iWidth;
/// <summary>
/// 图像内存 y 方向大小
/// </summary>
public int iHeight;
/// <summary>
/// iDepth:
/// uint8_t 取值范围[0, 255], iDepth = 0(常用,解码图像一般为此种格式)
/// int8_t 取值范围[-128, 127], iDepth = 1
/// uint16_t 取值范围[0, 56635], iDepth = 2(常用,点料机图像一般为此种格式)
/// int16_t 取值范围[-32768, 32767], iDepth = 3
/// int32_t 取值范围[-2147483648, 2147483647], iDepth = 4
/// float_t 取值范围[-3.4028235E38, 3.4028235E38], iDepth = 5
/// double_t 取值范围[-1.7E-308, 1.7E+308], iDepth = 6
/// </summary>
public int iDepth;
/// <summary>
/// 彩色图 iChannels=3或4(比RGB多了Alpha通道),一般为3
/// 灰度图 iChannels = 1
/// </summary>
public int iChannels; // 图像通道数
}
[DllImport("libresnet.dll", CharSet = CharSet.None)]
unsafe internal static extern int match_feature(string output_path, EyemImage tpImage, char* partnumber, bool is_enroll, out float score);
[DllImport("libresnet.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int enroll_feature(string input_path, string output_path);
string input_path = "./Tongs/test_huanhong";
string output_path = "./Tongs/feature/";
string temp_path = "./Tongs/temp/";
//学习范围
Rectangle rectangle = new Rectangle(564, 953, 379, 107);
/// <summary>
/// 匹配抓手外观类型
/// </summary>
/// <param name="filename">x光图像文件名</param>
/// <param name="pn">返回标识</param>
/// <returns>匹配分值 -1:匹配失败 >=0:匹配成功</returns>
unsafe float match(string filename, out string pn, out Bitmap bitmap)
{
bitmap = null;
pn = "";
try
{
Directory.CreateDirectory(input_path);
Directory.CreateDirectory(output_path);
Directory.CreateDirectory(temp_path);
Bitmap a = new Bitmap(filename);
Bitmap b = new Bitmap(224, 224, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(b);
g.DrawImage(a, new Rectangle(0, 0, 224, 224), rectangle, GraphicsUnit.Pixel);
g.Save();
EyemImage eyemImage1 = new EyemImage();
eyemImage1.iWidth = 224;
eyemImage1.iHeight = 224;
eyemImage1.iChannels = 4;
eyemImage1.iDepth = 0;
BitmapData bitmapData = b.LockBits(new Rectangle(0, 0, 224, 224), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
eyemImage1.ucpImage = bitmapData.Scan0;
char* p = (char*)Marshal.AllocHGlobal(100);
match_feature(output_path, eyemImage1, (char*)p, false, out float score);
b.UnlockBits(bitmapData);
pn = Marshal.PtrToStringAnsi((IntPtr)p);
Marshal.FreeHGlobal((IntPtr)p);
bitmap = b;
score = score * 100;
return score;
}
catch(Exception e)
{
Common.LogUtil.error("match",e);
}
return 0;
}
public bool MatchAndSaveImg(Bitmap bitmap,string imgname)
{
if (bitmap == null)
return false;
try
{
var score = match(bitmap, out string pn, out Bitmap bitmap1);
var pnpath = Path.Combine($"{temp_path}/{DateTime.Now.ToString("yyyyMMdd")}/", score > Config.TongsDetectThreshold ? "OK" : "NG");
Directory.CreateDirectory(pnpath);
var simpletemp = Path.Combine(pnpath, $"{imgname}-{score.ToString("##.##")}-{pn}.png");
bitmap.Save(simpletemp, ImageFormat.Png);
bitmap.Dispose();
return score > Config.TongsDetectThreshold;
}
catch(Exception e)
{
Common.LogUtil.error("MatchAndSaveImg", e);
}
return false;
}
/// <summary>
/// 匹配抓手外观类型
/// </summary>
/// <param name="filename">x光图像文件名</param>
/// <param name="pn">返回标识</param>
/// <returns>匹配分值 -1:匹配失败 >=0:匹配成功</returns>
public unsafe float match(Bitmap inputImg, out string pn, out Bitmap bitmap)
{
Directory.CreateDirectory(input_path);
Directory.CreateDirectory(output_path);
Directory.CreateDirectory(temp_path);
bitmap = null;
pn = "";
if (inputImg == null)
return 0;
try
{
Bitmap b = new Bitmap(224, 224, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(b);
g.DrawImage(inputImg, new Rectangle(0, 0, 224, 224), rectangle, GraphicsUnit.Pixel);
g.Save();
EyemImage eyemImage1 = new EyemImage();
eyemImage1.iWidth = 224;
eyemImage1.iHeight = 224;
eyemImage1.iChannels = 4;
eyemImage1.iDepth = 0;
BitmapData bitmapData = b.LockBits(new Rectangle(0, 0, 224, 224), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
eyemImage1.ucpImage = bitmapData.Scan0;
char* p = (char*)Marshal.AllocHGlobal(100);
match_feature(output_path, eyemImage1, (char*)p, false, out float score);
b.UnlockBits(bitmapData);
pn = Marshal.PtrToStringAnsi((IntPtr)p);
Marshal.FreeHGlobal((IntPtr)p);
bitmap = b;
score = score * 100;
return score;
}
catch(Exception e)
{
Common.LogUtil.error("match", e);
}
return 0;
}
/// <summary>
/// 学习抓手外观
/// </summary>
/// <param name="filename">x光图像</param>
/// <param name="pn">标识符</param>
public unsafe void learn(string filename, string pn)
{
try
{
Directory.CreateDirectory(input_path);
Directory.CreateDirectory(output_path);
Directory.CreateDirectory(temp_path);
Bitmap a = new Bitmap(filename);
Bitmap b = new Bitmap(224, 224, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(b);
g.DrawImage(a, new Rectangle(0, 0, 224, 224), rectangle, GraphicsUnit.Pixel);
g.Save();
EyemImage eyemImage1 = new EyemImage();
eyemImage1.iWidth = 224;
eyemImage1.iHeight = 224;
eyemImage1.iChannels = 3;
eyemImage1.iDepth = 0;
BitmapData bitmapData = b.LockBits(new Rectangle(0, 0, 224, 224), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
eyemImage1.ucpImage = bitmapData.Scan0;
char* p = (char*)Marshal.StringToHGlobalAnsi(pn).ToPointer();
match_feature(output_path, eyemImage1, (char*)p, true, out _);
b.UnlockBits(bitmapData);
b.Dispose();
}
catch(Exception e)
{
Common.LogUtil.error("learn",e);
}
}
/// <summary>
/// 学习抓手外观
/// </summary>
/// <param name="filename">x光图像</param>
/// <param name="pn">标识符</param>
public unsafe void learn(Bitmap inputimg, string pn)
{
if (inputimg == null)
return;
try
{
Directory.CreateDirectory(input_path);
Directory.CreateDirectory(output_path);
Directory.CreateDirectory(temp_path);
Bitmap b = new Bitmap(224, 224, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(b);
g.DrawImage(inputimg, new Rectangle(0, 0, 224, 224), rectangle, GraphicsUnit.Pixel);
g.Save();
EyemImage eyemImage1 = new EyemImage();
eyemImage1.iWidth = 224;
eyemImage1.iHeight = 224;
eyemImage1.iChannels = 3;
eyemImage1.iDepth = 0;
BitmapData bitmapData = b.LockBits(new Rectangle(0, 0, 224, 224), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
eyemImage1.ucpImage = bitmapData.Scan0;
char* p = (char*)Marshal.StringToHGlobalAnsi(pn).ToPointer();
match_feature(output_path, eyemImage1, (char*)p, true, out _);
b.UnlockBits(bitmapData);
b.Dispose();
}
catch(Exception e)
{
Common.LogUtil.error("learn",e);
}
}
}
}