-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewModel.cs
515 lines (500 loc) · 18.1 KB
/
MainViewModel.cs
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using Main.Helper;
using Basler.Base.Data;
using Basler.Vision;
using System.Threading;
using static Basler.Vision.SiSo;
using System.Diagnostics;
using System.Windows.Media;
using PixelFormat = Basler.Base.PixelFormat;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows;
using Basler.Base;
using Point = System.Drawing.Point;
namespace Main.ViewModels
{
public class MainViewModel : ViewModelBase
{
private SiSo _Siso;
private CancellationTokenSource _cancelToken;
private bool _isTaskRunning;
private string[] _processByDeviceMsg;
private WriteableBitmap _wbmp;
/// <summary>
/// Image windows size in UI. If camera image size is 5000 x 5000 then UI refresh time is slow.
/// So image window size = image size / 4 for speed.
/// </summary>
private Int32Rect _rect;
/// <summary>
/// Camera image size.
/// </summary>
private System.Drawing.Size _size;
private int _updateCount;
private Task[] _process;
private Task _tDisp;
private int _p;
private int _FPSCount;
private DateTime _now;
private PerformanceCounter _CPUUsage;
private LogHandler Log => LogHandler.Instance;
private ConfigHandler Config => ConfigHandler.Instance;
private SystemData SystemData => Config.SystemData;
public MainViewModel()
{
DisplayItems = new ObservableCollection<ResultSelection>()
{
ResultSelection.Result,
ResultSelection.Hue,
ResultSelection.Saturation,
ResultSelection.Intensity,
ResultSelection.Red,
ResultSelection.Green,
ResultSelection.Blue,
};
DebayerFormatItems = new ObservableCollection<PixelFormat>()
{
PixelFormat.GR,
PixelFormat.GB,
PixelFormat.BG,
PixelFormat.RG
};
DeviceItems = new ObservableCollection<Device>()
{
Device.FPGA,
Device.CPU
};
LogMessage = new ObservableCollection<string>();
_processByDeviceMsg = new string[]
{ "Debayer + Color Classification by FPGA",
"Debayer + Color Classification by CPU" };
EnableDisplayImage = true;
_process = new Task[5];
_CPUUsage = new PerformanceCounter("Process", "% Processor Time", "BaslerDemo", true);
}
private int _hueMin;
public int HueMin
{
get => _hueMin;
set
{
SetProperty(ref _hueMin, CheckHSIMinMax(value));
if(_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_HueMin", Convert.ToInt32(value));
}
ColorParam.Hue = new Point(_hueMin, ColorParam.Hue.Y);
}
}
private int _hueMax;
public int HueMax
{
get => _hueMax;
set
{
SetProperty(ref _hueMax, CheckHSIMinMax(value));
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_HueMax", Convert.ToInt32(value));
}
ColorParam.Hue = new Point(ColorParam.Hue.X, _hueMax);
}
}
private int _saturationMin;
public int SaturationMin
{
get => _saturationMin;
set
{
SetProperty(ref _saturationMin, CheckHSIMinMax(value));
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_SaturationMin", Convert.ToInt32(value));
}
ColorParam.Saturation = new Point(_saturationMin, ColorParam.Saturation.Y);
}
}
private int _saturationMax;
public int SaturationMax
{
get => _saturationMax;
set
{
SetProperty(ref _saturationMax, CheckHSIMinMax(value));
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_SaturationMax", Convert.ToInt32(value));
}
ColorParam.Saturation = new Point(ColorParam.Saturation.X, _saturationMax);
}
}
private int _intensityMin;
public int IntensityMin
{
get => _intensityMin;
set
{
SetProperty(ref _intensityMin, CheckHSIMinMax(value));
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_IntensityMin", Convert.ToInt32(value));
}
ColorParam.Intensity = new Point(_intensityMin, ColorParam.Intensity.Y);
}
}
private int _intensityMax;
public int IntensityMax
{
get => _intensityMax;
set
{
SetProperty(ref _intensityMax, CheckHSIMinMax(value));
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_IntensityMax", Convert.ToInt32(value));
}
ColorParam.Intensity = new Point(ColorParam.Intensity.X, _intensityMax);
}
}
private PixelFormat _selectDebayerFormatItem;
public PixelFormat SelectDebayerFormatItem
{
get => _selectDebayerFormatItem;
set
{
SetProperty(ref _selectDebayerFormatItem, value);
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_Debayer", Convert.ToInt32(value));
}
}
}
private ObservableCollection<PixelFormat> _debayerFormatItems;
public ObservableCollection<PixelFormat> DebayerFormatItems
{
get => _debayerFormatItems;
set => SetProperty(ref _debayerFormatItems, value);
}
private ResultSelection _selectDisplayItem;
public ResultSelection SelectDisplayItem
{
get => _selectDisplayItem;
set
{
SetProperty(ref _selectDisplayItem, value);
if (_Siso != null && SelectDeviceItem == Device.FPGA)
{
_Siso.SetParameter("Device1_Process0_Parameters_Display", Convert.ToInt32(value));
}
}
}
private ObservableCollection<ResultSelection> _displayItems;
public ObservableCollection<ResultSelection> DisplayItems
{
get => _displayItems;
set => SetProperty(ref _displayItems, value);
}
private Device _selectDeviceItem;
public Device SelectDeviceItem
{
get => _selectDeviceItem;
set
{
SetProperty(ref _selectDeviceItem, value);
if (value == Device.CPU)
{
_Siso.SetParameter("Device1_Process0_Parameters_Display", Convert.ToInt32(7));
}
else
{
SelectDisplayItem = ResultSelection.Result;
}
if(ProcessByDevice != _processByDeviceMsg[(int)value])
{
ProcessByDevice = _processByDeviceMsg[(int)value];
}
HueMin = ColorParam.Hue.X;
HueMax = ColorParam.Hue.Y;
SaturationMin = ColorParam.Saturation.X;
SaturationMax = ColorParam.Saturation.Y;
IntensityMin = ColorParam.Intensity.X;
IntensityMax = ColorParam.Intensity.Y;
}
}
private ColorClassParam ColorParam
{
get
{
if (SelectDeviceItem == Device.FPGA)
{
return SystemData.ColorFPGA;
}
else
{
return SystemData.ColorCPU;
}
}
}
private ObservableCollection<Device> _deviceItems;
public ObservableCollection<Device> DeviceItems
{
get => _deviceItems;
set => SetProperty(ref _deviceItems, value);
}
private ObservableCollection<string> _logMessage;
public ObservableCollection<string> LogMessage
{
get => _logMessage;
set => SetProperty(ref _logMessage, value);
}
private bool _enableDisplayImage;
public bool EnableDisplayImage
{
get => _enableDisplayImage;
set => SetProperty(ref _enableDisplayImage, value);
}
private string _processByDevice;
public string ProcessByDevice
{
get => _processByDevice;
set => SetProperty(ref _processByDevice, value);
}
private string _fps;
public string Fps
{
get => _fps;
set => SetProperty(ref _fps, value);
}
private string _xy;
public string Xy
{
get => _xy;
set => SetProperty(ref _xy, value);
}
public int X { get; set; }
public int Y { get; set; }
private ImageSource _displayImage;
public ImageSource DisplayImage
{
get => _displayImage;
set => SetProperty(ref _displayImage, value);
}
public byte[] ImageByte { get; set; }
private string _CpuUsag;
public string CPUUsage
{
get => _CpuUsag;
set => SetProperty(ref _CpuUsag, value);
}
private string _Fps;
public string FPS
{
get => _Fps;
set => SetProperty(ref _Fps, value);
}
public void Initialize()
{
Config?.LoadSystemFile();
FPGAInitialize();
SelectDeviceItem = SystemData.SelectDevice;
SelectDebayerFormatItem = SystemData.Format;
SelectDisplayItem = SystemData.Result;
EnableDisplayImage = SystemData.EnableDisplayImage;
}
public void Release()
{
SystemData.SelectDevice = SelectDeviceItem ;
SystemData.Format = SelectDebayerFormatItem;
SystemData.Result = SelectDisplayItem;
SystemData.EnableDisplayImage = EnableDisplayImage;
Config.SaveSystemFile();
FPGARelease();
}
public void FPGAInitialize()
{
_size = new System.Drawing.Size(4992, 5120);
_Siso = new SiSo("Color_1Channel_IWCP.hap", 0, _size.Width, _size.Height, 1);
_Siso.Initialize();
_Siso.SetParameter("Device1_Process0_Camera_ResetStatus", (int)ResetStatus.Off);
_Siso.SetParameter("Device1_Process0_AppletProperties_ExtensionGpioType", (int)GpioType.OpenDrain);
_Siso.SetParameter("Device1_Process0_DRAM_XOffset", Convert.ToUInt32(0));
_Siso.SetParameter("Device1_Process0_DRAM_XLength", Convert.ToUInt32(_size.Width));
_Siso.SetParameter("Device1_Process0_DRAM_YOffset", Convert.ToUInt32(0));
_Siso.SetParameter("Device1_Process0_DRAM_YLength", Convert.ToUInt32(_size.Height));
_Siso.SetParameter("Device1_Process0_Parameters_ImageWidth", Convert.ToInt64(_size.Width));
_Siso.SetParameter("Device1_Process0_Parameters_ImageHeight", Convert.ToInt64(_size.Height));
_Siso.BoardInitialize();
_Siso.StartAcquisition();
_isTaskRunning = true;
_cancelToken = new CancellationTokenSource();
_wbmp = new WriteableBitmap(_size.Width/4, _size.Height/4, 96, 96, PixelFormats.Gray8, null);
_rect = new Int32Rect(0,0, (int)_wbmp.Width, (int)_wbmp.Height);
DisplayImage = _wbmp;
_now = DateTime.Now;
Task.Factory.StartNew(() => {
while(_isTaskRunning)
{
if(_cancelToken.IsCancellationRequested)
{
break;
}
if(SelectDeviceItem == Device.FPGA)
{
FPGATask(_Siso.GetImage(0));
}
else
{
CPUTask(_Siso.GetImage(0));
}
}
}, _cancelToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
public void FPGARelease()
{
_cancelToken?.Cancel();
_Siso?.ReleaseAll();
}
private void FPGATask(IntPtr img)
{
RefreshAsync(img);
}
private void CPUTask(IntPtr img)
{
while(_process[_p] != null && !_process[_p].IsCompleted)
{
_p++;
_p = _p >= _process.Length ? 0 : _p;
Task.Delay(1).Wait();
}
_process[_p] = Task.Run(() => {
var result = CPUProcess(img);
RefreshAsync(result);
});
}
private void RefreshAsync(IntPtr img)
{
_FPSCount++;
if((DateTime.Now - _now).TotalMilliseconds >= 1000 )
{
_now = DateTime.Now;
var pct = _CPUUsage.NextValue() / Environment.ProcessorCount;
FPS = $"{_FPSCount:##}";
CPUUsage = $"{pct:##}%";
Fps = $"FPS: {_FPSCount:##}. CPU={pct:##}%";
_FPSCount = 0;
}
if ((_tDisp != null && !_tDisp.IsCompleted) || !EnableDisplayImage)
{
return;
}
_tDisp = Task.Run(() => {
var simg = VisionHelper.Resize(img, _size.Width, _size.Height, _rect.Width, _rect.Height);
Refresh(simg);
});
}
private void RefreshAsync(byte[] img)
{
_FPSCount++;
if ((DateTime.Now - _now).TotalMilliseconds >= 1000)
{
_now = DateTime.Now;
var pct = _CPUUsage.NextValue() / Environment.ProcessorCount;
FPS = $"{_FPSCount:##}";
CPUUsage = $"{pct:##}%";
Fps = $"FPS: {_FPSCount:##}. CPU={pct:##}%";
_FPSCount = 0;
}
if ((_tDisp != null && !_tDisp.IsCompleted) || !EnableDisplayImage)
{
return;
}
_tDisp = Task.Run(() => {
var ptr = Marshal.AllocHGlobal(img.Length);
Marshal.Copy(img, 0, ptr, img.Length);
var simg = VisionHelper.Resize(ptr, _size.Width, _size.Height, _rect.Width, _rect.Height);
Marshal.FreeHGlobal(ptr);
Refresh(img);
});
}
private int CheckHSIMinMax(int value)
{
if(value <= 0)
{
return 0;
}
if (value >= 255)
{
return 255;
}
return value;
}
void CreateThumbnail(string filename, BitmapSource image5)
{
if (filename != string.Empty)
{
using (FileStream stream5 = new FileStream(filename, FileMode.Create))
{
PngBitmapEncoder encoder5 = new PngBitmapEncoder();
encoder5.Frames.Add(BitmapFrame.Create(image5));
encoder5.Save(stream5);
}
}
}
private void Refresh(byte[] img)
{
if (!EnableDisplayImage)
{
return;
}
ShowRGB(img);
this.DispatchToUi(() =>
{
_wbmp.Lock();
Marshal.Copy(img, 0, _wbmp.BackBuffer, img.Length);
_wbmp.AddDirtyRect(_rect);
_wbmp.Unlock();
if (img == null)
{
CreateThumbnail(@"C:\Temp\fpga_bgr.png", _wbmp.Clone());
}
});
}
private void ShowRGB(byte[] img)
{
try
{
var x = X;
var y = Y;
var w = _rect.Width;
var h = _rect.Height;
_updateCount = (_updateCount + 1) % 1;
if (x >= 0 && x <= _size.Width && y >= 0 && y <= _size.Height && _updateCount == 0 &&
(SelectDisplayItem == ResultSelection.Hue ||
SelectDisplayItem == ResultSelection.Saturation ||
SelectDisplayItem == ResultSelection.Intensity))
{
var stride = y * w;
var g = SelectDeviceItem == Device.CPU ? img[stride + x] : img[stride + x];
var hsi = SelectDisplayItem == ResultSelection.Hue ? "Hue" : SelectDisplayItem == ResultSelection.Saturation ? "Saturation" : "Intensity";
Xy = $"X,Y = {x:#}, {y:#}\r{hsi} = {g}";
}
else
{
Xy = "";
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
private byte[] CPUProcess(IntPtr img)
{
return VisionHandler.Instance.HSIClass.Execute((img, _size.Width, _size.Height), (SelectDisplayItem, SelectDebayerFormatItem,
new Point(HueMin, HueMax), new Point(SaturationMin, SaturationMax), new Point(IntensityMin, IntensityMax)));
}
}
}