轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 06c627ec032b3f3876fd7db8a3ff0ff1a6614fa2
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
using AForge.Video;
using AForge.Video.DirectShow;
using HalconDotNet;
using MvCamCtrl.NET;
using MVSDK;
using LB_VisionProcesses;
using OpenCvSharp;
using SharpCompress;
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Text.RegularExpressions;
using System.Timers;
using System.Windows.Forms;
using LB_VisionProcesses.Cameras;
 
namespace LB_VisionProcesses.Cameras.LocalCameras
{
    public class LocalCamera : BaseCamera
    {
        #region variable
        VideoCaptureDevice _capture = null;
        IntPtr Handle = IntPtr.Zero;
        TriggerMode _triggerMode = TriggerMode.On; // 触发模式
        #endregion
 
        public LocalCamera()
        {
            Brand = CameraBrand.LocalCamera;
        }
 
        public override bool AutoBalanceWhite()
        {
            return true;
        }
 
        public override bool CloseDevice()
        {
            try
            {
                if (_capture != null)
                {
                    _capture.NewFrame -= VideoSource_NewFrame;
                    // 停止采集
                    _capture.SignalToStop();
                    _capture.WaitForStop();
                    _capture.Stop();
                    _capture = null;
                }
 
                isGrabbing = false;
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetExpouseTime(out double value)
        {
            value = 0;
            return true;
        }
 
        public override bool GetGain(out double gain)
        {
            gain = 0;
            return true;
        }
 
        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
        {
            lineStatus = LineStatus.Hight;
            return true;
        }
 
        public override List<string> GetListEnum()
        {
            List<string> cameras = new List<string>();
 
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
 
            foreach (FilterInfo device in videoDevices)
            {
                cameras.Add($"{device.Name}({device.MonikerString})");
            }
            return cameras;
        }
 
        public override bool GetTriggerDelay(out double delay)
        {
            delay = 0;
            return true;
        }
 
        public override bool GetTriggerFliter(out double flitertime)
        {
            flitertime = 0;
            return true;
        }
 
        public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source)
        {
            mode = _triggerMode;
            source = TriggerSource.Software;
            return true;
        }
 
        public override bool GetTriggerPolarity(out TriggerPolarity polarity)
        {
            polarity = TriggerPolarity.RisingEdge;
            return true;
        }
 
        public override bool InitDevice(string SN, object Handle)
        {
            try
            {
                if (!(Handle is IntPtr) || Handle == null)
                {
                    MessageBox.Show("初始化失败,窗体句柄为null !", "异常");
                    return false;
                }
 
                FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
 
                foreach (FilterInfo device in videoDevices)
                {
                    if (SN.Equals($"{device.Name}({device.MonikerString})"))
                    {
                        _capture = new VideoCaptureDevice(device.MonikerString);
                        var formats = _capture.VideoCapabilities;
                        //设置为最高分辨率
                        if (formats.Length > 1)
                            _capture.VideoResolution = formats[0];
 
                        if (_capture.IsRunning)
                            _capture.Stop();
 
                        this.SN = SN;
                        this.Handle = (IntPtr)Handle;
                        try
                        {
                            // 订阅
                            _capture.NewFrame -= VideoSource_NewFrame;
                            _capture.NewFrame += VideoSource_NewFrame;
 
                            // 设置为Off会一直触发回调
                            SetTriggerMode(TriggerMode.On, TriggerSource.Software);
 
                            // 开始拉流 
                            // Start grabbing 
                            if (!StartGrabbing())
                            {
                                Debug.WriteLine("开始采集失败");
                                return false;
                            }
                        }
                        catch { /* Ignore if not supported */ }
                        return true;
                    }
                }
                return false;
            }
            catch { return false; }
        }
 
        private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                Bitmap frame = (Bitmap)eventArgs.Frame.Clone(); // 防止资源占用
                if (frame != null)
                {
                    try
                    {
                        if (this.ImageGrabbed != null)
                            this.ImageGrabbed(this, new CameraEventArgs(SN, frame));
 
                        //if (this.TriggerRunMessageReceived != null)
                        //    this.TriggerRunMessageReceived(SN, "Line0");
 
                        CallBackImg = frame;
                    }
                    catch { }
                }
            }
            catch { }
        }
 
        void Timer_CaptureFrame(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (!isGrabbing)
                    return;
 
                //触发模式为软触发时,定时模拟触发
                if (_triggerMode != TriggerMode.On)
                    _capture.SimulateTrigger(); // 模拟触发
            }
            catch { }
        }
 
        public override bool SetExpouseTime(double value)
        {
            return true;
        }
 
        public override bool SetGain(double gain)
        {
            return true;
        }
 
        public override bool SetLineMode(IOLines line, LineMode mode)
        {
            return true;
        }
 
        public override bool SetLineStatus(IOLines line, LineStatus linestatus)
        {
            return true;
        }
 
        public override bool SetTriggerDelay(double delay)
        {
            return true;
        }
 
        public override bool SetTriggerFliter(double flitertime)
        {
            return true;
        }
 
        public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0)
        {
            try
            {
                _triggerMode = mode;
                if (_triggerMode == TriggerMode.On)
                {
                    // 停止采集
                    _capture.SignalToStop();
                    _capture.WaitForStop();
                }
                else
                    _capture.Start();
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetTriggerPolarity(TriggerPolarity polarity)
        {
            return true;
        }
 
        public override bool SoftTrigger()
        {
            CallBackImg = null;
 
            if (_capture == null)
                return false;
 
            try
            {
                _capture.Start();
                DateTime StartTime = DateTime.Now;
                while (CallBackImg == null && (DateTime.Now - StartTime).TotalSeconds < 5)
                    Thread.Sleep(200);
 
                if (_capture.IsRunning)
                {
                    _capture.SignalToStop();
                    _capture.WaitForStop();
                }
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool StartGrabbing()
        {
            try
            {
                if (_capture == null)
                    return false;
 
                _capture.Start();
                if (_triggerMode == TriggerMode.On)
                {
                    // 停止采集
                    _capture.SignalToStop();
                    _capture.WaitForStop();
                }
 
                CallBackImg = null;
                isGrabbing = true;
                return true;
            }
            catch { return false; }
 
        }
 
        public override bool StopGrabbing()
        {
            try
            {
                if (_capture == null)
                    return false;
 
                _capture.SignalToStop();
                _capture.WaitForStop();
 
                isGrabbing = false;
                CallBackImg = null;
                return true;
            }
            catch { return false; }
        }
 
        public void SetSetting(int show = 1)
        {
            if (_capture != null && Handle != IntPtr.Zero)
                _capture.DisplayPropertyPage(this.Handle);
        }
 
        public override bool GetImage(out Bitmap bitmap, int outtime = 3000)
        {
            bitmap = null;
 
            try
            {
                // 设置超时时间
                DateTime lastTime = DateTime.Now.AddMilliseconds(outtime);
                // 判断是否超时
                while (lastTime > DateTime.Now)// 设置超时时间为 3 秒
                {
                    try
                    {
                        if (CallBackImg != null)
                        {
                            // 保存旧 Bitmap 并释放
                            bitmap = CallBackImg.Clone() as Bitmap; // 创建副本
 
                            // 释放旧资源
                            CallBackImg.Dispose();
                            CallBackImg = null;
                            return true;
                        }
                    }
                    catch { }
                    Thread.Sleep(10);
                }
                return false;
            }
            catch { return bitmap == null ? false : true; }
        }
 
        public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
        {
            if (!isGrabbing)
            {
                StartGrabbing();
            }
            GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
            if (triggerMode != TriggerMode.On && triggerSource != TriggerSource.Software)
            {
                SetTriggerMode(TriggerMode.On, TriggerSource.Software);
            }
            bitmap = null;
            CallBackImg = null;
            if (!SoftTrigger())
            {
                return false;
            }
            // 开始时间
            DateTime startTime = DateTime.Now; // 当前时间
            // 判断是否超时
            while (DateTime.Now < startTime.AddMilliseconds(outtime))// 设置超时时间为 3 秒
            {
                GetImage(out bitmap, 50);
                if (bitmap != null)
                {
                    break;
                }
                Thread.Sleep(10);
            }
            if (triggerMode != TriggerMode.On)
            {
                SetTriggerMode(TriggerMode.On, triggerSource);
            }
            return (bitmap != null);
        }
 
 
        public override void SetCamConfig(CameraConfig config)
        {
            if (Enum.TryParse(config.Params.Inputs["触发模式"].ToString(), out TriggerMode TriggerMode)
                && Enum.TryParse(config.Params.Inputs["触发方式"].ToString(), out TriggerSource TriggerSource)
                && Enum.TryParse(config.Params.Inputs["触发极性"].ToString(), out TriggerPolarity TriggerPolarity)
                )
            {
                SetTriggerMode(TriggerMode, TriggerSource);
                SetTriggerPolarity(TriggerPolarity);
                //SetTriggerFliter(Convert.ToDouble(config.Params.Inputs["触发消抖"].ToString()));
                //SetTriggerDelay(Convert.ToDouble(config.Params.Inputs["触发延时"].ToString()));
                SetExpouseTime(Convert.ToDouble(config.Params.Inputs["曝光时间"].ToString()));
                SetGain(Convert.ToDouble(config.Params.Inputs["增益"].ToString()));
            }
        }
 
        public override void GetCamConfig(out CameraConfig config)
        {
            GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
            GetTriggerPolarity(out TriggerPolarity triggerPolarity);
            //GetTriggerFliter(out double triggerfilter);
            //GetTriggerDelay(out double triggerdelay);
            GetExpouseTime(out double expouseTime);
            GetGain(out double gain);
 
            config = new CameraConfig(null);
            config.Params.Inputs.Add("触发模式", triggerMode);
            config.Params.Inputs.Add("触发方式", triggerSource);
            config.Params.Inputs.Add("触发极性", triggerPolarity);
            //config.Params.Inputs.Add("触发消抖", triggerfilter);
            //config.Params.Inputs.Add("触发延时", triggerdelay);
            config.Params.Inputs.Add("曝光时间", expouseTime);
            config.Params.Inputs.Add("增益", gain);
        }
 
        public override bool StartWith_SoftTriggerModel()
        {
            SetTriggerMode(TriggerMode.Off, TriggerSource.Software);
            return StartGrabbing();
        }
 
        public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
        {
            if (hardtriggeritem == TriggerSource.Software) hardtriggeritem = TriggerSource.Line0;
            SetTriggerMode(TriggerMode.On, hardtriggeritem);
            return StartGrabbing();
        }
 
        public override bool StartContinuousGrab()
        {
            throw new NotImplementedException();
        }
    }
}