EyemManager.cs
12.6 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
using OnlineStore.Common;
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.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class EyemManager
{
public static bool Init()
{
try
{
int flag = eyemInitNNDetector(".\\darknet\\detect-tiny-label.cfg", ".\\darknet\\detect-tiny-label.weights", 640, 640);
if (flag != 0)
{
LogUtil.error("EyemManager" + $"初始化模型失败");
return false;
//MessageBox.Show("初始化模型失败!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
LogUtil.info("EyemManager" + $"init ok ,flag="+ flag);
eyemNNDetectorParams(0.45f, 0.45f);
}
catch (Exception ex)
{
LogUtil.error(" EyemManager Init error :" + ex.ToString());
}
return true;
}
public static bool record = false;//是否保存结果图片
public static bool ReelCheck(Bitmap a)
{
try
{
//string defI = @"D:\出料-4-20230519-110337371.bmp";
//if (a == null&&File.Exists(defI))
//{
// a = new Bitmap(defI);
//}
//Bitmap b = GetReducedImage(a, a.Width, a.Height);
//EyemImage image = new EyemImage();
//image.iWidth = b.Width;
//image.iHeight = b.Height;
//image.iChannels = b.Channels.;
//image.iDepth = 0;
//var bl = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
//image.ucpImage = bl.Scan0;
EyemImage image = eyemCvtToEyemImage2(a);
int ipNum;
BboxContainer bboxes = new BboxContainer();
eyemNNDetector(image, out ipNum, ref bboxes, out EyemImage tpDstImg);
LogUtil.info("EyemManager" + $"eyemNNDetector ,ipNum=" + ipNum);
if (record)
{
var c = eyemCvtToBitmap(tpDstImg);
a.Save($"CameraDebug\\{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.jpg");
c.Save($"CameraDebug\\{DateTime.Now:yyyy-MM-dd-HH-mm-ss}_m.jpg");
c.Dispose();
}
eyemImageFree(ref tpDstImg);
eyemImageFree(ref image);
//b.UnlockBits(bl);
//b.Dispose();
//for (int i = 0; i < ipNum; i++)
//{ }
if (ipNum > 0)
{
return true;
}
}
catch (Exception ex)
{
LogUtil.error(" EyemManager ReelCheck error :" + ex.ToString());
}
return false;
}
public unsafe static EyemImage eyemCvtToEyemImage2(Bitmap bitmap)
{
EyemImage tpImage = new EyemImage();
//锁定数据区
BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, bitmap.PixelFormat);
switch (bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
tpImage.iChannels = 1;
break;
case PixelFormat.Format24bppRgb:
tpImage.iChannels = 3;
break;
case PixelFormat.Format32bppArgb:
tpImage.iChannels = 4;
break;
case PixelFormat.Format32bppRgb:
tpImage.iChannels = 4;
break;
default:
throw new Exception("Image formats are not supported");
}
//仅支持8位
tpImage.iDepth = 0;
//图像尺寸
tpImage.iWidth = bitmap.Width; tpImage.iHeight = bitmap.Height;
//分配内存
tpImage.ucpImage = eyemMallocMemBlock(bd.Stride * bd.Height);
try
{
int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;
long bytesToCopy = tpImage.iWidth * tpImage.iChannels;
for (int y = 0; y < tpImage.iHeight; y++)
{
long offsetSrc = y * pd;
long offsetDst = y * tpImage.iWidth * tpImage.iChannels;
Buffer.MemoryCopy((byte*)(bd.Scan0.ToPointer()) + offsetSrc, (byte*)(tpImage.ucpImage.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
}
}
finally
{
bitmap.UnlockBits(bd);
}
return tpImage;
}
#region 通用
//读取图像,支持彩色与多深度
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemImageRead(string filename, int iFlags, out EyemImage tpImage);
//释放图像资源
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern void eyemImageFree(ref EyemImage tpImage);
#endregion
#region 项目
/// <summary>
/// 初始化检测器
/// </summary>
/// <param name="detectorConfigPath">配置文件</param>
/// <param name="detectorModelPath">模型文件</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemInitNNDetector(string detectorConfigPath, string detectorModelPath, int iNetSizew, int iNetSizeh);
/// <summary>
/// 目标检测器
/// </summary>
/// <param name="tpImage">输入图像</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemNNDetector(EyemImage tpImage, out int ipNum, ref BboxContainer container, out EyemImage tpDstImg);
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemNNDetectorParams(float fConfidence, float fNMSThreshold);
/// <summary>
/// 从进程中的非托管内存分配指定长度的内存
/// </summary>
/// <param name="cb">长度</param>
/// <returns>指向新分配的内存指针</returns>
[DllImport("eyemLib.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr eyemMallocMemBlock(int cb);
#endregion
#region 结构体
//图像信息
[StructLayout(LayoutKind.Sequential)]
public struct EyemImage
{
public IntPtr ucpImage; // 地址
public int iWidth; // 图像内存 x 方向大小
public int iHeight; // 图像内存 y 方向大小
public int iDepth; // 图像位深度(详见说明)
public int iChannels; // 图像通道数
}
// 矩形定义
[StructLayout(LayoutKind.Sequential)]
public struct EyemRect
{
public int iXs; // 起始点(左上角) x 坐标
public int iYs; // 起始点(左上角) y 坐标
public int iWidth; // x 方向大小(宽度)
public int iHeight; // y 方向大小(高度)
}
[StructLayout(LayoutKind.Sequential)]
public struct BboxContainer
{
//最多支持100个目标
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public EyemRect[] bboxes;
}
#endregion
/// <summary>
/// 生成缩略图重载方法1,返回缩略图的Image对象
/// </summary>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图的高度</param>
/// <returns>缩略图的Image对象</returns>
private static Bitmap GetReducedImage(Bitmap resourceImage, int width, int height)
{
if (height == 0)
{
var sc = resourceImage.Width / (float)width;
height = (int)(resourceImage.Height / sc);
}
try
{
Bitmap data = null;
//用指定的大小和格式初始化Bitmap类的新实例
using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
//从指定的Image对象创建新Graphics对象
using (Graphics graphics = Graphics.FromImage(bitmap))
{
//清除整个绘图面并以透明背景色填充
//graphics.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片对象
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
}
data = new Bitmap(bitmap);
}
return data;
}
catch (Exception e)
{
throw e;
}
}
public T DeepClone<T>(T _object)
{
T dstobject;
using (MemoryStream mStream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mStream, _object);
mStream.Seek(0, SeekOrigin.Begin);//指定当前流的位置为流的开头。
dstobject = (T)bf.Deserialize(mStream);
mStream.Close();
}
return dstobject;
}
#region 将EyemImage转换成Bitmap
public static unsafe Bitmap eyemCvtToBitmap(EyemImage tpImage)
{
if (tpImage.ucpImage == IntPtr.Zero)
throw new ArgumentNullException("图像不存在");
if (tpImage.iDepth != 0)
throw new ArgumentException("图像必须是8位无符号整型");
PixelFormat format;
switch (tpImage.iChannels)
{
case 1:
format = PixelFormat.Format8bppIndexed;
break;
case 3:
format = PixelFormat.Format24bppRgb;
break;
case 4:
format = PixelFormat.Format32bppArgb;
break;
default:
return null;
}
Bitmap bitmap = new Bitmap(tpImage.iWidth, tpImage.iHeight, format);
//对于输出灰度图像
if (format == PixelFormat.Format8bppIndexed)
{
ColorPalette palette = bitmap.Palette;
for (int i = 0; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
bitmap.Palette = palette;
}
//锁定数据区
BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, tpImage.iWidth, tpImage.iHeight),
ImageLockMode.WriteOnly, format);
try
{
int pd = ((tpImage.iWidth * tpImage.iChannels) + 3) / 4 * 4;
long bytesToCopy = tpImage.iWidth * tpImage.iChannels;
for (int y = 0; y < tpImage.iHeight; y++)
{
long offsetSrc = (y * tpImage.iWidth * tpImage.iChannels);
long offsetDst = (y * pd);
Buffer.MemoryCopy((byte*)(tpImage.ucpImage.ToPointer()) + offsetSrc, (byte*)(bd.Scan0.ToPointer()) + offsetDst, bytesToCopy, bytesToCopy);
}
}
catch (Exception ex)
{
LogUtil.error("EyemManager eyemCvtToBitmap error:" + ex);
}
finally
{
bitmap.UnlockBits(bd);
}
return bitmap;
}
#endregion
}
}